【发布时间】: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