Android通知之通知栏点击通知后返回正在运行的程序,而不是一个新Activity

一般的写法,点击通知栏进入的Activity是一个新创建的Activity,而不是原先正在运行的Activity,这和我的想法是背道而驰的。当你点击返回按键退出这个Activity之后,发现,原先正在运行的Activity终于出现了。明显这样是不符合条理的。 
我们想要点击通知后返回的是正在运行的活动(如果活动正在运行)或者创建新的活动(活动已经停止),应该这样写:

只有在设置PendingIntent这里稍微设置一下就可以。 
一般的写法:

Intent notificationIntent = new Intent(this, MyActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);//PendingIntent获取的是活动
notification.contentIntent = contentIntent;//通知绑定 PendingIntent
notification.flags=Notification.FLAG_AUTO_CANCEL;//设置自动取消
NotificationManager manager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(NOTIFY_ID, notification);
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

下面是稍微设置了的:

// 设置启动的程序,如果存在则找出,否则新的启动
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setComponent(new ComponentName(this, MainActivity.class));//用ComponentName得到class对象
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);// 关键的一步,设置启动模式,两种情况

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);//将经过设置了的Intent绑定给PendingIntent
notification.contentIntent = contentIntent;// 通知绑定 PendingIntent
notification.flags=Notification.FLAG_AUTO_CANCEL;//设置自动取消
NotificationManager manager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(NOTIFY_ID, notification);
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值