通知点击在Nexus手机上无法启动指定的Activity

22

我正在使用这段代码来显示本地通知,当通知出现时,希望在单击通知时启动ListActivity。但是在Google Nexus设备上点击通知时无法启动ListActiviy,但在其他设备上,此代码可以正常工作。

    Intent notificationIntent = new Intent(context,
            ListActivity.class);
    notificationIntent.putExtra("clicked", "Notification Clicked");
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |   Intent.FLAG_ACTIVITY_SINGLE_TOP);   // To open only one activity on launch.
    PendingIntent pIntent = PendingIntent.getActivity(context, reqCode,
            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationManager nM = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationCompat.Builder notify = new NotificationCompat.Builder(
            context);

    notify.setContentIntent(pIntent);
    notify.setSmallIcon(R.drawable.app_icon);
    notify.setContentTitle("Hello World");
    notify.setContentText("");
    notify.setAutoCancel(true);
    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    notify.setSound(alarmSound);
    notify.setLights(Color.BLUE, 500, 1000);
    nM.notify(reqCode, notify.build());

在未启动活动时添加logcat:

03-26 14:22:35.893: W/ActivityManager(515): Permission Denial: starting Intent { cmp=com.x.y/.music.ui.PlaybackActivity bnds=[42,101][636,186] (has extras) } from null (pid=-1, uid=10121) not exported from uid 10126
03-26 14:22:35.893: W/ActivityManager(515): Unable to send startActivity intent
03-26 14:22:35.893: W/ActivityManager(515): java.lang.SecurityException: Permission Denial: starting Intent { cmp=com.x.y/.music.ui.PlaybackActivity bnds=[42,101][636,186] (has extras) } from null (pid=-1, uid=10121) not exported from uid 10126
03-26 14:22:35.893: W/ActivityManager(515):     at com.android.server.am.ActivityStackSupervisor.startActivityLocked(ActivityStackSupervisor.java:1186)
03-26 14:22:35.893: W/ActivityManager(515):     at com.android.server.am.ActivityStackSupervisor.startActivityMayWait(ActivityStackSupervisor.java:741)
03-26 14:22:35.893: W/ActivityManager(515):     at com.android.server.am.ActivityManagerService.startActivityInPackage(ActivityManagerService.java:3300)
03-26 14:22:35.893: W/ActivityManager(515):     at com.android.server.am.PendingIntentRecord.sendInner(PendingIntentRecord.java:252)
03-26 14:22:35.893: W/ActivityManager(515):     at com.android.server.am.PendingIntentRecord.send(PendingIntentRecord.java:192)
03-26 14:22:35.893: W/ActivityManager(515):     at android.content.IIntentSender$Stub.onTransact(IIntentSender.java:64)
03-26 14:22:35.893: W/ActivityManager(515):     at android.os.Binder.execTransact(Binder.java:404)
03-26 14:22:35.893: W/ActivityManager(515):     at dalvik.system.NativeStart.run(Native Method)

好的,那是个问题!请看我的回答。 - Hardik
我还检查了Nexus 5(Kitkat 4.4)和Nexus 4(使用更新的操作系统Kitkat 4.4.2),两者都给出了相同的结果。 - Amrit
你可以尝试低于4.4版本的Android系统,例如4.3或4.2等。我认为你可能没有看到我的回答,这是一个与Android操作系统版本KitKat相关的问题。 - Hardik
感谢Hardik的回复... - Amrit
在模拟的4.4版本上,一切都正常 - 这是Nexus的问题。 - AlikElzin-kilaka
显示剩余5条评论
9个回答

66

7
这些链接中的建议之一是,在目标Activity的"activity"标签中添加"android:exported="true",这解决了我在KitKat上的问题。感谢提供链接。 - Kerem
2
PendingIntent.FLAG_CANCEL_CURRENT解决了我的问题。 - Qadir Hussain
1
这解决了我的KitKat问题,但是只有在设备重启后才能打开活动,有其他人遇到过这种情况吗?即在重新启动设备之前,我按下通知按钮,但什么也不会发生,但在重启后,如果我按下按钮,它就会打开活动。 - shaya ajzner
PendingIntent.FLAG_CANCEL_CURRENT 在我的运行 Android 4.3 的 Sony Xperia M 上解决了问题。 - Sam

10

此问题已知于Android Kitkat 4.4和Lollipop版本。请添加:

android:exported="true"
在Android清单文件中,活动标签内的代码将在从状态栏中点击通知后打开。

3
你可以添加 PendingIntent.FLAG_ONE_SHOT 来解决这个问题。
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
                        PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);

1
在清单文件中添加 android:exported = "true"。
<activity
            android:name=".ListActivity"
            android:label="ListActivity"
            android:exported="true">
</activity>

ListActivity是在通知点击时打开的活动。


0

希望这能对你有所帮助。

long uniqueId = System.currentTimeMillis();

    /** This is the part to let your application recognise difference notification when the user click on the notification.
     *  You could use any unique string to represent your notification. But be sure each notification have difference action name.
     **/
    taskDetailIntent.setAction("notifi" + uniqueId);


    PendingIntent contentIntent = PendingIntent.
            getActivity(this, 0, taskDetailIntent, PendingIntent.FLAG_ONE_SHOT);

    builder.setContentIntent(contentIntent);

    /** Set the unique id to let Notification Manager knows this is a another notification instead of same notification.
     *  If you use the same uniqueId for each notification, the Notification Manager will assume that is same notification
     *  and would replace the previous notification. **/
    notificationManager.notify((int) uniqueId, builder.build());

0
如果你用 android:exported="true" 没有效果,只需从你的 LAUNCHER 活动重定向意图即可。
在你的 LAUNCHER 活动中,检查传入的通知如下:
 if(getIntent().hasExtra("type") && getIntent().getStringExtra("type").compareTo("notification")==0){
        Intent redirect = new Intent(getIntent());
        redirect.setClass(this,YourNotificationActivity.class);
        startActivity(redirect);
 }

如果需要过滤意图,则使用以下代码:
 Intent incoming = getIntent();
 if(intent.hasExtra("type") && intent.getStringExtra("type").compareTo("notification")==0){
    Intent outgoing = new Intent(this, YourNotificationActivity.class);
    outgoing.putExtra("title", incoming.getStringExtra("title");
    outgoing.putExtra("content", incoming.getStringExtra("content");
    startActivity(outgoing);
 }

0

mass 提出的解决方案对我很有用,而且非常简单,无需更改您的代码:

在您的 AndroidManifest.xml 文件中,将以下属性添加到您的 <activity> 标签中:

android:exported="true" 

它在安卓4.4.2的三星Galaxy Young 2上运行良好。


0

我的上面的代码在除了Kitkat 4.4和4.4+之外的所有操作系统版本中都能正常工作,但我已经找到了解决方案,将接收器放在另一个进程中,这样它就可以在大多数Android操作系统版本中正常工作...

像这样...

activity android:name=".NotifyReciever" android:process=":remote"

我们可以在这里了解更多关于进程的知识....

我应该在我的接收器中使用android: process =":remote"吗?


0
尝试替换这个

标签


notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);   // To open only one activity on launch.
PendingIntent pIntent = PendingIntent.getActivity(context, reqCode,
        notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);   // To open only one activity on launch.
PendingIntent pIntent = PendingIntent.getActivity(context, reqCode, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);

它将适用于所有版本,甚至包括Kitkat。

注意:Activity将在现有Activity的上下文之外启动,因此您必须在Intent中使用Intent.FLAG_ACTIVITY_NEW_TASK启动标志。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接