Android 点击通知栏 跳转
来源:互联网 发布:js 多个条件判断 编辑:程序博客网 时间:2024/05/17 09:25
项目中使用的是google的gcm推播服务,当从服务端发送消息给手机客户端时,通知栏会收到一条信息,如果此时用户停留在软件的界面上,应该是点击了通知栏,还是显示在原来的界面上,当用户退出了软件,点击通知栏,应该是回到软件的登陆界面,关键点其实是:
转自:http://www.cnblogs.com/newcj/archive/2011/03/15/1984371.html,感谢这位前辈:
因为不管是在什么时候按Home,再次从程序列表启动时,总能返回到Task的栈顶Activity。起初我想过一个办法,便是重载Activity写一个类实现当onResume的时候更新Notification,然后我的所有Activity类都直接从该类继承,使得当按Home 之后总能让Notification记住Task的栈顶Activity,就像QQ一样,但这种方法当然有点牵强,于是我开始看SDK 中 有关Home的Simple,终于发现了如果使用如下的Intent,便不会调用对应的Activity,而是调用Task中的栈顶ActivityIntent intent = new Intent(Intent.ACTION_MAIN);intent.addCategory(Intent.CATEGORY_LAUNCHER);intent.setClass(this, Main.class);intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); 除了 setClass 可以换成使用 setClassName 绑定,FLAG_ACTIVITY_RESET_TASK_IF_NEEDED可以不设,其他的选项都缺一不可。设置ACTION_MAIN与CATEGORY_LAUNCHER是把该Intent发给了系统对应创建程序的模块,然后系统该模块根据设定的包与类信息还有flags进行处理。当然所有的Intent工作原理都是这样,只是对 ACTION_MAIN - CATEGORY_LAUNCHAR 的处理较为特殊,使得总是显示Task栈顶的Activity而不是setClass设定的Activity类。
重点如下代码,接收通知并处理的函数:
@SuppressWarnings("deprecation")private static void generateNotification(Context context, String message) {int icon = R.drawable.ic_launcher;long when = System.currentTimeMillis();NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);Notification notification = new Notification(icon, message, when);String title = context.getString(R.string.app_name);Intent notificationIntent = new Intent(Intent.ACTION_MAIN);notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);notificationIntent.setClass(context, LoginActivity.class);notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);PendingIntent intent = PendingIntent.getActivity(context, 0,notificationIntent, 0);notification.setLatestEventInfo(context, title, message, intent);notification.flags |= Notification.FLAG_AUTO_CANCEL;notificationManager.notify(0, notification);}
有兴趣的同学可以参考如下链接:
1.http://www.cnblogs.com/newcj/archive/2011/03/15/1984371.html
2.http://blog.csdn.net/jason0539/article/details/11946967
有很多不懂的,希望大牛指导。
1 0
- Android 点击通知栏 跳转
- Android 点击通知栏跳转到应用程序
- Android 通知栏Notification点击跳转无效
- 点击通知栏 跳转 Fragment
- IOS 通知栏点击跳转指定页面
- 通知栏通知,及点击跳转(广播实现)
- Android通知栏点击通知消失
- Android 点击通知栏消息 跳转到指定的Fragment界面
- Android点击通知栏,打开应用跳转到相应的Activity
- Android 点击通知栏消息 跳转到指定的Fragment界面
- Android点击通知栏,打开应用跳转到相应的Activity
- Android点击通知栏消息,仅打开App,不跳转到具体Activity
- 关于百度推送点击通知,跳转的问题(Android)
- Android 通知栏点击取消
- Android通知栏点击事件
- Notification通知,点击通知后跳转
- iOS 点击通知栏消息跳转到指定的页面
- Android缩小到通知栏,再点击通知栏启动
- Binary Tree Level Order Traversal II
- 山东理工ACM:1181
- 单例模式详解
- 第十二周--数据结构--计算图G中出度为0的顶点数
- activity的四种启动模式
- Android 点击通知栏 跳转
- 饿了吗,买早餐带咖啡给我带来了惊吓!
- qt+vs[基础控件介绍一]
- 设计模式(七)门面模式(Facade Pattern 外观模式)
- 存储过程中SELECT与SET对变量赋值
- LaTeX 表格
- iOS 限制UITextField的输入长度
- 怎么解决开发中遇到的问题
- perl | 匹配多个