关于notification的使用,setLatestEventInfo方法在新SDK不能使用的解决方法
来源:互联网 发布:php 判断数组长度 编辑:程序博客网 时间:2024/06/06 06:42
今天项目刚好要用到 Notification 。刚开始我是这样写的:
Notification notification2 = new Notification(R.mipmap.icon_demo, "通知测试", System.currentTimeMillis());
notification2.setLatestEventInfo(getActivity(),"testTitle","testContent",null);
结果androidstudio报错,setLatestEventInfo该方法找不到,实际上该方法存在于源码中
因为我用的是最新的SDK 23,对比SDK 22的源码如下
比较两个版本的源码我们可以发现Notification.Java在SDK23中比SDK 22中多了一行注释 “@removed”,这意味着如果你的工程是用SDK 23的那么此方法用不了,如果想用的话将编译版本换成 SDK 22即可。
另一个警告是 Notification 的构造方法已经 deprecated
在最新的版本中官方建议用 Notification.Builder来创建 notification 实例
新代码如下:
Notification.Builder builder = new Notification.Builder(getActivity());builder.setContentInfo("补充内容");builder.setContentText("主内容区");builder.setContentTitle("通知标题");builder.setSmallIcon(R.mipmap.icon_demo);builder.setTicker("新消息");builder.setAutoCancel(true);builder.setWhen(System.currentTimeMillis());Intent intent = new Intent(getActivity(), MainActivity.class);PendingIntent pendingIntent = PendingIntent.getActivity(getActivity(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);builder.setContentIntent(pendingIntent);Notification notification = builder.build();manager.notify(NOTIFICATION_ID, notification);运行后的结果如图
这里有几点地方需要注意:
1.如果该通知只是起到 “通知”的作用,不希望用户点击后有相应的跳转,那么,intent,pendingIntent这几行代码可以不写,可以创建延时操作,当通知被成功 notify 后,一段时间后调用manager.cancel(notificationID),将通知清除,此时builder.setAutoCancel()方法不写也可以。
2.如果通知栏下拉后,希望用户点击并有相应的跳转事件。那么,要注意跳转后,通知是否有必要继续存在。如果点击后通知消失,两种方法A 设置setAutoCancel 参数设置为 true,默认是false,B 在intent事件中的如本例中的MainActivity的onCreate()方法调用 manager.cancel(notificationID)取消该通知,此ID要和创建通知的ID一致,否则通知不消失。如果点击不消失,将setAutoCancel 参数设置为 false即可。
- 关于notification的使用,setLatestEventInfo方法在新SDK不能使用的解决方法
- 关于notification的使用,setLatestEventInfo方法在新SDK不能使用的解决方法
- 关于notification的使用,setLatestEventInfo方法在新SDK不能使用的解决方法
- (Notification)setLatestEventInfo方法在新SDK不能使用的解决方法及2个Error
- 关于notification.setLatestEventInfo()的替代方法
- 关于notification.setLatestEventInfo()的替代方法
- Androidn Notification的使用,解决找不到setLatestEventInfo方法
- Androidn Notification的使用,解决找不到setLatestEventInfo方法
- Androidn Notification的使用,解决找不到setLatestEventInfo方法
- Androidn Notification的使用,解决找不到setLatestEventInfo方法
- Androidn Notification的使用,解决找不到setLatestEventInfo方法
- Androidn Notification在新API中的使用(解决找不到setLatestEventInfo方法)
- Android中关于setLatestEventInfo()过时以及构建Notification的解决方法
- Android中关于setLatestEventInfo()过时以及构建Notification的解决方法
- Android中关于setLatestEventInfo()过时以及构建Notification的解决方法
- notification.builder代替notification的setLatestEventInfo()方法
- Android Notification的setLatestEventInfo()@deprecate
- 关于Notification中setLatestEventInfo方法已弃用
- IDA动态调试so源码 F5伪代码查看
- JS-8-封装
- 人脸检测和识别及python实现系列(2)-- 相关API介绍与数据采集
- MPEG
- 数据分析(入门篇)-第三章-Show出你的数据-Part2(水晶易表)
- 关于notification的使用,setLatestEventInfo方法在新SDK不能使用的解决方法
- (八)适配器模式详解
- NGINX和TOMCAT配置动静分离端口丢失问题
- React Native 触摸事件处理详解
- 第四章 文件系统的内部表示
- mysql 连接查询的基本语法
- margin的知识点
- (九)模板方法模式详解(包含与类加载器不得不说的故事)
- linux命令mkdir: cannot create directory Permission denied