Reputation: 2070
I'm using timer task to send updates on my API. The problem is, when the device is inactive, I don't really know the exact state. The timertask stops. Then when I unlock the screen and navigate again, the task started again.
Here's how I do it:
myTimer = new Timer();
UpdateCurrentLocation updateCurrentLocation= new UpdateCurrentLocation();
myTimer.scheduleAtFixedRate(updateCurrentLocation, 0, 900000);
This is updating every 15 minutes. But the activity stops when the device is disabled. I want updateCurrenLocation to be executed every 15 minutes even on background or the device is locked or even the app is not on the front.
Any ideas? Thanks!
Upvotes: 0
Views: 447
Reputation: 72331
You did not described what you are trying to achieve, but most likely you will have to create a Service
or an IntentService
, and make use of AlarmManager
to schedule the Service
to be executed at every 15 minutes.
public static void scheduleSyncAlarm(Context context){
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent syncData = new Intent(context, SyncDataService.class);
PendingIntent pendingSyncData = PendingIntent.getService(context, 0, syncData,
PendingIntent.FLAG_UPDATE_CURRENT);
long syncInterval=AlarmManager.INTERVAL_HOUR/4;
alarmManager.setRepeating(AlarmManager.RTC, 0,
syncInterval, pendingSyncData);
}
Upvotes: 1