【问题标题】:PendingIntent not opening Activity in Android 4.3PendingIntent 未在 Android 4.3 中打开 Activity
【发布时间】:2013-09-26 15:04:08
【问题描述】:

在我的Service 中,我使用以下代码在正常运行时打开通知:

private final static NOTIFICATION_ID = 412434;
private void startNotification() {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(
            this);
    builder.setSmallIcon(R.drawable.notification);
    builder.setContentTitle("Running");

    final Intent intent = new Intent(this, MainActivity.class);
    intent.setAction(Intent.ACTION_VIEW);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    final PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(contentIntent);
    builder.setOngoing(true);
    builder.setAutoCancel(false);

    notification = builder.build();

    startForeground(NOTIFICATION_ID, notification);
}

PendingIntent 是在点击通知时打开MainActivity。这在我使用 Android 2.3.3、2.3.5 和 Android 4.1 的所有测试设备上运行良好。

它不起作用,但是在我的 Nexus 7 (Android 4.3) 上,这根本不起作用。当我点击通知时没有任何反应。

这些内容的组合方式是否发生了一些我错过的变化?

【问题讨论】:

  • 看起来不错。 MainActivity 有什么不寻常的地方吗?
  • @CommonsWare:据我所知,它实现了onNewIntent,但它只查找通知未使用的操作。
  • 我最近遇到了类似的问题。我通过为PendingIntent.getActivity requestCode 参数提供一个非 0 值来解决它。
  • @markushi_ 你能回答我这样的问题吗?
  • Android 4.4 上同样的问题

标签: android android-intent android-notifications


【解决方案1】:

在某些 4.3 设备上似乎存在问题。可以通过为requestCode 参数提供一个非 0 值来解决。

例子:

PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);

【讨论】:

  • 根据我处理此问题的经验,我必须将请求代码设置为高于 1000 以确保 PendingIntent 在运行 4.3 的 Nexus 10 上正常工作。所以像REQUEST_CODE_BASE + myRequestCode 这样const REQUEST_CODE_BASE = 1000 似乎可以解决我测试过的所有设备上的问题。
  • 它正在使用 0 然后停止一天切换到 requestcode=1000 它可以工作。谢谢。 Nexus 4,Android 4.4.2
  • 谢谢老兄,拯救了我的一天 :)
  • +1 非常好,在 HTC One 4.4 上遇到了这个问题。 RQ 1 已为我修复
  • 我解决了我的问题。带有毫秒 ID,但谢谢。
【解决方案2】:

这对我有用:

Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(100);
contentIntent = PendingIntent.getActivity(this, randomInt, intent, PendingIntent.FLAG_UPDATE_CURRENT);

【讨论】:

  • 当同一个应用程序有多个通知时,这对我有用。谢谢!
猜你喜欢
  • 2013-06-15
  • 2016-12-05
  • 1970-01-01
  • 2018-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-25
  • 2015-06-29
相关资源
最近更新 更多