android?

Tags: 內容, 佈局,

notification是一種出現在工作列的提示,特別是在4.0以後notification改進了不少,本文內容都是基於4.0及4.1以後總結來的。

分類

notification有以下幾種:

  1>普通notification

  

    1.內容標題

    2.大圖示

    3.內容

    4.內容附加資訊

    5.小圖示

    6.時間

  2>大布局Notification

  

  大布局notification是在android4.1以後才增加的,大布局notification與小布局notification只在‘7’部分有區別,其它部分都一致。大布局notification只有在所有notification的最上  面時才會顯示大布局,其它情況下顯示小布局。你也可以用手指將其擴充套件為大布局(前提是它是大布局)。如下圖:

    大布局notification有三種類型:如圖1為NotificationCompat.InboxStyle 型別。圖2左部為NotificationCompat.BigTextStyle。圖2右部      為:NotificationCompat.BigPictureStyle

  3>自定義佈局notification

    除了系統提供的notification,我們也可以自定義notification。如下圖所示的一個音樂播放器控制notification:

如何建立notification

    1>例項化一個NotificationCompat.Builder物件;如builder

     2>呼叫builder的相關方法對notification進行上面提到的各種設定

    3>呼叫builder.build()方法此方法返回一個notification物件。

     4>例項化一個NotificationManager物件;如:manager

     5>呼叫manager的notify方法。

  注:

  一個notification不必對上面所有的選項都進行設定,但有3項是必須的:

   小圖示, set by setSmallIcon()

  內容標題, set by setContentTitle()

  內容, set by setContentText()

示例程式碼

示例程式截圖:

0>初始化部分程式碼

View Code 1publicclass MainActivity extends Activity implements OnClickListener { 2 3privatestaticfinalint NOTIFICATION_ID_1 = 0; 4privatestaticfinalint NOTIFICATION_ID_2 = 1; 5privatestaticfinalint NOTIFICATION_ID_3 = 2; 6privatestaticfinalint NOTIFICATION_ID_4 = 3; 7privatestaticfinalint NOTIFICATION_ID_5 = 4; 8privatestaticfinalint NOTIFICATION_ID_6 = 5; 9privatestaticfinalint NOTIFICATION_ID_7 = 6;10privatestaticfinalint NOTIFICATION_ID_8 = 7;1112privatestaticint messageNum = 0;13private Context context = this;14private NotificationManager manager;15private Bitmap icon;16privatestaticfinalint[] btns = newint[] { R.id.btn1, R.id.btn2,17 R.id.btn3, R.id.btn4, R.id.btn5, R.id.btn6, R.id.btn7, R.id.btn8,18 R.id.btn9 };1920 @Override21protectedvoid onCreate(Bundle savedInstanceState) {22super.onCreate(savedInstanceState);23 setContentView(R.layout.activity_main);24 init();25 }2627privatevoid init() {28// 獲取通知服務29 manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);30// 註冊監聽器31for (int btn : btns) {32 findViewById(btn).setOnClickListener(this);33 }3435 icon = BitmapFactory.decodeResource(getResources(),36 R.drawable.ic_launcher);37 }3839 @Override40publicvoid onClick(View v) {41switch (v.getId()) {42case R.id.btn1:43 showNormal();44break;45case R.id.btn2:46 showBigView_Text();47break;48case R.id.btn3:49 showBigView_Pic();50break;51case R.id.btn4:52 showBigView_Inbox();53break;54case R.id.btn5:55 showCustomView();56break;57case R.id.btn6:58 backApp();59break;60case R.id.btn7:61 backScreen();62break;63case R.id.btn8:64 showProgressBar();65break;66case R.id.btn9:67 dismiss();68break;69default:70 Toast.makeText(context, "error", Toast.LENGTH_SHORT).show();71break;72 }73 }7475privatevoid dismiss() {76 manager.cancelAll();77 }

1>普通notification

View Code 1privatevoid showNormal() { 2 3 Notification notification = new NotificationCompat.Builder(context) 4 .setLargeIcon(icon).setSmallIcon(R.drawable.ic_launcher) 5 .setTicker("showNormal").setContentInfo("contentInfo") 6 .setContentTitle("ContentTitle").setContentText("ContentText") 7 .setNumber(++messageNum) 8 .setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL) 9 .build();10 manager.notify(NOTIFICATION_ID_1, notification);11 }

2>大布局Text型別notification

View Code 1privatevoid showBigView_Text() { 2 NotificationCompat.BigTextStyle textStyle = new BigTextStyle(); 3 textStyle 4 .setBigContentTitle("BigContentTitle") 5 .setSummaryText("SummaryText") 6 .bigText( 7 "I am Big Texttttttttttttttttttttttttttttttttttttttttttt!!!!!!!!!!!!!!!!!!!......"); 8 Notification notification = new NotificationCompat.Builder(context) 9 .setLargeIcon(icon).setSmallIcon(R.drawable.ic_launcher)10 .setTicker("showBigView_Text").setContentInfo("contentInfo")11 .setContentTitle("ContentTitle").setContentText("ContentText")12 .setStyle(textStyle)13 .setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL)14 .build();15 manager.notify(NOTIFICATION_ID_2, notification);16 }

