How to continu a game in background on android ?
Hello, I want create a game on android, which continue to execute a script when the game is not open. How to do that ?
Answer by MattSmiechowski · Oct 22, 2016 at 01:24 PM
Hello.
Taking in consideration Android architecture it's not possible per se to run code in background, when your application is not focused. Android system will pause your application in that situation.
Solution here could be implementing a system that monitors device time, and every time a user opens/focuses the application, it recalculates it's state to take into account time passed. You can use DateTime for that purpose:
System.DateTime
The problem with that solution is, it's easy to cheat or break the app by changing device time.
To make it bulletproof, you'd have to ping some sort of server each time the application is awaken, check the server time and then adjust the application state accordingly.
Mac
Okay I get it. But if I want show a notification for a event in the game, how do I to do to check this event and show the notification, if the game is not focused ?
That's not really what You asked about in the topic. Push notifications are a different thing altogether. Push notifications are linked to an application, but are sent from some sort of a server to an android device. It's not the device application that sends the notification. It's the server application, when certain conditions are met and the notification is triggered. There is a couple of services that offer you a server with ready API, ex: AWS has SNS, or (untested) things like http://www.gamedonia.com/ have push notifications API.
Your answer
Follow this Question
Related Questions
error CS0120: An object reference is required to access non-static member `ScoreManager.score' 1 Answer
How to make an enemy 2d chasing player? 1 Answer
Wrong after Update? 0 Answers
warning CS0618 with unity ngui 0 Answers
Same Q as before but it has been a while and nones replyed so im reposting 2 Answers