Android 点击Notification事件处理
来源:互联网 发布:mac air能玩游戏吗 编辑:程序博客网 时间:2024/05/08 09:46
项目中,要实现一个点击Notification跳转到指定页面的功能,但是这个页面是Fragment之一,Fragment又是在viewpager里面,因此不是仅仅打开Activity就可以的,还需要打开activity后设置viewpager.setCurrentItem()。
Intent intent = new Intent(this, MainActivity.class);intent.putExtra("from_mainservice", true);PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);notif.contentIntent = pIntent;
设置了PendingIntent打开MainActivity,并且intent中有boolean字段,但是在MainActivity中,getIntent().getBooleanExtra("from_mainservice",false)获取的值却为false。尝试了onStart(),onResume()的不同位置,都不能得到正确的值。
后来通过查找stack overflow,找到了原因。
点击Notification的时候,MainActivity已经运行,而getIntent()只能获得启动该Activity的Intent,因此获取到的为null。解决方法为在MainActivity中重写onNewIntent(Intent intent)方法,同时声明PedingIntent的时候,添加标记位PendingIntent.FLAG_UPDATE_CURRENT。
Intent intent = new Intent(this, MainActivity.class);intent.putExtra("from_mainservice", true);PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);notif.contentIntent = pIntent;
@Overrideprotected void onNewIntent(Intent newIntent) {super.onNewIntent(newIntent);if(newIntent.getBooleanExtra("from_mainservice", false)) {if(mViewPage != null) {mViewPage.setCurrentItem(2);}} }参考:
http://stackoverflow.com/questions/6352281/getintent-extras-always-null/6357330#6357330
1 0
- Android 点击Notification事件处理
- Android中Notification捕捉点击事件的替代方式
- 自定义Notification添加点击事件
- notification几种点击事件
- Notification点击事件和点击消失
- android graphics画图的点击事件处理
- Android graphics画图的点击事件处理
- android graphics画图的点击事件处理
- Android关于点击事件的特别处理
- android--面试题:多次点击事件处理
- android点击和滑动事件的处理
- 【Android】RecyclerView的OnItemClickListener点击事件处理
- android 点击、滑动事件的处理 GestureDetector
- android webview点击拦截事件处理判断
- Android 系列 6.15处理长时间点击事件
- 浅谈android 点击事件分发处理流程
- 《android按钮点击事件的频繁点击问题处理》
- android-Notification点击跳转Activity
- net.at.json.JSONException
- 堆与堆排序
- An的roi的进阶之数据库操作
- quick-cocos2d-x 3.3 final 中cc.CardinalSplineTo:create导出到lua
- undefined reference to `inflateReset2'
- Android 点击Notification事件处理
- POJ 3126 Prime Path
- 百度地图定位经纬度返回4.9E-324有关问题
- BZOJ 2119 股市的预测 后缀数组
- define中#的用法
- Kinect成像原理
- JavaScript 常用方法总结
- libevent 参考手册
- 仿QQ分组列表(UITableView)