0

我有一个创建重复警报的活动(设置警报),我需要从另一个活动中取消该警报(取消警报)。我无法取消闹钟。我究竟做错了什么?:

public class settingalarm...

am =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent alrm = new Intent(context, z.class);
alrm.setAction(ALARM);
alrm.putExtra(x, x);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, alrm, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, 0, 30000, alarmIntent);

我有另一个需要取消现有警报的活动:

public class cancelingalarm...

am =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent alrm = new Intent(context, z.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, alrm, 0);
am.cancel(alarmIntent);

提前致谢!

4

2 回答 2

2

You need to use an equivalent Intent for the PendingIntent you create. In this that means setting the same action. BTW, since you are using an explicit Intent (you specify the class name), there is really no need to add an action as well.

In short: either remove alrm.setAction(ALARM); or use it in both places.

于 2012-12-18T06:32:19.540 回答
0

I am making intent Global & working for me:--

private Intent alrm;


alrm = new Intent(context, z.class);
于 2012-12-18T06:32:15.560 回答