3> 大布局Picture型別notificatio

View Code 1privatevoid showBigView_Pic() { 2 NotificationCompat.BigPictureStyle pictureStyle = new BigPictureStyle(); 3 pictureStyle.setBigContentTitle("BigContentTitle") 4 .setSummaryText("SummaryText").bigPicture(icon); 5 Notification notification = new NotificationCompat.Builder(context) 6 .setLargeIcon(icon).setSmallIcon(R.drawable.ic_launcher) 7 .setTicker("showBigView_Pic").setContentInfo("contentInfo") 8 .setContentTitle("ContentTitle").setContentText("ContentText") 9 .setStyle(pictureStyle)10 .setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL)11 .build();12 manager.notify(NOTIFICATION_ID_3, notification);13 }

4>大布局Inbox型別notification

View Code 1privatevoid showBigView_Inbox() { 2 NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(); 3 inboxStyle.setBigContentTitle("BigContentTitle").setSummaryText( 4 "SummaryText"); 5for (int i = 0; i < 5; i++) 6 inboxStyle.addLine("news:" + i); 7 Notification notification = new NotificationCompat.Builder(context) 8 .setLargeIcon(icon).setSmallIcon(R.drawable.ic_launcher) 9 .setTicker("showBigView_Inbox").setContentInfo("contentInfo")10 .setContentTitle("ContentTitle").setContentText("ContentText")11 .setStyle(inboxStyle)12 .setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL)13 .build();14 manager.notify(NOTIFICATION_ID_4, notification);15 }

5>自定義notification

效果圖:

並對中間的播放按鈕做了一個簡單的點選處理事件(點選播放後,請關閉幕簾否則可能會看不到toast提示)

View Code 1privatevoid showCustomView() { 2 RemoteViews remoteViews = new RemoteViews(getPackageName(), 3 R.layout.custom_notification); 4 Intent intent = new Intent(this, TestMusicControl.class); 5 PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, 6 intent, 0); 7 remoteViews.setOnClickPendingIntent(R.id.paly_pause_music, 8 pendingIntent); 9 NotificationCompat.Builder builder = new Builder(context);10 builder.setContent(remoteViews).setSmallIcon(R.drawable.music_icon)11 .setLargeIcon(icon).setOngoing(true)12 .setTicker("music is playing");13 manager.notify(NOTIFICATION_ID_8, builder.build());14 }

佈局檔案:

View Code 1 2 7 8 1314 1920 2627 3334 40 4142

帶進度條的notification

