Android 在通知栏中单击时复制应用程序
我的代码用于将内容显示到通知栏:Android 在通知栏中单击时复制应用程序,android,android-activity,notifications,android-pendingintent,Android,Android Activity,Notifications,Android Pendingintent,我的代码用于将内容显示到通知栏: NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this,
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
}
}
问题是,当我在通知栏中单击通知时,MyActivity活动总是新建的(如果MyActivity活动存在,则重复活动)我想要打开应用程序(这意味着MyActivity活动处于活动状态),而不是创建MyActivity new 仅在应用程序关闭时启动MyActivity活动(MyActivity未激活)
请在创建意图时通知我使用此标志
Intent m_intent = new Intent(this, YourActivity.class);
m_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
然后在挂起的意图中使用此意图您可以使用标志
android:launchMode="singleTop"
在Manifest.xml中的活动中:
<activity
android:name=[your_activity_name]
android:launchMode="singleTop"
android:theme=[your_theme] />
如果需要,请不要忘记在活动中覆盖
onNewIntent(Intent)
。