Android点击通知栏信息后返回正在运行的程序,而不是一个新Activity
16lz
2021-01-25
From http://blog.sina.com.cn/s/blog_80a855370101hqr5.html
很多网上关于 通知栏的例子都是打开一个新的Activity,代码也很多。
根据那些代码如下public void shownotification(String tab)
{
NotificationManager barmanager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification msg2=new Notification(android.R.drawable.stat_notify_chat,"信息",System.currentTimeMillis());
PendingIntent contentIntent =PendingIntent.getActivity(this, 0,new Intent(this,MsgClient.class),PendingIntent.FLAG_ONE_SHOT);
msg2.setLatestEventInfo(this,"服务器端发回信息了","信息:"+tab, contentIntent);
barmanager.notify(NOTIFICATION,msg2);
//Toast.makeText(ReceiveMessage.this, tab,Toast.LENGTH_SHORT).show();
//System.out.println(tab);
}
写出来运行之后,发现结果基本可以实现,但是点击通知栏进入的Activity是一个新创建的Activity,而不是原先正在运行的Activity,这和我的想法是背道而驰的。当你点击返回按键退出这个Activity之后,发现,原先正在运行的Activity终于出现了。明显这样是不符合条理的。
如果要实现点击通知图标返回已经运行的程序,贴出关键的部分代码。
public void shownotification(String msg)
{
NotificationManager barmanager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification notice = new Notification(android.R.drawable.stat_notify_chat,"服务器发来信息了",System.currentTimeMillis());
notice.flags=Notification.FLAG_AUTO_CANCEL;
Intent appIntent = new Intent(Intent.ACTION_MAIN);
//appIntent.setAction(Intent.ACTION_MAIN);
appIntent.addCategory(Intent.CATEGORY_LAUNCHER);
appIntent.setComponent(new ComponentName(this.getPackageName(), this.getPackageName() + "." + this.getLocalClassName()));
appIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);//关键的一步,设置启动模式
PendingIntent contentIntent =PendingIntent.getActivity(this, 0,appIntent,0);
notice.setLatestEventInfo(this,"通知","信息:"+msg, contentIntent);
barmanager.notify(STATUS_BAR_ID,notice);
更多相关文章
- 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
- Android(安卓)判断一个url是否有效
- Android网络连接判断与相关处理
- 解决h264readInt()值太大 以及 SPS和PPS的获取
- [置顶] Androd用WebView播放gif动画
- RxJava 2: 用Retrofit2架构Android(安卓)MVVM 生命周期
- 技术转载:Android开发之常用代码片段
- Android的源代码下载教程-android学习之旅(102)
- Android(安卓)Jetpack之Paging初探