View Code 1privatevoid showProgressBar() { 2 3final NotificationCompat.Builder builder = new NotificationCompat.Builder( 4 context); 5 builder.setLargeIcon(icon).setSmallIcon(R.drawable.ic_launcher) 6 .setTicker("showProgressBar").setContentInfo("contentInfo") 7 .setOngoing(true).setContentTitle("ContentTitle") 8 .setContentText("ContentText"); 9new Thread(new Runnable() {1011 @Override12publicvoid run() {1314int progress = 0;1516for (progress = 0; progress < 100; progress += 5) {17//將setProgress的第三個引數設為true即可顯示為無明確進度的進度條樣式18 builder.setProgress(100, progress, false);19 manager.notify(NOTIFICATION_ID_7, builder.build());20try {21// Sleep for 5 seconds22 Thread.sleep(2 * 1000);23 } catch (InterruptedException e) {24 System.out.println("sleep failure");25 }26 }27 builder.setContentTitle("Download complete")28 .setProgress(0, 0, false).setOngoing(false);29 manager.notify(NOTIFICATION_ID_7, builder.build());3031 }32 }).start();33 }

點選事件處理

  有時候我們可能需要實現這樣的功能:當新notification出現時,我們希望點選它後可直接進入應用相應的介面中去完整檢視或處理此訊息的功能。然後,當我們點選back按鈕時返回到應用主介面而不是桌面。比如:當我們有新的簡訊來時,我們在工作列中點選它後進入讀資訊頁面,當我們讀完簡訊後,按“返回”鍵回到簡訊的主介面,而不是手機桌面。要實現這樣的功能要我們做相應的處理:

1>返回應用主介面

View Code 1privatevoid backApp() { 2 3 TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); 4// Adds the back stack 5 stackBuilder.addParentStack(OtherActivity.class); 6// Adds the Intent to the top of the stack 7 Intent resultIntent = new Intent(this, OtherActivity.class); 8 stackBuilder.addNextIntent(resultIntent); 9// Gets a PendingIntent containing the entire back stack10 PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,11 PendingIntent.FLAG_UPDATE_CURRENT);1213 Notification notification = new NotificationCompat.Builder(context)14 .setLargeIcon(icon).setSmallIcon(R.drawable.ic_launcher)15 .setTicker("backApp").setContentInfo("contentInfo")16 .setContentTitle("ContentTitle").setContentText("ContentText")17 .setContentIntent(resultPendingIntent).setAutoCancel(true)18 .setDefaults(Notification.DEFAULT_ALL).build();19 manager.notify(NOTIFICATION_ID_5, notification);20this.finish();21 }

並需要我們在配置檔案中對我們用來顯示詳細資訊的OtherActivity進行相應的配置如下:

1 5 8

2>直接返回桌面

  有些時候我們可能需要實現這樣的功能:當我們點選notification時彈出一個稍大點的視窗來顯示整個訊息,這視窗的作用就是用來顯示整個訊息內容的,和此應用內的其它Activity都沒有關係,然後當我們點選"back"後直接返回到手機桌面。要實現這樣的功能我們只需要呼叫builder的.setContentIntent方法,然後對所要跳轉到的activity在配置檔案中進行一些配置:

View Code 1privatevoid backScreen() { 2 Intent notifyIntent = new Intent(this, SpecialActivity.class); 3// Sets the Activity to start in a new, empty task 4 notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK 5 Intent.FLAG_ACTIVITY_CLEAR_TASK); 6// Creates the PendingIntent 7 PendingIntent notify_Intent = PendingIntent.getActivity(this, 0, 8 notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT); 910 Notification notification = new NotificationCompat.Builder(context)11 .setLargeIcon(icon).setSmallIcon(R.drawable.ic_launcher)12 .setTicker("backScreen").setContentInfo("contentInfo")13 .setContentTitle("ContentTitle").setContentText("ContentText")14 .setContentIntent(notify_Intent).setAutoCancel(true)15 .setDefaults(Notification.DEFAULT_ALL).build();16 manager.notify(NOTIFICATION_ID_6, notification);17this.finish();18 }

配置檔案:

View Code

相關問題答案