【问题标题】:addFlags not working for pendingIntent (SingleTop mode expected)addFlags 不适用于 pendingIntent(预期为 SingleTop 模式)
【发布时间】:2018-07-10 08:42:50
【问题描述】:

如何确保 Activity 以 SingleTop 模式启动? 我不想在 Manifest 中设置 launchMode,但只使用标志。

我的代码有什么问题?帮助表示赞赏。

MainActivity.java

Intent intent=new Intent(this, MainActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                PendingIntent pi=PendingIntent.getActivity(this, 0,intent, 0);

                if(Build.VERSION.SDK_INT>=26){
                    Toast.makeText(MainActivity.this, "Verision>26, id:"+id,Toast.LENGTH_SHORT).show();

                    NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "Channel name",
                            NotificationManager.IMPORTANCE_HIGH);
                    channel.setDescription("description");
                    NotificationManager notificationManager = getSystemService(NotificationManager.class);
                    notificationManager.createNotificationChannel(channel);


                    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
                            .setSmallIcon(R.mipmap.ic_launcher)
                            .setContentTitle(textTitle)
                            .setContentText(textContent)
                            .setAutoCancel(true)
                            .setContentIntent(pi);
                    notificationManager.notify(id, mBuilder.build());

                }

编辑

用 setFlags 替换 addFlags 对我不起作用...

【问题讨论】:

    标签: android android-intent android-notifications


    【解决方案1】:

    尝试 setFlags 而不是 addFlags

    Intent intent=new Intent(this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

    请检查 Intent() 的参数一次。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-18
      • 2014-06-03
      • 1970-01-01
      • 1970-01-01
      • 2018-03-03
      相关资源
      最近更新 更多