Android 还有一个通知没有启动活动
我已经在StackOverflow上阅读了(几乎)与同一问题相关的所有其他问题 问题通常是:当我点击我的应用程序发布的Android 还有一个通知没有启动活动,android,notifications,Android,Notifications,我已经在StackOverflow上阅读了(几乎)与同一问题相关的所有其他问题 问题通常是:当我点击我的应用程序发布的通知时,相关的活动没有启动。代码如下: NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); ... String json is prepared ... Intent inten
通知时,相关的活动
没有启动。代码如下:
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
... String json is prepared ...
Intent intentForActivity = new Intent(this, MyActivity.class);
Bundle extras = new Bundle();
extras.putString(Activity.KEY_JSON, json);
intentForActivity.setFlags(
Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_CLEAR_TASK);
intentForActivity.putExtras(extras);
PendingIntent pendingIntent =
PendingIntent.getActivity(this,
0,
intentForActivity,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
... various builder methods for icon, title, message ...
builder.setContentIntent(pendingIntent);
notificationManager.notify(NOTIFICATION_ID++, builder.build());
一些注意事项:
- 我尝试过各种各样的旗帜和排列,没有任何改变李>
- 我必须把
pendingent.FLAG\u UPDATE\u CURRENT
放在extras
中更新字符串json
,否则Android会继续使用第一个
这是我一直在使用的,它对我很有效。代码是不言自明的
private void showNotification(final String title, String text, int ID, boolean showTimeStamp) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) //Use a builder
.setContentTitle(title) // Title
.setContentText(text) // Message to display
.setTicker(text).setSmallIcon(R.drawable.ic_notif_small) // This one is also displayed in ticker message
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.bulb)); // In notification bar
Intent resultIntent = new Intent(this, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
//mBuilder.addAction(R.drawable.bulb_small, "OK", resultPendingIntent);
Notification notification = mBuilder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS | Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;
long time = 0;
if (showTimeStamp)
Calendar.getInstance().getTimeInMillis();
else
time = android.os.Build.VERSION.SDK_INT >= 9 ? -Long.MAX_VALUE : Long.MAX_VALUE;
notification.when = time;
mNotificationManager.cancel(ID);
mNotificationManager.notify(ID, notification);
}
并将此android:launchMode=“singleTask”
添加到您的清单中,您可以在其中执行主要活动
例如:
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:launchMode="singleTask" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
newintent(此,Activity.class);//您是将“活动”放在这里还是您自己想要开始的活动名称?对此表示抱歉。我刚刚编辑了这个问题。谢谢你。谢谢你的回答,但我宁愿让代码尽可能简单:根据谷歌教程,它应该不会那么难。删除与时间相关的东西,声音和LED闪烁,如果你不需要它,但appart从那是没有得到任何sinpler