通知单击不在Nexus电话上启动给定的活动

时间:2014-01-21 06:03:52

标签: android events notifications click

我正在使用此代码显示本地通知,当收到通知时,点击通知想要启动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)

9 个答案:

答案 0 :(得分:65)

这是针对kitkat 4.4报告的问题,因为点击此处的通知是一个问题网址

http://code.google.com/p/android/issues/detail?id=63236

http://code.google.com/p/android/issues/detail?id=61850

建议的解决方法是取消现有PendingIntent,或使用PendingIntent.FLAG_CANCEL_CURRENT

OR

尝试以下

Activity

中的AndroidManifiest.xml中添加标记
android:exported="true"

答案 1 :(得分:10)

此问题以Android Kitkat 4.4和Lollipop而闻名。 请添加:

android:exported="true"

在android清单里面的活动标签,应该在点击状态栏的通知后打开

答案 2 :(得分:3)

您还可以添加PendingIntent.FLAG_ONE_SHOT来解决此问题。

PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
                        PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);

答案 3 :(得分:1)

在清单

中添加android:exported =“true”
<activity
            android:name=".ListActivity"
            android:label="ListActivity"
            android:exported="true">
</activity>

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

答案 4 :(得分:0)

我的上述代码适用于除Kitkat 4.4和4.4 +之外的所有操作系统版本 但我有解决方案我,把接收器放在另一个过程中,它适用于所有大多数Android操作系统版本......

就像这样..

活动android:name =“。NotifyReciever”android:process =“:remote”

我们可以在这里了解更多关于流程的信息....

Should I use android: process =":remote" in my receiver?

答案 5 :(得分:0)

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

在AndroidManifest.xml中,将以下属性添加到<activity>标记中:

android:exported="true" 

它已在三星Galaxy Young 2中使用Android 4.4.2。

答案 6 :(得分: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也适用于所有版本。

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

答案 7 :(得分:0)

如果android:exported="true"不适合您,只需从您的启动活动重定向意图。

在您的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);
 }

答案 8 :(得分: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());