How to Stop Repeating Alarm Services in Android?

12,576

Solution 1

Create the same PendingIntent as when you registered the alarm and use AlaramManager.cancel() to cancel it.

Solution 2

If you have set alarm service for repeating interval of time and you want to cancel your services, then this solution will help you, here first of all i am creating repeating services after every 30 sec, but when i'll press the stop button then it will cancel all the services which i have generated.

  public void AlarmService_afte30sec()
   {

       Toast.makeText(this, "Starting  Alarm Services", Toast.LENGTH_LONG).show();
       Intent myIntent = new Intent(this, StartAlarmServices.class);
       pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);

               alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(System.currentTimeMillis());
                int sec=30;
                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sec*1000, pendingIntent);
     }

Now i'll stop/cancel this services using the same "pendingintent" and "alarmManager", to cancel this the code will be

  public void stopservice()
 {
        alarmManager.cancel(pendingIntent);      
   }

Solution 3

You can pass an intent extra value as 1 hours (or any) from current system time. Then you can check that value from inside the onBind() event of the service and compare it. Following is an example:

 //your code:
 AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
            Intent intent1 = new Intent(C2DMReceiver.this, GPSSetting.class);

 //my code:
 intent1.putExtra("end", System.currentTimeMillis() + 3600000); //current time plus 1 hour

 //your code:
            PendingIntent pendingIntent = PendingIntent.getService(
                    C2DMReceiver.this, 0, intent1, 0); //You might wanna give PendingIntent.FLAG_UPDATE_CURRENT as flag.
            am.setRepeating(AlarmManager.RTC, System.currentTimeMillis(), intervalTime,
                    pendingIntent);

Then inside the onBind() methode:

public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
 long end = intent.getLongExtra("end", 0);
 if(end <= System.currentTimeMillis()){
      AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
      PendingIntent pendingIntent = PendingIntent.getService(
                    this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
      am.cancel(pendingIntent);
 }
    return null;
}

Not sure if it will work for services but it worked for BroadcastReceiver.

Share:
12,576
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    I have totalTime = 1 hr and and intervalTime = 5min, both are in millisecond,

    I want to repeat my GPSSetting services every interval and after completion of totaltime this repeating shoud be stop.

    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
                    Intent intent1 = new Intent(C2DMReceiver.this, GPSSetting.class);
                    PendingIntent pendingIntent = PendingIntent.getService(
                            C2DMReceiver.this, 0, intent1, 0);
                    am.setRepeating(AlarmManager.RTC, System.currentTimeMillis(), intervalTime,
                            pendingIntent);
    

    So how to i can stop this repeating alarm ?

    Regards, Girish