By hteo


2018-08-10 12:03:15 8 Comments

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?

1 comments

@Khemraj 2018-08-10 12:39:39

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.

@hteo 2018-08-10 12:59:25

Job scheduler seems perfect for me, also with the limitation <21. If I set a Job every X minutes, and the Job is already running, the old Job is deleted, right?

@Khemraj 2018-08-10 14:35:02

Why you prefer job scheduler over work manager ?

@hteo 2018-08-10 14:46:15

I read some doc about them, and I prefer JobScheduler. My background task is simple.

@Khemraj 2018-08-10 14:48:18

Okay, but that will not work before 21 version.

@hteo 2018-08-10 15:02:40

Yes, I know. Please, answer this. If I set a Job every X minutes, and the Job is already running, the old Job is deleted, right?

Related Questions

Sponsored Content

6 Answered Questions

[SOLVED] Alarm Manager Example

  • 2010-12-16 09:12:17
  • Rajamohan Sugumaran
  • 274382 View
  • 282 Score
  • 6 Answer
  • Tags:   android alarmmanager

1 Answered Questions

Alarm manager stops unexpectedly

1 Answered Questions

1 Answered Questions

Repeating Alarms not working below one minute

1 Answered Questions

[SOLVED] AlarmManager not working as expected in sleep mode on S5 Neo

0 Answered Questions

Android Alarm Manager not being triggered

0 Answered Questions

Alarm manager trigger every time app running

2 Answered Questions

[SOLVED] Using Alarm Manager to Start a Service

0 Answered Questions

How can I minimize a wakeup alarm's running time overhead?

1 Answered Questions

Sponsored Content