PendingIntent未在Android 4.3中打开活动

Lan*_*nbo 32 android android-intent android-notifications

在我的中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);
}
Run Code Online (Sandbox Code Playgroud)

PendingIntent是打开MainActivity时,通知被窃听.这在我的所有测试设备上运行得非常好,使用Android 2.3.3,2.3.5和Android 4.1.

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

我错过了这些放在一起的方式有什么变化吗?

mar*_*-hi 79

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

例:

PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Run Code Online (Sandbox Code Playgroud)

  • 根据我对此问题的经验,我必须将请求代码设置为高于1000,以确保PendingIntent在运行4.3的Nexus 10上运行.所以``REQUEST_CODE_BASE + myRequestCode`,其中`const REQUEST_CODE_BASE = 1000`似乎解决了我测试的所有设备上的问题. (13认同)
  • 它正在使用0然后停止一天切换到requestcode = 1000它工作.谢谢.Nexus 4,Android 4.4.2 (5认同)