在 API 21 中,有一个新的 AlarmManager.setAlarmClock(...) 方法,可以设置新的闹钟并显示状态栏警报图标。我是这样使用它的:
AlarmManager.setAlarmClock(...)
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(ALARM_ALERT_ACTION);
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
am.setAlarmClock(new AlarmManager.AlarmClockInfo(time, sender), sender);
问题在于我不知道如何取消这个闹钟,因为这段代码不起作用:
am.cancel(PendingIntent.getBroadcast(context, 0, new Intent(ALARM_ALERT_ACTION), PendingIntent.FLAG_CANCEL_CURRENT));
闹钟本身已经取消(我的BroadcastReceiver的onReceive方法没有被调用),但状态栏中的闹钟图标仍然显示,AlarmManager.getNextAlarmClock()方法返回这个闹钟。
cancel()
调用中删除PendingIntent.FLAG_CANCEL_CURRENT
。 - CommonsWarePendingIntent.FLAG_CANCEL_CURRENT
?因为它不是可选参数。 - AVEbrahimi0
。 - CommonsWare