【问题标题】:Unable to retrieve same pending intent.无法检索相同的待处理意图。
【发布时间】:2017-06-14 14:40:03
【问题描述】:

即使传递了相同的意图 ID,我也无法检索相同的意图。

我检查了广播接收器在创建待处理意图时使用的上下文完全相同。

public class AlarmReceiver extends BroadcastReceiver {

public static String NOTIFICATION_ID = "notification-id";
public static String NOTIFICATION = "notification";
public static MediaPlayer mMediaPlayer;
public static NotificationManager notificationManager;
public static Notification notification;
public static int id;
Context ctx;

@Override
public void onReceive(final Context context, Intent intent) {
    ctx = context;

    Log.d("WTF", ""+intent.getAction());

    if (intent.getAction() != null) {
        String action = intent.getAction();
        switch (action) {
            case "SNOOZE":
                Log.v("shuffTest", "Pressed Snoozed");
                break;
            case "STOP_ACTION":
                Log.v("shuffTest", "Pressed Stop");
                mMediaPlayer.stop();
                notificationManager.cancel(id);
                stopReceiverServices();
                break;
        }
    } else {
        notificationManager = (NotificationManager) context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = intent.getParcelableExtra(NOTIFICATION);
        id = intent.getIntExtra(NOTIFICATION_ID, 0);
        notificationManager.notify(id, notification);
        playAlarmSound();
    }

    // This is the Intent to deliver to our service.
    //Intent service = new Intent(context, SimpleWakefulService.class);
    // Start the service, keeping the device awake while it is launching.
    //startWakefulService(context, service)
}

public void stopReceiverServices(){
    Intent notificationIntent = new Intent(ctx.getApplicationContext(), AlarmReceiver.class);
    notificationIntent.putExtra(AlarmReceiver.NOTIFICATION_ID, 123);
    notificationIntent.putExtra(AlarmReceiver.NOTIFICATION, notification);
    Log.d("TAG", "Context A1: "+ctx.getApplicationContext().toString());
    Log.d("TAG", "Context A2: "+ctx.toString());
    AlarmManager alarmManager = (AlarmManager) ctx.getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    Log.d("TAG", "AlarmManager Before: " + alarmManager);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(ctx.getApplicationContext(), 987654321, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    Log.d("TAG", "PendingIntent Before: " + pendingIntent);
    alarmManager.cancel(pendingIntent);
    pendingIntent.cancel();
    Log.d("TAG", "AlarmManager After: " + alarmManager);
    Log.d("TAG", "PendingIntent After: " + pendingIntent);
}
/..
}

在创建 PendingIntent 时。

void setAlarm(Context context) {
    mContext = context;
    Intent notificationIntent = new Intent(context, AlarmReceiver.class);
    notificationIntent.putExtra(AlarmReceiver.NOTIFICATION_ID, 123);
    notificationIntent.putExtra(AlarmReceiver.NOTIFICATION, getNotification("Wake Up! Wake Up"));

    AlarmManager alarmManager = (AlarmManager) context.getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), 987654321, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
    calendar.set(Calendar.MINUTE, minuteOfHour);

    alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

    Log.d("TAG", "AlarmManager After setting : " + alarmManager);
    Log.d("TAG", "PendingIntent After setting : " + pendingIntent);

    Log.d("Ctx setAlarm1 ", context.getApplicationContext().toString());
}

日志猫:

06-14 20:01:39.297 23767-23767/com.apps.testapp D/TAG: AlarmManager After setting : android.app.AlarmManager@ed1e807
06-14 20:01:39.297 23767-23767/com.apps.testapp D/TAG: PendingIntent After setting : PendingIntent{7c1d34: android.os.BinderProxy@ff6775d}
06-14 20:01:39.297 23767-23767/com.apps.testapp D/Ctx setAlarm1: com.apps.testapp.ApplicationMain@33d58d2
06-14 20:01:39.305 23767-23881/com.apps.testapp E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb91721a8
06-14 20:01:44.334 23767-23767/com.apps.testapp D/WTF: null
06-14 20:01:44.545 23767-23767/com.apps.testapp D/MediaPlayer: setSubtitleAnchor in MediaPlayer
06-14 20:01:49.772 23767-23767/com.apps.testapp D/WTF: STOP_ACTION
06-14 20:01:49.773 23767-23767/com.apps.testapp V/shuffTest: Pressed Stop
06-14 20:01:49.777 23767-23767/com.apps.testapp D/TAG: Context A1: com.apps.testapp.ApplicationMain@33d58d2
06-14 20:01:49.777 23767-23767/com.apps.testapp D/TAG: Context A2: android.app.ReceiverRestrictedContext@3af302a
06-14 20:01:49.777 23767-23767/com.apps.testapp D/TAG: AlarmManager Before: android.app.AlarmManager@ed1e807 
06-14 20:01:49.780 23767-23767/com.apps.testapp D/TAG: PendingIntent Before: PendingIntent{cc07e1b: android.os.BinderProxy@8441bb8}
06-14 20:01:49.782 23767-23767/com.apps.testapp D/TAG: AlarmManager After: android.app.AlarmManager@ed1e807
06-14 20:01:49.782 23767-23767/com.apps.testapp D/TAG: PendingIntent After: PendingIntent{cc07e1b: android.os.BinderProxy@8441bb8}

【问题讨论】:

  • 你想做什么? “完全相同的意图”是什么意思?
  • 我想在用户关闭通知时取消pendingIntent。
  • 为什么你认为这不能如你所愿?您希望看到什么?

标签: android android-intent alarmmanager android-notifications android-pendingintent


【解决方案1】:

在alarmManager 和PendingIntent 中删除getApplicationConext()

void setAlarm(Context context) {

...

AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context), 987654321, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

...
}

【讨论】:

  • context.getApplicationContext() 和 context 返回相同的值,它会改变任何东西。
  • ^ 这个问题是另一回事,与 pendingIntent 或 alarmManager 无关。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-20
  • 1970-01-01
  • 2019-12-04
相关资源
最近更新 更多