|
在做Android 4.4.2下的APP開發時,使用了Notification的setLatestEventInfo()方法時,Eclipse出現了嘆號警告提示,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();
複製代碼
1.如果該通知只是起到“通知”的作用,不希望用戶點擊後有相應的跳轉,那麼,intent,pendingIntent這幾行代碼可以不寫,可以創建延時操作
- Notification noti = new Notification.Builder(context)
- .setContentTitle(label +updatayes)
- .setContentText(pkgName+updatayes)
- .setSmallIcon(R.drawable.ic_launcher)
- //.setLargeIcon(R.drawable.ic_launcher)
- .build();
複製代碼
注意:
在構造notification的時候有很多種寫法,但是要注意,用Notification notification = new Notification(); 這種構建方法的時候,一定要加上notification.icon這個設置,不然,程序雖不會報錯,但是會沒有效果。
參考:
|
|