6

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

4 Answers4

6

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

Nikolay Elenkov
  • 52,576
  • 10
  • 84
  • 84
  • Store the first time the alarm runs somewhere and calculate the elapsed time each time it runs. If it is more than 1 hour, cancel the alarm. – Nikolay Elenkov Nov 18 '11 at 09:01
  • 2
    No, but here's a hint: you can get the current time using `System.currentTimeMillis()`. One hour is `60 * 60 * 1000` milliseconds. Do the math. – Nikolay Elenkov Nov 18 '11 at 09:07
  • how to calculate elapsed time ? –  Nov 18 '11 at 09:21
  • Last hint: `elapsed = t2 - t1` – Nikolay Elenkov Nov 18 '11 at 09:32
  • I want to be Automaticaly Cancel Alarm after 1 hr..then how i can do this ? –  Nov 19 '11 at 04:53
  • can you help here? http://stackoverflow.com/questions/23316413/alarm-not-getting-stopped/23320549?noredirect=1#23320549 – moDev Apr 27 '14 at 07:40
  • can you please help me to solve this https://stackoverflow.com/questions/51542099/cant-stop-the-ringing-alarm-from-another-activity#51542099 @NikolayElenkov – Zhu Jul 26 '18 at 15:49
4

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);      
   }
Pir Fahim Shah
  • 10,505
  • 1
  • 82
  • 81
2

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.

Nafis Abdullah Khan
  • 2,062
  • 3
  • 22
  • 39
0

You can cancel the alarm by passing the the same pending intent you used while setting up in the cancel method of alarm manager:

am.cancel(pendingIntent);
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Maneesh
  • 6,098
  • 5
  • 36
  • 55
  • 1
    @maneesh....I want to be Automaticaly Cancel Alarm after 1 hr..then how i can do this ? –  Nov 19 '11 at 04:53
  • you have to set another alarm which will act right after one hour and responsible for just only cancelling the specific alarm – Maneesh Nov 19 '11 at 05:30