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

标签 android android-activity notifications android-pendingintent

我的代码显示通知栏的内容:

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 Activity 总是创建新的(如果 MyActitvity Activity 存在则重复 Activity )。

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

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

请多多指教

最佳答案

当你创建 Intent 时使用这个标志

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

然后在未决 Intent 中使用这个 Intent

关于android - 单击通知栏时复制应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29430445/

相关文章:

Android 媒体播放器错误 (-38, 0) "stop called in state 0"

android - 更大更新后删除 Android 应用程序的所有用户数据

android - 为传入的 SMS 注册 BroadcastReceiver

android - 如何在 Android 中的两个 Activity 之间显示 "Loading"栏

Android - 如何将通知设置为将来的特定日期?

jquery - 让浏览器选项卡闪烁通知

java - 带进度条的 AsyncTask

android - 从纵向 Activity 中以横向方式打开相机时上下文为空

android - 何时保存文档

c# - C# 通知中的新行(紧凑型框架)