【问题标题】:Duplicate app when click in notification bar在通知栏中单击时重复的应用程序
【发布时间】:2015-04-03 10:29:19
【问题描述】:

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

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 未激活)

请给我建议

【问题讨论】:

    标签: android android-activity notifications android-pendingintent


    【解决方案1】:

    当你创建意图时使用这个标志

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

    然后在待处理的意图中使用此意图

    【讨论】:

      【解决方案2】:

      你可以使用标志

       android:launchMode="singleTop"
      

      在 Manifest.xml 中的活动中:

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

      【讨论】:

      • 如果需要,不要忘记在活动中覆盖 onNewIntent(Intent)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多