android开发之让Android点击通知栏信息后返回正在运行的程序

在应用里使用了后台服务,并且在通知栏推送了消息,希望点击这个消息回到activity,
  结果总是存在好几个同样的activity,就算要返回的activity正在前台,点击消息后也会重新打开一个一样的activity,返回好几次才能退出,
  而不能像qq之类的点击通知栏消息回到之前存在的activity,如果存在就不再新建一个activity
  说的有点绕,如果是遇到此类问题的肯定能懂,没遇到过的估计看不懂我这混乱的表达了.....
  经过查阅资料,通过如下方式解决了,
  其实主要功劳就是第10行,
  java代码
  1. public void shownotification(String msg)

  2.   {
  3.   NotificationManager 
  4. barmanager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
  5.   Notification notice = new 
  6. Notification(android.R.drawable.stat_notify_chat,"服务器发来信息了",System.currentTimeMillis());
  7.   notice.flags=Notification.FLAG_AUTO_CANCEL;
  8.   Intent appIntent = new Intent(Intent.ACTION_MAIN);
  9.   //appIntent.setAction(Intent.ACTION_MAIN);
  10.   appIntent.addCategory(Intent.CATEGORY_LAUNCHER);
  11.   appIntent.setComponent(new ComponentName(this.getPackageName(), 
  12. this.getPackageName() + "." + this.getLocalClassName()));
  13.   appIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| 
  14. Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);//关键的一步,设置启动模式
  15.   PendingIntent contentIntent =PendingIntent.getActivity(this, 
  16. 0,appIntent,0);
  17.   notice.setLatestEventInfo(this,"通知","信息:"+msg, contentIntent);
  18.   barmanager.notify(STATUS_BAR_ID,notice);
  19.   }
复制代码

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值