Android下setLatestEventInfo警告:

正 文: 在做Android4.4.2下的APP开发时,使用了Notification下的setLatestEventInfo()方法时,Eclipse却提示:“ 不建议使用类型Notification的方法setLatestEventInfo(Context,CharSequence,CharSequence,PendingIntent) ”!

这是为什么呢?查询后得知:setLatestEventInfo该方法已被deprecate,不建议使用了。

/**
*@hid
*/
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即可。

在4.0.3平台也就是APILevel15中,使用Notification的setLatestEventInfo()函数时,也会显示成 setLatestEventInfo() 效果,查看文档发现,在APILevel11中,该函数已经被替代,不推荐使用了。

在不同的版本下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开发中遇到的一些警告以及解决方法:
1:Handler
//ThisHandlerclassshouldbestaticorleaksmightoccur:IncomingHandler
@SuppressLint("HandlerLeak")
privateHandlermHandler=newHandler(){
@Override
publicvoidhandleMessage(Messagemsg){

};
};

解决方法:

privateHandlermHandler=newHandler(newHandler.Callback(){
@Override
publicbooleanhandleMessage(Messagemsg){
returnfalse;
}
});


2:SimpleDateFormat

//TogetlocalformattingusegetDateInstance(),getDateTimeInstance(),or
//getTimeInstance(),orusenewSimpleDateFormat(Stringtemplate,Locale
//locale)withforexampleLocale.USforASCIIdates.
@SuppressLint("SimpleDateFormat")
SimpleDateFormatsimpleDateFormat=newSimpleDateFormat(
"yyyy-MM-ddHH:mm:ss");
解决方法:

SimpleDateFormatnewSimpleDateFormat=newSimpleDateFormat(
"yyyy年MM月dd日HH时mm分",Locale.getDefault());

3:newHashMap()
@SuppressLint("UseSparseArrays")
publicstaticMapCMD_MAP=newHashMap();

警告原因:UsenewSparseArray(...)insteadforbetterperformance

4:"String".toUpperCase();"String".toLowerCase();

@SuppressLint("DefaultLocale")
booleanb="String".toUpperCase().equals("STRING");
解决方法:
booleanb="String".equalsIgnoreCase("STRING");
警告原因:Implicitlyusingthedefaultlocaleisacommonsourceofbugs:UsetoUpperCase(Locale)instead

更多相关文章

  1. C基础—函数指针,联合体,枚举,结构体和结构体指针
  2. 《Android 获取当前app的版本号和版本名称》
  3. Android 中文设置成粗体的方法
  4. Bitmap和Drawable相互转换方法
  5. android onSaveInstanceState方法 横坚屏切换
  6. Android P 以及之后版本不支持同时从多个进程使用具有相同数据目
  7. Android Studio 编译提示 aapt.exe finished with non-zero exit

随机推荐

  1. android环境搭建
  2. Android简明开发教程六:用户界面设计
  3. android:layout_gravity 和 android:grav
  4. 安卓开发环境的搭建(Eclipse+ADT+SDK+JDK
  5. Android(安卓)模拟器安装apk文件
  6. 达牛:android的基础学习—如何做好环境搭
  7. Android应用安全风险与防范
  8. Android(安卓)Shell 常用命令
  9. android GrantPermissionsActivity 详解
  10. Android初始化语言 (init.*.rc、init.con