Androidn Notification的使用,解决找不到setLatestEventInfo方法
来源:互联网 发布:恒生电子 java 工资 编辑:程序博客网 时间:2024/06/05 14:15
转自:http://blog.csdn.net/songyachao/article/details/51245370
今天使用4.0.3使用
Notification notification2 = new Notification(R.drawable.advise2, "通知测试", System.currentTimeMillis());notification2.setLatestEventInfo(getActivity(), "testTitle", "testContent", null);
结果androidstudio报错,setLatestEventInfo该方法找不到,经过查证官方在API Level 11中,该函数已经被替代,不推荐使用了。古在4.0.3平台也就是API Level 15中,使用Notification的setLatestEventInfo()函数时,显示setLatestEventInfo()效果。建议使用Notification.Builder来创建 notification 实例
Notification.Builder builder1 = new Notification.Builder(MainActivity.this);builder1.setSmallIcon(R.drawable.advise2); //设置图标builder1.setTicker("显示第二个通知"); builder1.setContentTitle("通知"); //设置标题builder1.setContentText("点击查看详细内容"); //消息内容builder1.setWhen(System.currentTimeMillis()); //发送时间builder1.setDefaults(Notification.DEFAULT_ALL); //设置默认的提示音,振动方式,灯光builder1.setAutoCancel(true);//打开程序后图标消失Intent intent =new Intent (MainActivity.this,Center.class);PendingIntent pendingIntent =PendingIntent.getActivity(MainActivity.this, 0, intent, 0);builder1.setContentIntent(pendingIntent);Notification notification1 = builder1.build();notificationManager.notify(124, notification1); // 通过通知管理器发送通知如果该通知只是起到 “通知”的作用,不希望用户点击后有相应的跳转,那么,intent,pendingIntent这几行代码可以不写
Notification.Builder builder = new Notification.Builder(MainActivity.this);builder.setSmallIcon(R.drawable.advise); builder.setTicker("显示第一个通知");builder.setContentTitle("第一个通知");builder.setContentText("每天进步一点点");builder.setWhen(System.currentTimeMillis()); //发送时间builder.setDefaults(Notification.DEFAULT_ALL);Notification notification = builder.build();notificationManager.notify(123, notification);
第一个具有点击提示有跳转功能,后面一个没有跳转功能,只是提示作用
以下借鉴其他博主的总结:
在不同的版本下Notification使用有一些不同,涉及到改成Builder的使用,现在网上大多数资料还是API Level 11版本前的用法介绍,如果不熟悉的话,会绕一些弯路。
现在总结如下,希望对以后使用的程序员有所帮助。
低于API Level 11版本,也就是Android 2.3.3以下的系统中,setLatestEventInfo()函数是唯一的实现方法。前面的有关属性设置这里就不再提了,网上资料很多。
Intent intent = new Intent(this,MainActivity);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
notification.setLatestEventInfo(context, title, message, pendingIntent);
manager.notify(id, notification);
高于API Level 11,低于API Level 16 (Android 4.1.2)版本的系统中,可使用Notification.Builder来构造函数。但要使用getNotification()来使notification实现。此时,前面版本在notification中设置的Flags,icon等属性都已经无效,要在builder里面设置。PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
notification.setLatestEventInfo(context, title, message, pendingIntent);
manager.notify(id, notification);
Notification.Builder builder = new Notification.Builder(context)
.setAutoCancel(true)
.setContentTitle("title")
.setContentText("describe")
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setWhen(System.currentTimeMillis())
.setOngoing(true);
notification=builder.getNotification();
高于API Level 16的版本,就可以用Builder和build()函数来配套的方便使用notification了。.setAutoCancel(true)
.setContentTitle("title")
.setContentText("describe")
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setWhen(System.currentTimeMillis())
.setOngoing(true);
notification=builder.getNotification();
Notification notification = new Notification.Builder(context)
.setAutoCancel(true)
.setContentTitle("title")
.setContentText("describe")
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setWhen(System.currentTimeMillis())
.build();
【注意点】:.setAutoCancel(true)
.setContentTitle("title")
.setContentText("describe")
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setWhen(System.currentTimeMillis())
.build();
在构造notification的时候有很多种写法,但是要注意,用
Notification notification = new Notification();
这种构建方法的时候,一定要加上notification.icon这个设置,不然,程序虽然不会报错,但是会没有效果。
0 0
- Androidn Notification的使用,解决找不到setLatestEventInfo方法
- Androidn Notification的使用,解决找不到setLatestEventInfo方法
- Androidn Notification的使用,解决找不到setLatestEventInfo方法
- Androidn Notification的使用,解决找不到setLatestEventInfo方法
- Androidn Notification的使用,解决找不到setLatestEventInfo方法
- Androidn Notification在新API中的使用(解决找不到setLatestEventInfo方法)
- Androidn Notification的使用
- notification基础及解决setLatestEventInfo找不到的情况
- notification.builder代替notification的setLatestEventInfo()方法
- 关于notification.setLatestEventInfo()的替代方法
- 关于notification.setLatestEventInfo()的替代方法
- 关于notification的使用,setLatestEventInfo方法在新SDK不能使用的解决方法
- 关于notification的使用,setLatestEventInfo方法在新SDK不能使用的解决方法
- 关于notification的使用,setLatestEventInfo方法在新SDK不能使用的解决方法
- (Notification)setLatestEventInfo方法在新SDK不能使用的解决方法及2个Error
- Notification通知setLatestEventInfo方法失效
- [Android] Notification.setLatestEventInfo()方法被移除的问题
- Android Notification的setLatestEventInfo()@deprecate
- Oracle hextoraw和rawtohex
- 分享好东西
- 关于<mvc:default-servlet-handler />
- Excel2016右键新建工作表,打开时提示“因为文件格式或文件扩展名无效。请确定文件未损坏,并且文件扩展名与文件的格式匹配。”的解决办法
- hadoop伪分布式非ha搭建
- Androidn Notification的使用,解决找不到setLatestEventInfo方法
- 2018年南京大学哲学系的考研专业课资料汇总
- Linux内核态和用户态共享内存
- select实现单选和多选
- Java连接池详解
- Mac 应用列表
- 使用log4j管理日志
- xml三种解析方式
- Error: Error setting TTL index on collection : sessions