有我的代码显示内容到通知栏:
NotificationManager mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MyActivity.class).putExtra("package.notification", 123).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK),
PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher).setContentTitle("");
// Set Big View style to see full content of the message
NotificationCompat.BigTextStyle inboxStyle =
new NotificationCompat.BigTextStyle();
inboxStyle.setBuilder(mBuilder);
inboxStyle.bigText("");
inboxStyle.setBigContentTitle("");
inboxStyle.setSummaryText("");
// Moves the big view style object into the notification object.
mBuilder.setStyle(inboxStyle);
mBuilder.setContentText(msg);
mBuilder.setDefaults(Notification.DEFAULT_ALL);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
MyActivity.class中的onStart()
@Override
protected void onStart() {
super.onStart();
if (getIntent().getIntExtra("package.notification", -1) == 123) {
//check in here
}
}
问题是,当我单击通知栏中的通知时,MyActitvity活动总是新建的(如果存在MyActitvity活动,则重复活动)。
我要打开应用程序(这意味着MyActitvity活动处于活动状态),而不是新建MyActitvity。
仅在关闭应用程序时启动MyActitvity活动(MyActitvity未激活)
请给我指教
最佳答案
创建意图时,请使用此标志
Intent m_intent = new Intent(this, YourActivity.class);
m_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
然后在待定意图中使用此意图