我的代码是向通知栏显示内容:
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未激活)
请告诉我
答案 0 :(得分:1)
创建意图时,请使用此标记
Intent m_intent = new Intent(this, YourActivity.class);
m_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
然后在待定意图中使用此意图
答案 1 :(得分:1)
您可以使用标记
android:launchMode="singleTop"
在Manifest.xml中的活动中:
<activity
android:name=[your_activity_name]
android:launchMode="singleTop"
android:theme=[your_theme] />