开发者

Android canceling all alarm set

I am making an event application and user can set a reminder for events he wants. So i use the alarmManager to create alarms. I would like to put a cancel all option to my main activity so that i could cancel all the alarms created by my application. The usual method for canceling the alarm with the same intent doesnt really help cause i set tha alarms on a different activity than the one I want to cancel them in. So is there a开发者_如何学C way to cancel all the alarms created by my application? Thanks!


The code that executes the cancellation does not have to be the originator of the alarm. Your code identifies the alarm to cancel by identifying the PendingIntent that created it. You can 'manufacture' a facsimile of the original PendingIntent as follows . . .

   String pname = this.getPackageName();

   // manufacture an appropriate context
   Context mycontext = null;
   try {
      mycontext = createPackageContext(pname,CONTEXT_IGNORE_SECURITY);
   } catch (NameNotFoundException e) {
      // handle exception here
      e.printStackTrace();
   }
   // and generate a pendingintent
   PendingIntent pi = PendingIntent.getService(mycontext,
                    0, new Intent(mycontext, myalarmreceiver.class), 0);
   // Now use alarmmanager to terminate the alarm
   AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
   am.cancel(pi);

This may not work in your situation, but if you haven't tried this approach, it may be a good place to start!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