单击通知栏时复制应用程序

Duplicate app when click in notification bar

这是我的代码,可以将内容显示到通知栏:

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());

onStart() 在 MyActivity.class

@Override
    protected void onStart() {
        super.onStart();
        if (getIntent().getIntExtra("package.notification", -1) == 123) {
            //check in here
        }
    }

问题是当我单击通知栏中的通知时,MyActitvity activity 总是创建新的(重复 activity 如果 MyActitvity activity 存在)。

我想要打开应用程序(这意味着 MyActitvity activity 处于活动状态),而不是创建新的 MyActitvity。

仅在应用关闭时启动 MyActitvity activity(MyActitvity 未激活)

请多多指教

创建意图时使用此标志

Intent m_intent = new Intent(this, YourActivity.class);
m_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

然后在待定意图中使用此意图

您可以使用标志

 android:launchMode="singleTop"

在你的 activity 在 Manifest.xml:

            <activity
            android:name=[your_activity_name]
            android:launchMode="singleTop"
            android:theme=[your_theme] />