Android 通知单击未在 Nexus 手机上启动给定的活动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21250364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Notification Click not launch the given Activity on Nexus Phones
提问by Amrit
I am using this code to show the local notification and When notification comes then on click of notification want to launch the ListActivity but on Google nexus device ListActiviyis not launches when click on notification, but on other device this code is working well.
我正在使用此代码来显示本地通知,当通知出现时,点击通知想要启动 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());
Adding logcat when the activity is not launched:
活动未启动时添加 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)
采纳答案by Amrit
My above code is working well for all OS version except Kitkat 4.4 and 4.4 + But i have got solution i, put the receiver in another process and it works well for all most Android OS versions...
我上面的代码适用于除 Kitkat 4.4 和 4.4 + 之外的所有操作系统版本,但我有解决方案,将接收器置于另一个进程中,它适用于所有大多数 Android 操作系统版本......
Like this way..
像这样..
activity android:name=".NotifyReciever" android:process=":remote"
活动 android:name=".NotifyReciever" android:process=":remote"
and we can learn more about processes here....
我们可以在此处了解有关流程的更多信息....
回答by Hardik
This is reported issue for kitkat 4.4 not opening activity when click on notification here is an issue url
这是 kitkat 4.4 在点击通知时未打开活动的报告问题是问题网址
http://code.google.com/p/android/issues/detail?id=63236
http://code.google.com/p/android/issues/detail?id=63236
http://code.google.com/p/android/issues/detail?id=61850
http://code.google.com/p/android/issues/detail?id=61850
suggested workaround is to cancel existing PendingIntent, or use PendingIntent.FLAG_CANCEL_CURRENT
建议的解决方法是取消现有的PendingIntent,或使用PendingIntent.FLAG_CANCEL_CURRENT
OR
或者
Try below
试试下面
Adding a flag in Activityin AndroidManifiest.xml
在Activity中添加标志AndroidManifiest.xml
android:exported="true"
回答by satyapol
This issue is known for Android Kitkat 4.4 and Lollipop. Please add :
此问题在 Android Kitkat 4.4 和 Lollipop 中是已知的。请加 :
android:exported="true"
in android manifest inside activity tag which is supposed to be opened after tapping notification from status bar
在活动标签内的android清单中,应该在点击状态栏通知后打开
回答by Istvan Jonyer
You can also add PendingIntent.FLAG_ONE_SHOTto fix this.
你也可以添加PendingIntent.FLAG_ONE_SHOT来解决这个问题。
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
回答by manav patadia
Add android:exported = "true" in the manifest
在清单中添加 android:exported = "true"
<activity
android:name=".ListActivity"
android:label="ListActivity"
android:exported="true">
</activity>
ListActivity is the activity which will be opened on the notification click.
ListActivity 是将在通知点击时打开的活动。
回答by Sonali8890
Try to replace this
尝试替换这个
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);
to
到
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);
it will work for all version even with Kitkat also.
即使使用 Kitkat,它也适用于所有版本。
Note: Activity will be started outside of the context of an existing activity, so you must use the Intent.FLAG_ACTIVITY_NEW_TASK launch flag in the Intent.
注意:活动将在现有活动的上下文之外启动,因此您必须在 Intent 中使用 Intent.FLAG_ACTIVITY_NEW_TASK 启动标志。
回答by Helton Malambane
If android:exported="true"doesn't work for you, just redirect the intent from your LAUNCHER activity.
如果android:exported="true"不适合您,只需从您的 LAUNCHER 活动重定向意图。
In your LAUNCHER activity check for incoming notifications as follows:
在您的 LAUNCHER 活动中,按如下方式检查传入通知:
if(getIntent().hasExtra("type") && getIntent().getStringExtra("type").compareTo("notification")==0){
Intent redirect = new Intent(getIntent());
redirect.setClass(this,YourNotificationActivity.class);
startActivity(redirect);
}
If you need to filter the intent then use:
如果您需要过滤意图,请使用:
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);
}
回答by palasha dbzgt
I hope it will help u.
我希望它会帮助你。
long uniqueId= System.currentTimeMillis();
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());
回答by david.perez
The solution proposed by masshas worked for me and it's very simple, no need to change your code:
提出的解决方案mass对我有用,而且非常简单,无需更改代码:
In your AndroidManifest.xml add the following attribute to your <activity>tag:
在您的 AndroidManifest.xml 中,将以下属性添加到您的<activity>标签中:
android:exported="true"
It has worked in a Samsung Galaxy Young 2 with Android 4.4.2.
它已在运行 Android 4.4.2 的三星 Galaxy Young 2 中运行。

