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 ?


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:


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.