TShopping

 找回密碼
 註冊
搜索
查看: 4748|回復: 0
打印 上一主題 下一主題

[教學] Android Notification的setLatestEventInfo()怎麼代替

[複製鏈接]
跳轉到指定樓層
1#
發表於 2016-7-13 08:51:59 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
 
Push to Facebook
在做Android 4.4.2下的APP開發時,使用了Notification的setLatestEventInfo()方法時,Eclipse出現了嘆號警告提示,setLatestEventInfo()該方法已被deprecate,不建議使用了。
  1. /**
  2.      * @hide
  3.      */
  4.     public Notification(Context context, int icon, CharSequence tickerText, long when,
  5.             CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
  6.     {
  7.         this.when = when;
  8.         this.icon = icon;
  9.         this.tickerText = tickerText;
  10.         setLatestEventInfo(context, contentTitle, contentText,
  11.                 PendingIntent.getActivity(context, 0, contentIntent, 0));
  12.     }
複製代碼


    這個構造函數被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()函數是唯一的實現方法。前面的有關屬性設置這裡就不再提了,網上資料很多。

  1. Intent  intent = new Intent(this,MainActivity);  
  2. PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);  
  3. notification.setLatestEventInfo(context, title, message, pendingIntent);         
  4. manager.notify(id, notification);  
複製代碼

    高於API Level 11,低於API Level 16 (Android 4.1.2)版本的系統中,可使用Notification.Builder來構造函數。但要使用getNotification()來使notification實現。此時,前面版本在notification中設置的Flags,icon等屬性都已經無效,要在builder裡面設置。
  1. Notification.Builder builder = new Notification.Builder(context)  
  2.             .setAutoCancel(true)  
  3.             .setContentTitle("title")  
  4.             .setContentText("describe")  
  5.             .setContentIntent(pendingIntent)  
  6.             .setSmallIcon(R.drawable.ic_launcher)  
  7.             .setWhen(System.currentTimeMillis())  
  8.             .setOngoing(true);  
  9. notification=builder.getNotification();  
複製代碼

    高於API Level 16的版本,就可以用Builder和build()函數使用notification了。
  1. Notification notification = new Notification.Builder(context)   
  2.          .setAutoCancel(true)   
  3.          .setContentTitle("title")   
  4.          .setContentText("describe")   
  5.          .setContentIntent(pendingIntent)   
  6.          .setSmallIcon(R.drawable.ic_launcher)   
  7.          .setWhen(System.currentTimeMillis())   
  8.          .build();   
複製代碼

1.如果該通知只是起到“通知”的作用,不希望用戶點擊後有相應的跳轉,那麼,intent,pendingIntent這幾行代碼可以不寫,可以創建延時操作
  1. Notification noti = new Notification.Builder(context)
  2.                             .setContentTitle(label +updatayes)
  3.                             .setContentText(pkgName+updatayes)
  4.                             .setSmallIcon(R.drawable.ic_launcher)
  5.                             //.setLargeIcon(R.drawable.ic_launcher)
  6.                             .build();
複製代碼


注意:

    在構造notification的時候有很多種寫法,但是要注意,用Notification notification = new Notification(); 這種構建方法的時候,一定要加上notification.icon這個設置,不然,程序雖不會報錯,但是會沒有效果。


參考:



 

臉書網友討論
*滑块验证:
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2024-4-26 19:44 , Processed in 0.063786 second(s), 22 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表