我在一个活动中设置闹钟,并希望在另一个作为服务的类中取消它。以下是我的代码:
Intent intent = new Intent(SetAlarm.this, AlertFriend.class);
PendingIntent pi = PendingIntent.getActivity(SetAlarm.this, unique_id, inent,
PendingIntent.FLAG_UPDATE_CURRENT);
Calendar c1 = Calendar.getInstance();
c1.set(Calendar.HOUR_OF_DAY, hour);
c1.set(Calendar.MINUTE, min);
c1.set(Calendar.SECOND, 0);
c1.set(Calendar.MILLISECOND, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, c1.getTimeInMillis(), pi);
以下是我要删除警报的地方(其中a是pi):
Intent intent = new Intent(context, AlertFriend.class);
AlarmManager alarm = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarm.cancel(PendingIntent.getActivity(context, a, intent,PendingIntent.FLAG_UPDATE_CURRENT));