Notifiacation/setLatestEventInfo deprecated

来源:互联网 发布:python 读写文本文件 编辑:程序博客网 时间:2024/06/03 22:40

在使用Notification的时候,发现Notification的方法deprecated,那用什么方法代替呢?

Notification notification = new Notification(iconId, title, System.currentTimeMillis());                   

 notification.setLatestEventInfo(context, ", title, contentItent);

 高于API Level 11,低于API Level 16 (Android 4.1.2)版本的系统中,可使用Notification.Builder来构造函数。但要使用getNotification()来使notification实现。此时,前面版本在notification中设置的Flags,icon等属性都已经无效,要在builder里面设置。

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了。

Notification notification = new Notification.Builder(context)             .setAutoCancel(true)             .setContentTitle("title")             .setContentText("describe")             .setContentIntent(pendingIntent)             .setSmallIcon(R.drawable.ic_launcher)             .setWhen(System.currentTimeMillis())             .build();   

参考链接:http://www.piaoyi.org/mobile-app/Android-setLatestEventInfo-Handler-SimpleDateFormat.html


0 0