Android开发中setLatestEventInfo、Handler、SimpleDateFormat警告解决办法

今天在做Android 4.4.2下的APP开发时,使用了Notification下的setLatestEventInfo()方法时,Eclipse却提示:“ 不建议使用类型 Notification 的方法setLatestEventInfo(Context, CharSequence, CharSequence, PendingIntent)”!

这是为什么呢?查询后得知: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中,该函数已经被替代,不推荐使用了。
 
Android下setLatestEventInfo警告、Handler警告、SimpleDateFormat警告

在不同的版本下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里面设置。

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();  

【注意点】:

在构造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

时间: 2024-08-03 01:47:01

Android开发中setLatestEventInfo、Handler、SimpleDateFormat警告解决办法的相关文章

5个Android开发中比较常见的内存泄漏问题及解决办法

android中一个对象已经不需要了,但是其他对象还持有他的引用,导致他不能回收,导致这个对象暂存在内存中,这样内存泄漏就出现了. 内存泄漏出现多了,会是应用占用过多的没存,当占用的内存超过了系统分配的内存容量,就会出现内存溢出了导致应用Crash. 了解了内存泄漏的原因及影响后,我们需要做的就是掌握常见的内存泄漏,并在以后的Android程序开发中,尽量避免它.下面搜罗了5个Android开发中比较常见的内存泄漏问题及解决办法,分享给大家,一起来看看吧. 一.单例造成的内存泄漏 android

在Android开发中替换资源图片不起作用的解决方法_Android

现象 在android开发中,经常会需要替换res\drawable中的图片,打开res\layout下的文件预览布局页面发现图片已经被替换,但在模拟器或者真实机器上运行时发现该图片并没有被替换,还是使用的是原来的资源图片. 原因 在开发过程中,由于使用模拟器测试了程序,在首次运行后会将res文件夹下的图片资源文件(如drawable-hdpi.drawable-ldpi和drawable-mdpi)拷贝到bin文件夹下.在替换资源图片后,eclipse并不清楚是否有图片改变,所以会使用原来bi

聊天-Android开发中遇到了一个问题,求大神解决下。

问题描述 Android开发中遇到了一个问题,求大神解决下. 我现在将别人写好的聊天软件,根据我自己的需要,剔除了很多不用的activity和功能,然后当成Library.我遇到的问题是,我现在只有进入到这个聊天界面才能够收到信息和来信息的提示,如果我不进入,我就没法得到来消息的提示,请问我该怎么解决呢? 解决方案 就想QQ的离线消息一样?后台起一个Service接收信息,有信息了就在消息通知栏里放一个消息. 解决方案二: 这个问题可能涉及到通知提醒的内容,建议你先看一下android是如何实现

android开发中webview保存cookie问题的解决

最近老是发现在IE里会有Cookie的问题,如IE下面无法登出,或无法登录,或者登录后信息却无法取到,而Firefox下面一直是通过的,都试过好多次了,今天终于找回的主要的原因: Cookie的问题: 首先看一下我的Cookie存取代码 (这个Cookie操作支持二级域名访问) #region 存取Cookie /// 〈summary〉 /// 存Cookie /// Json Lee 2007-09-24 /// 〈/summary〉 /// 〈param name="strName&quo

Android开发中如何解决加载大图片时内存溢出的问题

Android开发中如何解决加载大图片时内存溢出的问题    在Android开发过程中,我们经常会遇到加载的图片过大导致内存溢出的问题,其实类似这样的问题已经屡见不鲜了,下面将一些好的解决方案分享给大家.   尽量不要使用setImageBitmap或setImageResource或BitmapFactory.decodeResource来设置一张大图,因为这些函数在完成decode后,最终都是通过java层的createBitmap来完成的,需要消耗更多内存. 因此,改用先通过Bitmap

Android开发中的9个常见错误和解决方法_Android

经过各种各样的整理,以及和热心网友讨论,终于整理出了九种android开发中最常见的问题和解决方案再次跟大家分享下!!有用的话请顶顶帖子,共同进步.好了不多说了,下面是详解! 1. 如果你的项目的R文件不见的话,可以试下改版本号在保存,R文件不见一般都是布局文本出错导致. 2. 布局文件不可以有大写字母 3. 抛出如下错误WARNING: Application does not specify an API level requirement!, 是由于没有指定users sdk的缘故,修改A

Android开发中Activity之间切换出现短暂黑屏的解决方法_Android

本文实例讲述了Android开发中Activity之间切换出现短暂黑屏的解决方法.分享给大家供大家参考,具体如下: 在默认情况下,Android应用程序启动时,会有一个黑屏的时期,原因是,首个activity会加载一些数据,比如初始化列表数据.向服务器发送请求获取数据等等.同样,使用startActivity(inte -- 在默认情况下,Android应用程序启动时,会有一个黑屏的时期,原因是,首个activity会加载一些数据,比如初 始化列表数据.向服务器发送请求获取数据等等.同样,使用s

Android开发中听筒无法播放音乐的解决方法_Android

本文实例讲述了Android开发中听筒无法播放音乐的解决方法.分享给大家供大家参考,具体如下: 这个问题让我蛋疼了,既然百度也木有资料. 耗时的主要原因是因为权限不足时,而没有终止程序,只用了一小行日志提醒,没有看到 用听筒播放很简单 AudioManager.setMode(AudioManager.MODE_IN_CALL) //设定为通话中即可 还是这一句代码的事,不过记得要加上权限 Android.permission.MODIFY_AUDIO_SETTINGS 不然会像我一样蛋疼半天

Android开发中听筒无法播放音乐的解决方法

本文实例讲述了Android开发中听筒无法播放音乐的解决方法.分享给大家供大家参考,具体如下: 这个问题让我蛋疼了,既然百度也木有资料. 耗时的主要原因是因为权限不足时,而没有终止程序,只用了一小行日志提醒,没有看到 用听筒播放很简单 AudioManager.setMode(AudioManager.MODE_IN_CALL) //设定为通话中即可 还是这一句代码的事,不过记得要加上权限 Android.permission.MODIFY_AUDIO_SETTINGS 不然会像我一样蛋疼半天