I tried to set a background "service" that, every minute, triggers an activity that do some stuff. I found the Alarm Manager class and I wrote this code based on Android doc:
Intent backg = new Intent(getApplicationContext(), CheckConnectivity.class);
boolean backgRunning = (PendingIntent.getBroadcast(getApplicationContext(), 0, backg, PendingIntent.FLAG_NO_CREATE) != null);
if(!backgRunning) {
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, backg, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 60000, pendingIntent);
}
but the service is not triggered every minute, but it seems working only when the screen is off. Do you know why? What am I doing wrong?
Alarm Manager are not reliable, that's why Android introduced Job Scheduler . But you can not use Job Scheduler < 21. So Android introduced Work Manager in Android Architecture Component. Which handle your work internally by AlarmManager or JobScheduler.
Another good approach is to use Evernote's Jobs , which will use WorkManager from next version.
Finally I suggest you use Work Manager instead of AlarmManager.
Also see my explanation on Doze mode / Background restrictions .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.