The game I am working on is a missile-oriented GPS-based combat game on Android. The app checks every 15 minutes to see if the user is under attack by any other players, and if so, sends them a notification that they are under attack. Currently, because of the minimum 15 minute interval, the app sends these notifications either too late or not at all. What I need to do is alter this so that somehow, some way, the app checks the "under attack" status of the user more often than 15 minutes. Every minute or every 30 seconds would be ideal.
here is the doWork() method which starts the notification check:
{
if(!MainActivity.GetRunning())
{
Utilities.DebugLog(context, "AlertService", "Main activity not running. Firing notification service handler.");
NotificationServiceHandler handler = new NotificationServiceHandler(context);
handler.Start();
}
return Result.SUCCESS;
}
CodePudding user response:
WorkManager
is not a suitable tool for what you wish to do. You will need to use a foreground service and your own in-process timing engine (e.g., ScheduledExecutorService
). That will not work for very long before Doze mode and other power-saving measures take effect, but hopefully your games are only an hour or so long.
CodePudding user response:
Hej theBiscuit,
instead of using WorkManager
, you could set up an AlarmManager
to wake up the app and check for attacks.
If you want to do it while the app is running, a CountDownTimer could help for short periods of time.