addFlags无法用于未决的Intent(应为SingleTop模式)

时间:2018-07-10 08:42:50

标签: android android-intent android-notifications

如何确保活动以SingleTop模式启动? 我不想在清单中设置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对我不起作用...

1 个答案:

答案 0 :(得分:-1)

尝试使用 setFlags 而不是 addFlags

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

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