Alarmmanager is not repeating

Asked

Viewed 131 times

1

I have an alarm that is triggered every 5 minutes, and it calls an intentService to test a condition that if true, sends notification to the user, otherwise he does nothing.

But the intentService is only being called once, and only if I open the application (even if open I need to reopen), I even put to send a notification in case the condition is false to make sure that it is the alarmManager that is not repeating.

public class AlarmMgr {
    AlarmManager alarmMgr;
    public void Alarm(Context context) {
            // Set the alarm here.
            PendingIntent alarmIntent;

            alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            Intent intentA = new Intent(context, BlockFoundService.class);
            alarmIntent = PendingIntent.getBroadcast(context, 0, intentA, PendingIntent.FLAG_UPDATE_CURRENT);

            // setRepeating() lets you specify a precise custom interval--in this case,
            alarmMgr.setRepeating(AlarmManager.RTC, System.currentTimeMillis(),
                    1000 * 60 * 5 , alarmIntent);
    }
    public void CancelAlarm (Context context){
        if (alarmMgr!= null) {
            Intent intentA = new Intent(context, BlockFoundService.class);
            PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intentA, PendingIntent.FLAG_UPDATE_CURRENT);
            alarmMgr.cancel(alarmIntent);
        }
    }
}
  • I’ve had this problem, what API are you using? what level?

1 answer

0


That way the alarm can never launch the Intentservice.

You’re building the Pendingintent with PendingIntent.getBroadcast(), which serves to launch Broadcast Receivers. To launch a Service you must use PendingIntent.getService().

Substitute

alarmIntent = PendingIntent.getBroadcast()(context, 0, intentA, PendingIntent.FLAG_UPDATE_CURRENT);

for

alarmIntent = PendingIntent.getService(context, 0, intentA, PendingIntent.FLAG_UPDATE_CURRENT);
  • Even though it is not repeating, it happens only once when opening the application.

  • Are you launching Intentservice elsewhere than through Alarmmanager? What version of Android is the application running on?

  • Yes, I was kind of hiding haha Thanks for the help! :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.