正 文:
今天飘易在做Android 4.4.2下的APP开发时,使用了Notification下的setLatestEventInfo()方法时,Eclipse却提示:“
不建议使用类型 Notification 的方法 setLatestEventInfo(Context, CharSequence, CharSequence, PendingIntent)
”!
/**
* @hide
*/
public Notification(Context context, int icon, CharSequence tickerText, long when,
CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
{
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的使用,现在网上大多数资料还是API Level 11版本前的用法介绍,如果不熟悉的话,会绕一些弯路。
现在总结如下,希望对以后使用的程序员有所帮助。
低于API Level 11版本,也就是Android 2.3.3以下的系统中,setLatestEventInfo()函数是唯一的实现方法。前面的有关属性设置这里就不再提了,网上资料很多。
在构造notification的时候有很多种写法,但是要注意,用
Notification notification = new Notification();
这种构建方法的时候,一定要加上notification.icon这个设置,不然,程序虽然不会报错,但是会没有效果。
1:Handler
// This Handler class should be static or leaks might occur: IncomingHandler
@SuppressLint("HandlerLeak")
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
};
解决方法:
@Override
public boolean handleMessage(Message msg) {
return false;
}
});
2:SimpleDateFormat
// getTimeInstance(), or use new SimpleDateFormat(String template, Locale
// locale) with for example Locale.US for ASCII dates.
@SuppressLint("SimpleDateFormat")
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
"yyyy-MM-ddHH:mm:ss");
解决方法:
"yyyy年MM月dd日HH时mm分", Locale.getDefault());
@SuppressLint("UseSparseArrays")
public static Map CMD_MAP = new HashMap();
boolean b = "String".toUpperCase().equals("STRING");
解决方法:
boolean b = "String".equalsIgnoreCase("STRING");
警告原因:Implicitly using the default locale is a common source of bugs: Use toUpperCase(Locale) instead
【参考】
1、 Notification在不同版本下的使用贴士
2、 解决Android中Handler警告、SimpleDateFormat警告、"String".toUpperCase()警告
id="cproIframe_u182710_1" width="336" height="280" src="http://pos.baidu.com/acom?adn=4&at=231&aurl=&cad=1&ccd=24&cec=GBK&cfv=0&ch=0&col=zh-CN&conBW=0&conOP=1&cpa=1&dai=1&dis=0<r=https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DBdlhl8UcevLp463RBBX0xBfu1I5KcuIWVdfx3VnoJ8AtnVjMN9AHBFa_qFhFmX-5ocxiGltBDfawCFOM7kpuipqS70DG5t3pbPOpNjwTz0M-sa90Nd3gVNo0PkINVVyM4pZco3Opia7iG6qmjS7bmK%26wd%3D%26eqid%3Da9a2321c00001f07000000065663bbc1<u=http%3A%2F%2Fwww.piaoyi.org%2Fmobile-app%2FAndroid-setLatestEventInfo-Handler-SimpleDateFormat.html&lu_161=10&lunum=6&n=flymorn_cpr&pcs=1349x643&pis=10000x10000&ps=3760x0&psr=1366x768&pss=1349x3760&qn=5b020a31f5fe3290&rad=&rsi0=336&rsi1=280&rsi5=2&rss0=%23FFFFFF&rss1=%23FFFFFF&rss2=%230000ff&rss3=%23444444&rss4=%23008000&rss5=&rss6=%23e10900&rss7=&scale=&skin=tabcloud_skin_3&stid=5&td_id=182710&titFF=%E5%AE%8B%E4%BD%93&titFS=12&titTA=left&tn=text_default_336_280&tpr=1449377252036&ts=1&version=2.0&xuanting=0&dtm=BAIDU_DUP2_SETJSONADSLOT&dc=2&di=u182710&ti=Android%E4%B8%8BsetLatestEventInfo%E8%AD%A6%E5%91%8A%E3%80%81Handler%E8%AD%A6%E5%91%8A%E3%80%81SimpleDateFormat%E8%AD%A6%E5%91%8A&tt=1449377252023.15.95.102" align="center,center" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" allowtransparency="true">
|
这是为什么呢?查询后得知:setLatestEventInfo该方法已被deprecate,不建议使用了。
/**
* @hide
*/
public Notification(Context context, int icon, CharSequence tickerText, long when,
CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
{
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平台也就是API Level 15中,使用Notification的setLatestEventInfo()函数时,也会显示成 setLatestEventInfo() 效果,查看文档发现,在API Level 11中,该函数已经被替代,不推荐使用了。
在不同的版本下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);
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里面设置。
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();
.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();
.setAutoCancel(true)
.setContentTitle("title")
.setContentText("describe")
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setWhen(System.currentTimeMillis())
.build();
【 注意点 】:
在构造notification的时候有很多种写法,但是要注意,用
Notification notification = new Notification();
这种构建方法的时候,一定要加上notification.icon这个设置,不然,程序虽然不会报错,但是会没有效果。
另外,补充下在实际android开发中遇到的一些警告以及解决方法:
1:Handler
// This Handler class should be static or leaks might occur: IncomingHandler
@SuppressLint("HandlerLeak")
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
};
};
解决方法:
private Handler mHandler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
return false;
}
});
2:SimpleDateFormat
// To get local formatting use getDateInstance(), getDateTimeInstance(), or
// getTimeInstance(), or use new SimpleDateFormat(String template, Locale
// locale) with for example Locale.US for ASCII dates.
@SuppressLint("SimpleDateFormat")
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
"yyyy-MM-ddHH:mm:ss");
解决方法:
SimpleDateFormat newSimpleDateFormat = new SimpleDateFormat(
"yyyy年MM月dd日HH时mm分", Locale.getDefault());
3:new HashMap()
@SuppressLint("UseSparseArrays")
public static Map CMD_MAP = new HashMap();
警告原因:Use new SparseArray(...) instead for better performance
4:"String".toUpperCase(); "String".toLowerCase();
@SuppressLint("DefaultLocale")
boolean b = "String".toUpperCase().equals("STRING");
解决方法:
boolean b = "String".equalsIgnoreCase("STRING");
警告原因:Implicitly using the default locale is a common source of bugs: Use toUpperCase(Locale) instead
【参考】
1、 Notification在不同版本下的使用贴士
2、 解决Android中Handler警告、SimpleDateFormat警告、"String".toUpperCase()警告