Android Notification.setLatestEventInfo弃用和Notification.Builder用法
2017-01-22 16:15
399 查看
转自:http://blog.csdn.net/sinat_28676875/article/details/50590602
今天在学习小米便签的源码的时候,至于源码的地址,http://m.blog.csdn.net/article/details?id=50544248 ,里面有好多github的开源项目,打开项目,报错2处,一处就是便是找不到setLatestEventInfo()这个方法,我用的api6.0编译的,在这个api这个方法时被弃用的,采用Notification.Builder来代替,
原来的用法:1首先是拿到NotificationManager这个管理类实例,
2
1
2
1
3调用setLatestEvent(xx),里面有四个参数,分别是context,标题,内容,和pendingIntnet 4个参数
2
1
2
4.d调用NotificationManager的notify(xx)方法,接收两个参数一个id,可用于取消通知的时候用,另一个是Notification的实例
新的:在APi6.0时方法2,和3步骤被弃用,用过Notification.Builder来创建,直接来看官方的提供代码例程吧`
2
3
4
5
6
1
2
3
4
5
6
他是通过穿件一个Notification.Builder对象,并对这个对象进行设置,然后调用build方法来返回一个Notification对象,最后再执行上述第四个步骤,这里要注意的是,替代setLatestEventInfo(xxx)方法除了上述设置标题和内容,里面还有一个setContentIntent(pendingIntent)来设置点击通知后调用的Intent,还有其他方法详见Android官方文档http://developer.android.com/reference/android/app/Notification.Builder.html
今天在学习小米便签的源码的时候,至于源码的地址,http://m.blog.csdn.net/article/details?id=50544248 ,里面有好多github的开源项目,打开项目,报错2处,一处就是便是找不到setLatestEventInfo()这个方法,我用的api6.0编译的,在这个api这个方法时被弃用的,采用Notification.Builder来代替,
原来的用法:1首先是拿到NotificationManager这个管理类实例,
mNotifiManager = (NotificationManager) mContext .getSystemService(Context.NOTIFICATION_SERVICE);1
2
1
2
2.创建Notification实例,第一个参数是传入通知的图片,第二个是传入通知的标题,这里采用的getString的方法,顺带一提,getString方法时通过参数tickerId来调用xml文件里的常量,一般这个值为R.String.xxx
Notification notification =new Notification(R.drawable.notification, mContext.getString(tickerId), System.currentTimeMillis())1
1
3调用setLatestEvent(xx),里面有四个参数,分别是context,标题,内容,和pendingIntnet 4个参数
notification.setLatestEventInfo(mContext, mContext.getString(R.string.app_name), content, pendingIntent);1
2
1
2
4.d调用NotificationManager的notify(xx)方法,接收两个参数一个id,可用于取消通知的时候用,另一个是Notification的实例
新的:在APi6.0时方法2,和3步骤被弃用,用过Notification.Builder来创建,直接来看官方的提供代码例程吧`
Notification noti = new Notification.Builder(mContext) .setContentTitle("New mail from " + sender.toString()) .setContentText(subject) .setSmallIcon(R.drawable.new_mail) .setLargeIcon(aBitmap) .build();1
2
3
4
5
6
1
2
3
4
5
6
他是通过穿件一个Notification.Builder对象,并对这个对象进行设置,然后调用build方法来返回一个Notification对象,最后再执行上述第四个步骤,这里要注意的是,替代setLatestEventInfo(xxx)方法除了上述设置标题和内容,里面还有一个setContentIntent(pendingIntent)来设置点击通知后调用的Intent,还有其他方法详见Android官方文档http://developer.android.com/reference/android/app/Notification.Builder.html
相关文章推荐
- Android Notification.setLatestEventInfo弃用和Notification.Builder用法
- Android NotificationsetLatestEventInfo弃用和NotificationBuilder用法(1/2)
- Android Notification.setLatestEventInfo弃用和Notification.Builder用法
- Android Notification.setLatestEventInfo弃用和Notification.Builder用法
- Android Notification.setLatestEventInfo(...)
- Android Notification setLatestEventInfo方法已废弃
- Android Notification.setLatestEventInfo()警告
- Androidn Notification在新API中的使用(解决找不到setLatestEventInfo方法)
- Android学习(6)---前台服务notification.setLatestEventInfo 废弃之后的替代写法
- Androidn Notification的使用,解决找不到setLatestEventInfo方法
- Android中关于setLatestEventInfo()过时以及构建Notification的解决方法
- Android:Notification通知消息,NotificationManager,setLatestEventInfo
- Android Notification的setLatestEventInfo()@deprecate
- Androidn Notification的使用,解决找不到setLatestEventInfo方法
- Androidn Notification的使用,解决找不到setLatestEventInfo方法
- Android中关于setLatestEventInfo()过时以及构建Notification的解决方法
- Android 23 Connot resolve method updateNotification.setLatestEventInfo()解决方案
- [Android] Notification.setLatestEventInfo()方法被移除的问题
- Android中关于setLatestEventInfo()过时以及构建Notification的解决方法
- 关于Notification中setLatestEventInfo方法已弃用