【问题标题】:Intent.getIntExtra() is giving unexpected resultsIntent.getIntExtra() 给出了意想不到的结果
【发布时间】:2018-03-27 10:23:03
【问题描述】:

我正在使用NotificationCompat.Builder 类发布通知并向其添加一个名为取消的操作按钮
我希望,单击此操作按钮应该会取消通知。为此,我通过在 Intent 上调用 putExtra(String, int) 发送广播并将通知 ID 附加到 Intent 对象
BroadcastReceiver 通过调用getIntExtra(String, int) 接收到Notification 的id 并通过调用NotificationManagerCompat.cancel(id) 取消通知

但是,每次我尝试,BroadcastReceiver 收到的id 的值都是 8。因此,我无法取消通知。


这是我将操作按钮添加到通知的代码,

 Intent cancelIntent = new Intent(this, NotificationActionReceiver.class);
 cancelIntent.putExtra(EXTRA_NOTIFICATION_ID, id);
 cancelIntent.setAction(ACTION_DISMISS);
 if (BuildConfig.DEBUG)
 Log.d(TAG, "Sent id: "+id);
 PendingIntent pendingCancelIntent = PendingIntent.getBroadcast(this, 0, cancelIntent, 0);
 builder.addAction(R.drawable.ic_dismiss, "Cancel", pendingCancelIntent);

BroadcastReceiver 类

 public class NotificationActionReceiver extends BroadcastReceiver {

    private final static String TAG = NotificationActionReceiver.class.getSimpleName();

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "onReceive(), getAction == "+intent.getAction());

        switch (intent.getAction()){

            case MyActivity.ACTION_DISMISS:

                int id = intent.getIntExtra(MyActivity.EXTRA_NOTIFICATION_ID, -1);
                if (BuildConfig.DEBUG)
                Log.d(TAG, "Notification id received: "+id);

                NotificationManagerCompat managerCompat = NotificationManagerCompat.from(context);
                managerCompat.cancel(id);
                break;

            case ....:
            ....
        }
    }
 }



日志猫总是报告 BroadcastReciver 获取通知 id 的值为 8

【问题讨论】:

  • 正如@Nik 所说,生成PendingIntent 时必须使用标志PendingIntent.FLAG_UPDATE_CURRENT。这可确保您的“额外”将在PendingIntent 中。否则,您最终只会引用现有的 PendingIntent,其中可能包含其他“附加”。

标签: android android-intent android-notifications


【解决方案1】:

我正在做同样的事情,我得到了正确的输出:

ComponentName receiver = new ComponentName(context, AlarmReceiver.class);
        PackageManager pm = context.getPackageManager();

        pm.setComponentEnabledSetting(receiver,
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);

        Log.v(TAG," Notification id:" + model.getNotificationIdForLocalDB());

        Intent intent1 = new Intent(context, AlarmReceiver.class);
        intent1.putExtra("notification_id", model.getNotificationIdForLocalDB());
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, model.getNotificationIdForLocalDB(), intent1, PendingIntent.FLAG_UPDATE_CURRENT);

在 BroadcastReceiver 结束:

if(intent != null){
            if(intent.getExtras() != null){
                notificationId = intent.getExtras().getInt("notification_id",0);
            }
        }

尝试将待处理意图标志 0 更改为 PendingIntent.FLAG_UPDATE_CURRENT

【讨论】:

猜你喜欢
  • 2019-05-19
  • 2017-01-05
  • 2021-10-14
  • 1970-01-01
  • 2021-12-17
  • 2021-04-22
  • 2016-02-13
  • 2017-11-05
  • 1970-01-01
相关资源
最近更新 更多