0

我有一个PendingIntentAlarmManager The PendingIntents 中使用的唯一 ID,我会跟踪这些 ID。PendingIntent因此,当我尝试取消它时,我应该能够通过传递唯一 ID 来取消它。我正在尝试以Fragment与我创建它的不同的方式取消它,如果这有什么不同的话。

但是即使在查看了许多过去的问题之后,我也无法取消它。

我看过例如:https://stackoverflow.com/a/4350149/2442638https://stackoverflow.com/a/12257277/2442638https://stackoverflow.com/a/11682008/2442638

这就是我创建PendingIntent并设置它的方式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);

这就是我试图取消 PendingIntent 的方式:

// 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);
4

0 回答 0