1. 程式人生 > >Androidn Notification的使用,解決找不到setLatestEventInfo方法

Androidn Notification的使用,解決找不到setLatestEventInfo方法

今天使用4.0.3使用

Notification notification2 = new Notification(R.drawable.advise2,
"通知測試", System.currentTimeMillis());
notification2.setLatestEventInfo(getActivity(), "testTitle", "testContent", null);

結果androidstudio報錯,setLatestEventInfo該方法找不到,經過查證官方在API Level 11中,該函式已經被替代,不推薦使用了。古在4.0.3平臺也就是API Level 15中,使用Notification的setLatestEventInfo()函式時,顯示

setLatestEventInfo()效果。建議使用Notification.Builder來建立 notification 例項

Notification.Builder builder1 = new Notification.Builder(MainActivity.this);
builder1.setSmallIcon(R.drawable.advise2); //設定圖示
builder1.setTicker("顯示第二個通知"); 
builder1.setContentTitle("通知"); //設定標題
builder1.setContentText("點選檢視詳細內容"); //訊息內容
builder1.setWhen(System.currentTimeMillis()); //傳送時間 builder1.setDefaults(Notification.DEFAULT_ALL); //設定預設的提示音,振動方式,燈光 builder1.setAutoCancel(true);//開啟程式後圖標消失 Intent intent =new Intent (MainActivity.this,Center.class); PendingIntent pendingIntent =PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
builder1.setContentIntent(pendingIntent); Notification notification1 = builder1.build(); notificationManager.notify(124, notification1); // 通過通知管理器傳送通知
如果該通知只是起到 “通知”的作用,不希望使用者點選後有相應的跳轉,那麼,intent,pendingIntent這幾行程式碼可以不寫
Notification.Builder builder = new Notification.Builder(MainActivity.this);
builder.setSmallIcon(R.drawable.advise);
builder.setTicker("顯示第一個通知");
builder.setContentTitle("第一個通知");
builder.setContentText("每天進步一點點");
builder.setWhen(System.currentTimeMillis()); //傳送時間
builder.setDefaults(Notification.DEFAULT_ALL);
Notification notification = builder.build();
notificationManager.notify(123, notification);

第一個具有點選提示有跳轉功能,後面一個沒有跳轉功能,只是提示作用

以下借鑑其他博主的總結:

在不同的版本下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這個設定,不然,程式雖然不會報錯,但是會沒有效果。