Android Notification的setLatestEventInfo()@deprecate

在做Android4.4.2下的APP开发时,使用了Notification的setLatestEventInfo()方法时,Eclipse出现了叹号警告提示,setLatestEventInfo()该方法已被deprecate,不建议使用了。

/***@hide*/publicNotification(Contextcontext,inticon,CharSequencetickerText,longwhen,CharSequencecontentTitle,CharSequencecontentText,IntentcontentIntent){this.when=when;this.icon=icon;this.tickerText=tickerText;setLatestEventInfo(context,contentTitle,contentText,PendingIntent.getActivity(context,0,contentIntent,0));}

这个构造函数被hide,setLatestEventInfo方法也被deprecate,不建议使用,提示使用Notification.Builder。

在不同的版本下Notification使用有一些不同,涉及到改成Builder的使用,现在网上大多数资料还是APILevel11版本前的用法介绍,如果不熟悉的话,会绕一些弯路。现在总结如下,希望对看到这篇文章的有所帮助。低于APILevel11版本(即Android2.3.3之前的系统)中,setLatestEventInfo()函数是唯一的实现方法。前面的有关属性设置这里就不再提了,网上资料很多。

Intentintent=newIntent(this,MainActivity);PendingIntentpendingIntent=PendingIntent.getActivity(context,0,intent,PendingIntent.FLAG_ONE_SHOT);notification.setLatestEventInfo(context,title,message,pendingIntent);manager.notify(id,notification);

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

Notification.Builderbuilder=newNotification.Builder(context).setAutoCancel(true).setContentTitle("title").setContentText("describe").setContentIntent(pendingIntent).setSmallIcon(R.drawable.ic_launcher).setWhen(System.currentTimeMillis()).setOngoing(true);notification=builder.getNotification();

高于APILevel16的版本,就可以用Builder和build()函数使用notification了。

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

注意:

在构造notification的时候有很多种写法,但是要注意,,用Notificationnotification=newNotification();这种构建方法的时候,一定要加上notification.icon这个设置,不然,程序虽不会报错,但是会没有效果。

参考:

要铭记在心;每天都是一年中最美好的日子

Android Notification的setLatestEventInfo()@deprecate

相关文章:

你感兴趣的文章:

标签云: