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);
   

更多相关文章

  1. 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
  2. Android(安卓)判断一个url是否有效
  3. Android网络连接判断与相关处理
  4. 解决h264readInt()值太大 以及 SPS和PPS的获取
  5. [置顶] Androd用WebView播放gif动画
  6. RxJava 2: 用Retrofit2架构Android(安卓)MVVM 生命周期
  7. 技术转载:Android开发之常用代码片段
  8. Android的源代码下载教程-android学习之旅(102)
  9. Android(安卓)Jetpack之Paging初探

随机推荐

  1. DRBD+Heartbeat+Mysql高可用读写分离架构
  2. 在sqlzoo上自学加入#10教程
  3. 如何确定使用Java和MySQL的插入或更新是
  4. linux6.4搭建mysql主从复制
  5. php数据库数据转换为js中的json对象
  6. mysql 怎么给一个表一次增加多个字段, mys
  7. mysql 数据库自增id 的总结
  8. 最近升级mysql5.7出现下面问题,ORDER BY c
  9. win server 2008 R2 已有apache 、mysql,
  10. mysql必知必会(一、数据库基础知识)