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

我使用这段代码来显示本地通知,当通知来的时候点击通知要启动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.xy/.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.xy/.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) 

Solutions Collecting From Web of "通知单击不在Nexus电话上启动给定的活动"

这是报告问题kitkat 4.4不开放活动时点击通知这里是一个问题的url

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

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

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

要么

尝试下面

AndroidManifiest.xml Activity中添加一个标志

 android:exported="true" 

Android Kitkat 4.4和棒棒糖已知此问题。 请加 :

 android:exported="true" 

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

您也可以添加PendingIntent.FLAG_ONE_SHOT来解决这个问题。

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

在清单中添加android:exported =“true”

 <activity android:name=".ListActivity" android:label="ListActivity" android:exported="true"> </activity> 

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

尝试replace这个

 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启动标志。

我上面的代码适用于所有操作系统版本,除了Kitkat 4.4和4.4 +但我已经得到了解决scheme,把接收器放在另一个进程中,它适用于所有大多数Android操作系统版本。

像这样

activity android:name =“。NotifyReciever”android:process =“:remote”

我们可以在这里了解更多关于stream程

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

mass提出的解决scheme已经为我工作,这是非常简单的,不需要改变你的代码:

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

 android:exported="true" 

它已经在Android 4.4.2的三星Galaxy Young 2中工作。

如果android:exported="true"不适合你,只需从你的LAUNCHER活动中redirect。

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