Android開發中setLatestEventInfo、Handler、SimpleDateFormat警告解決辦法

來源:互聯網
上載者:User

今天在做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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.