Notification返回到当前正在运行的Activity

来源:互联网 发布:葡萄妈家的鞋子知乎 编辑:程序博客网 时间:2024/06/04 18:23

Notification notification=...

NotificationManager nm=...

//

Intent notificationIntent = new Intent(this,this.getClass()); 

PendingIntent contentIntent = PendingIntent.getActivity(this, 0,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);  

nm.notify(1, notification);

 

new Intent(this,this.getClass())保证了点击通知栏里的通知可以回到该Activity

但是,假如该Activity还在后台运行,并没有运行,通知事件响应后,系统会自动结束该Activity,然后再重新启动Activity,这不是我们要的。

 

解决方法为:在manifest.xml文件中找到该Activity,添加属性android:launchMode="singleTask“。这个属性很明显,就是只允许有一个该Activity运行,如果正在运行,则只能切换到当前运行的Activity,而不能重新启动Activity。

原创粉丝点击