正 文:

今天飘易在做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中,该函数已经被替代,不推荐使用了。

在不同的版本下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

【参考】
1、Notification在不同版本下的使用贴士
2、解决Android中Handler警告、SimpleDateFormat警告、"String".toUpperCase()警告

Android下setLatestEventInfo警告、Handler警告、SimpleDateFormat警告的更多相关文章

  1. android 下的 handler Message

    研究了下android下的 handler  message 实现原理: new handler() 的时候  从ThreadLocal里面 获取当前线程下的 Looper实例下的 MessageQu ...

  2. Android消息机制探索(Handler,Looper,Message,MessageQueue)

    概览 Android消息机制是Android操作系统中比较重要的一块.具体使用方法在这里不再阐述,可以参考Android的官方开发文档. 消息机制的主要用途有两方面: 1.线程之间的通信.比如在子线程 ...

  3. [android] android下文件访问的权限

    /**************2016年5月4日 更新**************************/ 知乎:android编程中写文件(例如a.txt)后存在手机哪个位置啊? 用FileOut ...

  4. Android多线程机制和Handler的使用

    参考教程:iMooc关于Handler,http://www.imooc.com/learn/267 参考资料:Google提供Android文档Communicating with the UI T ...

  5. 【Android 开发】: Android 消息处理机制之一: Handler 与 Message

    最近几讲内容,我们学习了Android中关于多线程的一些知识,上一讲我们讲解了异步任务 AsyncTask 的操作,Android中还提供了其他的线程操作,如Handler Message Messa ...

  6. android 多线程Thread,Runnable,Handler,AsyncTask

    先看两个链接: 1.http://www.2cto.com/kf/201404/290494.html 2. 链接1: android 的多线程实际上就是java的多线程.android的UI线程又称 ...

  7. Android下实现数据绑定功能

    在编写Android应用的时候经常需要做的事情就是对View的数据进行设置,在Android下设置控件相对.net来说是件麻烦的事情,首先根据ID从view把控件找出来然后才能设置相应属性值:如果数据 ...

  8. Android下利用zbar类库实现扫一扫

    程序源代码及可执行文件下载地址:http://files.cnblogs.com/rainboy2010/zbardemo.zip Android下常用的条码扫描类库有zxing和zbar,比较了一下 ...

  9. Onvif协议及其在Android下的实现

    好久没有写博客,今天将前段时间做的Onvif协议在Android上的实现分享给大家. 首先,我们先来了解一下什么是Onvif协议:ONVIF 协议是由Open Network Video Interf ...

随机推荐

  1. 插入标识列identity_insert

    插入标识列identity_insert 在进行数据插入时,如果插入列名包括标识列,常常会遇到以下3种提示: 一.“当 IDENTITY_INSERT 设置为 OFF 时,不能向表 'xxxxxxxx ...

  2. MySQL学习笔记:新增一列

    1.在一个已建好的表中,最后一列位置添加一列,可使用: ALTER TABLE aa_numbers_small ADD COLUMN date_time DATE NOT NULL; 2.添加一列到 ...

  3. Python 安装 pytesser 处理验证码出现的问题

    今天这个问题困扰了我好久,开始直接用 pip install pytesseract 安装了 pytesseract 然后出现了如下错误 Traceback (most recent call las ...

  4. C#基础系列 - 抽象类及其方法的学习

    在C#中使用关键字 abstract 来定义抽象类和抽象方法. 不能初始化的类被叫做抽象类,它们只提供部分实现,但是另一个类可以继承它并且能创建它们的实例. "一个包含一个或多个纯虚函数的类 ...

  5. 二、 sql*plus常用命令

    一.sys用户和system用户Oracle安装会自动的生成sys用户和system用户(1).sys用户是超级用户,具有最高权限,具有sysdba角色,有create database的权限,该用户 ...

  6. 8-16 不无聊序列 non-boring sequences uva1608

    题意: 如果一个序列的任意连续子序列中至少有一个只出现一次的元素  则称这个序列是 不无聊序列  输入一个n个元素的序列a   判断是不是不无聊序列 一开始想当然  以为只要 2位的子序列都满足即可 ...

  7. Python 爬虫个人记录(一)豆瓣电影250

    一.爬虫环境 Python3.6 scrapy1.4 火狐浏览器 qq浏览器 二.scrapy shell 测试并获取 xpath 1.进入scrapy shell 2 .获取html fetch(' ...

  8. Wannafly挑战赛9 D - 造一造

    链接:https://www.nowcoder.com/acm/contest/71/D来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言52428 ...

  9. 通过GeneXus如何快速构建微服务架构

    概览 “微服务”是一个非常广泛的话题,在过去几年里,市面上存在着各种不同的定义. 虽然对这种架构方式没有一个非常精确的定义,但仍然有一些概念具有代表性. 微服务有着许多围绕业务能力.自动化部署.终端智 ...

  10. Sublime Text 下的Install Package安装方法

    废话不多说.... 如果你是Sublime Text3用户,按下Ctrl+~呼出控制台,输入以下代码 import urllib.request,os,hashlib; h = '7183a2d3e9 ...