Notification用法,注意API版本,API大于11的setLatestEventInfo()方法已经不适用
来源:互联网 发布:淘宝大魔王是真是假 编辑:程序博客网 时间:2024/06/06 12:56
Android下setLatestEventInfo警告:
正 文: 在做Android 4.4.2下的APP开发时,使用了Notification下的setLatestEventInfo()方法时,Eclipse却提示:“ 不建议使用类型 Notification 的方法 setLatestEventInfo(Context, CharSequence, CharSequence, PendingIntent)”! 这是为什么呢?查询后得知:setLatestEventInfo该方法已被deprecate,不建议使用了。
/**
* @hid
*/
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()函数是唯一的实现方法。前面的有关属性设置这里就不再提了,网上资料很多。
在构造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 performance4:"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
/**
* @hid
*/
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);
高于API Level 11,低于API Level 16 (Android 4.1.2)版本的系统中,可使用Notification.Builder来构造函数。但要使用getNotification()来使notification实现。此时,前面版本在notification中设置的Flags,icon等属性都已经无效,要在builder里面设置。PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
notification.setLatestEventInfo(context, title, message, pendingIntent);
manager.notify(id, notification);
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了。.setAutoCancel(true)
.setContentTitle("title")
.setContentText("describe")
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setWhen(System.currentTimeMillis())
.setOngoing(true);
notification=builder.getNotification();
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 performance4:"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 0
- Notification用法,注意API版本,API大于11的setLatestEventInfo()方法已经不适用
- android错误---Notification用法,注意API版本
- notification.builder代替notification的setLatestEventInfo()方法
- Androidn Notification在新API中的使用(解决找不到setLatestEventInfo方法)
- 关于notification.setLatestEventInfo()的替代方法
- 关于notification.setLatestEventInfo()的替代方法
- WIN32 API 打开大于10序号的串口注意写法
- win32 API创建tooltip的版本不匹配问题解决方法
- Notification通知setLatestEventInfo方法失效
- Androidn Notification的使用,解决找不到setLatestEventInfo方法
- Androidn Notification的使用,解决找不到setLatestEventInfo方法
- Androidn Notification的使用,解决找不到setLatestEventInfo方法
- Androidn Notification的使用,解决找不到setLatestEventInfo方法
- [Android] Notification.setLatestEventInfo()方法被移除的问题
- Androidn Notification的使用,解决找不到setLatestEventInfo方法
- Android Notification的setLatestEventInfo()@deprecate
- 获取MAC地址多种方法,操作系统的 API 不一定适用。
- windows API获取物理内存大于4G的方法
- POJ - 2255 二叉树遍历
- UVA - 11054 Wine trading in Gergovia
- SDUT3334数据结构实验之栈七:出栈序列判定
- 线上遇到插入重复数据(重复提交)
- hdu1269 迷宫城堡(tarjan入门)
- Notification用法,注意API版本,API大于11的setLatestEventInfo()方法已经不适用
- Count The colors_zoj1610_线段树
- 【Java基础】面向对象特性
- matlab 函数
- php curl函数免ssl校验
- 初始博弈【二】Nim(hdu 1849,poj1704)
- HDU 2457DNA repair
- 关联规则挖掘 ---- Apriori算法与CBA算法
- ACM程序设计选修课——Problem E:(ds:图)公路村村通(优先队列或sort+克鲁斯卡尔+并查集优化)