Search code examples
androidandroid-alarms

Problem with AlarmManager In Android


When I run my application for the first time my alarm is started and it works very well.

Intent intent = new Intent(this, Kill_Task.class);  
PendingIntent pendingIntent = PendingIntent.getBroadcast(
                   this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);  

AlarmManager objAlarmManager = 
     (AlarmManager) getSystemService(Context.ALARM_SERVICE);  
objAlarmManager.setRepeating(
     AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000, pendingIntent);

Now my alarm is continuing in background and I have to switch off my phone. When I switch on my phone my alarm background processing does not start.

Why is this?


Solution

  • 1) your alarm repeat every second your defined pending intent object . AlarmManager.setRepeating(...
    2) to cancel/stop your alarm you've to invoke objAlarmManager.cancel(pendingIntent); . with a pendingIntent that match your pending intent that you've define (same class, same action ... except extra data that is not taking into account for comparizon ... see IntenFilter to know more about how intent may match another one
    3) then cancel the pending intent itself like pendingIntent.cancel(); to be sure that pending intent is unusable.