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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章