我有一个PendingIntent
用于AlarmManager
PendingIntent
我有独特的ID,我会跟踪它。因此,我可以通过在尝试取消它时传递唯一ID来取消PendingIntent
。我试图在与我创建的Fragment
不同的PendingIntent
中取消它,如果这有任何区别的话。
但是,即使看过很多过去的问题,我也无法取消它。
我看过例如:https://stackoverflow.com/a/4350149/2442638,https://stackoverflow.com/a/12257277/2442638,https://stackoverflow.com/a/11682008/2442638
这是我创建AlarmManager
的方式,并使用// remCounter is unique for each PendingIntent
Intent remIntent = new Intent();
remIntent.setAction("com.example.test.remBroadcast");
Bundle extras = new Bundle();
extras.putString("content_title", (String) s_content_title);
extras.putString("content_text", (String) s_content_text);
extras.putBoolean("bOngoing", boolean_ongoing);
extras.putInt("remCounter", remCounter); // This is how I keep track of the unique id. I have proved this to work.
remIntent.putExtras(extras);
PendingIntent remPendingIntent = PendingIntent.getBroadcast(
getActivity(), remCounter, remIntent,
PendingIntent.FLAG_ONE_SHOT);
getActivity(); // (Do I need this?)
AlarmManager remAlarmManager = (AlarmManager) getActivity()
.getSystemService(Context.ALARM_SERVICE);
remAlarmManager.set(AlarmManager.RTC_WAKEUP,
setForCal,
remPendingIntent);
:
// cancelCounter is the value of the unique id of the PendingIntent I want to cancel
Intent intent = new Intent();
PendingIntent pi = PendingIntent.getBroadcast(getActivity(),
cancelCounter, intent, PendingIntent.FLAG_UPDATE_CURRENT);
pi.cancel();
AlarmManager am = (AlarmManager) getActivity()
.getSystemService(Context.ALARM_SERVICE);
am.cancel(pi);
这就是我试图取消PendingIntent的方式:
{{1}}