android開發之notification通知完全解析

來源:互聯網
上載者:User

android開發之notification通知完全解析

android開發之notification通知完全解析


本文主要介紹的是notification通知的使用,通過閱讀此文,你可以瞭解,在android開發中,notification通知各種使用方法。本文的notification主要是針對android4.4以下的版本。

現在,我們來看一下,如何?一個notification。估計大家現在最常用的做法是下面這種:

 Notification notification = new Notification(R.drawable.ic_launcher,  getText(R.string.app_name),System.currentTimeMillis());Intent notificationIntent = new Intent(this, MyService.class);PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);notification.setLatestEventInfo(this, getText(R.string.hello_world),getText(R.string.hello_world), pendingIntent);startForeground(1, notification);

這是在service裡面啟動一個notification,但是大家一定記住這種方法已經過時了,也就是說Google官方已經不提倡使用這種方法來啟動一個notification了。官方推薦使用V4包下NotificationCompat.Builder,這樣,我們便可以設定各種屬性。

好了,現在我們具體來看一下,Notification的具體實現。


1.建立一個簡單的notification。

Required notification contentsnotification必須要包含的內容A Notification object must contain the following:一個notification必須包含下面的屬性:A small icon, set by setSmallIcon()一個small icon,用setSmallIcon()來設定,對應於中的2號地區A title, set by setContentTitle()一個title,用setContentTitle()來設定,對應於中的1號地區Detail text, set by setContentText()詳細文本,用setContentText()來設定,對應於中的3號地區

這三個是必須設定的,至於其他的擴充則是需求而定。代碼如下:

private NotificationManager manager;NotificationCompat.Builder notifyBuilder;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);/*執行個體化NotificationManager以擷取系統服務*/manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);}/** * 顯示最簡單的通知,以下method中的三個set方法是必須設定的 * * @param view */public void simNotification(View view) {Toast.makeText(this, "hha", Toast.LENGTH_LONG).show();notifyBuilder = new NotificationCompat.Builder(this)/*設定small icon*/.setSmallIcon(R.drawable.ic_launcher)/*設定title*/.setContentTitle("通知")/*設定詳細文本*/.setContentText("Hello world");manager.notify(100, notifyBuilder.build());}

代碼裡的注釋已經很詳細了,simNotification綁定到了一個Button。這樣,我們已經實現了最簡單的notification。

2.建立一個點擊跳轉到其它Activity的Notification。無法通過左右滑動刪除

/** * 點擊跳轉到指定Activity * * @param view */public void largePicture(View view) {     /*執行個體化NotificationManager以擷取系統服務*/    manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);    //點擊的意圖ACTION是跳轉到Intent    Intent resultIntent = new Intent(this, MainActivity.class);    resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);    notifyBuilder = new NotificationCompat.Builder(this)            /*設定large icon*/            .setLargeIcon(bitmap)             /*設定small icon*/            .setSmallIcon(R.drawable.ic_launcher)            /*設定title*/            .setContentTitle("通知")            /*設定詳細文本*/            .setContentText("Hello world")             /*設定發出通知的時間為發出通知時的系統時間*/            .setWhen(System.currentTimeMillis())             /*設定發出通知時在status bar進行提醒*/            .setTicker("來自問月的祝福")            /*setOngoing(boolean)設為true,notification將無法通過左右滑動的方式清除            * 可用於添加常駐通知,必須調用cancle方法來清除            */            .setOngoing(true)             /*設定點擊後通知消失*/            .setAutoCancel(true)             /*設定通知數量的顯示類似於QQ那種,用於同志的合并*/            .setNumber(2)             /*點擊跳轉到MainActivity*/            .setContentIntent(pendingIntent);    manager.notify(121, notifyBuilder.build());}

代碼裡的注釋都很詳細,加上前面的介紹相信大家都能理解了。

3.建立一個顯示bitmap的notification,類似於螢幕的顯示效果


/** * 類似於系統的效果 * * @param view */public void comNotification(View view) {/*執行個體化NotificationManager以擷取系統服務*/manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);/*擷取bitmap*/Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);notifyBuilder = new NotificationCompat.Builder(this) /*設定small icon*/.setSmallIcon(R.drawable.ic_launcher)/*設定title*/.setContentTitle("通知")/*設定詳細文本*/.setContentText("Hello world").setWhen(System.currentTimeMillis()).setOngoing(true).setNumber(2);NotificationCompat.BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle();bigPictureStyle.bigPicture(bitmap);notifyBuilder.setStyle(bigPictureStyle);manager.notify(121, notifyBuilder.build());}

這裡唯一需要注意的是載入圖片最好不要在UI線程進行(這裡只是為了示範)。


4.建立一個類似於日曆事件的notification,並與Service進行互動。


/** * 建立一個類似於日曆事件的notification * @param view */public void add_action(View view) {myIntent = new Intent(getApplicationContext(), MyIntentService.class);myIntent.putExtra(myConstants.EXTRA_MESSAGE, " 來自問月的祝福");myIntent.setAction(myConstants.ACTION_PING);myIntent.putExtra(myConstants.EXTRA_TIMER, 1000);startService(myIntent);}
IntentService代碼如下,之後在需要時可以在此擴充IntentService,這裡注意一下IntentService類的構造器的重載,super裡面是你的路徑名。
package me.androiddemo.canglangwenyue.test;import android.app.IntentService;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Intent;import android.support.v4.app.NotificationCompat;import android.util.Log;/** * Created by canglangwenyue on 14-12-9. */public class MyIntentService extends IntentService {private NotificationManager manager;private String message;private int mMills;NotificationCompat.Builder builder;public MyIntentService() {// The super call is required. The background thread that IntentService// starts is labeled with the string argument you pass.super("me.androiddemo.canglangwenyuet.test");}@Overrideprotected void onHandleIntent(Intent intent) {message = intent.getStringExtra(myConstants.EXTRA_MESSAGE);mMills = intent.getIntExtra(myConstants.EXTRA_TIMER, myConstants.DEFAULT_TIMER_DURATION);NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);String action = intent.getAction();issueNotification(intent,message);if (action.equals(myConstants.ACTION_PING)) {issueNotification(intent,message);}else if (action.equals(myConstants.ACTION_SNOOZE)) {notificationManager.cancel(myConstants.NOTIFICATION_ID);issueNotification(intent,"");}else if (action.equals(myConstants.ACTION_DISMISS)) {notificationManager.cancel(myConstants.NOTIFICATION_ID);}}private void issueNotification(Intent intent, String msg) {manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);Intent dissmissItent = new Intent(this,MyIntentService.class);dissmissItent.setAction(myConstants.ACTION_DISMISS);PendingIntent disIntent = PendingIntent.getService(this,0,dissmissItent,0);Intent snoozeIntent = new Intent(this,MyIntentService.class);snoozeIntent.setAction(myConstants.ACTION_SNOOZE);PendingIntent snoopIntent = PendingIntent.getService(this,0,snoozeIntent,0);builder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_launcher).setContentTitle("Information").setContentText("lalallalala").setDefaults(Notification.DEFAULT_ALL).setStyle(new NotificationCompat.BigTextStyle().bigText(msg)).addAction(R.drawable.ic_launcher, "Dismiss", disIntent).addAction(R.drawable.ic_launcher,"snooze",snoopIntent);Intent resultIntent = new Intent(this,MainActivity2.class);resultIntent.putExtra(myConstants.EXTRA_MESSAGE,msg);resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);PendingIntent resultPendingIntent = PendingIntent.getActivity(this,0,resultIntent,PendingIntent.FLAG_UPDATE_CURRENT);builder.setContentIntent(resultPendingIntent);//manager.notify(myConstants.NOTIFICATION_ID,builder.build());startTimer(mMills);}private void startTimer(int mMills) {try {Thread.sleep(mMills);}catch (Exception e) {Log.d(myConstants.DEBUG_TAG, "ERROR");}issueNotification(builder);}private void issueNotification(NotificationCompat.Builder builder) {manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);manager.notify(myConstants.NOTIFICATION_ID,builder.build());}}
這裡只是實現了一個簡單的Timer。參考一下就好。

5.建立一個自訂的notification,只要通過RemoteViews來擷取你的XML布局檔案,並通過.setContent(remoteViews)就好,大家只需要對比不同之處。

/** * 自訂notification樣式 * * @param view */public void Cus_Notification(View view) {Toast.makeText(MainActivity.this, "AHa", Toast.LENGTH_LONG).show();/*執行個體化NotificationManager以擷取系統服務*/manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.customnotification);remoteViews.setImageViewResource(R.id.imageView, R.drawable.psb);remoteViews.setTextViewText(R.id.textView, "Your Haven");remoteViews.setTextViewText(R.id.textView2, "YUI");remoteViews.setTextViewText(R.id.textView3, "豆瓣-FNM -我的紅心 MHZ");remoteViews.setViewVisibility(R.id.my_large_button, View.VISIBLE);notifyBuilder = new NotificationCompat.Builder(this).setContent(remoteViews).setContentIntent(getDefalutIntent(Notification.FLAG_AUTO_CANCEL)) /*設定small icon*/.setSmallIcon(R.drawable.ic_launcher)/*設定title*/.setContentTitle("通知")/*設定詳細文本*/.setTicker("Your Haven").setContentText("Hello world").setWhen(System.currentTimeMillis()).setPriority(Notification.PRIORITY_DEFAULT)// 設定該通知優先順序.setOngoing(true);Notification noty = notifyBuilder.build();noty.contentView = remoteViews;manager.notify(313, noty);}

這裡只是添加了一個ImageView和三個TextView,大家可以進一步擴充,例如添加ImageButton,並通過BroadcastReceiver來監聽。


6.最後在寫一個帶進度條指標的notification吧,到了這裡大家應該理解什麼叫求同存異了吧,哈哈。至於其它的notification,例如FullScreen下的notification或者LockScreen的notification等,都是換湯不換藥,大家只要按需設定相應的屬性就好,具體可以查詢官方文檔。

/** * 有進度條的notification * @param view */public void Pro_Notification(View view) {manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);notifyBuilder = new NotificationCompat.Builder(this);notifyBuilder.setContentTitle("Picture Download").setContentText("Download in progress").setOngoing(true).setSmallIcon(R.drawable.ic_launcher);// Start a lengthy operation in a background threadnew Thread(new Runnable() {@Overridepublic void run() {int incr;// Do the "lengthy" operation 20 timesfor (incr = 0; incr <= 100; incr += 5) {// Sets the progress indicator to a max value, the// current completion percentage, and "determinate"// statenotifyBuilder.setProgress(100, incr, false);// Displays the progress bar for the first time.manager.notify(0, notifyBuilder.build());// Sleeps the thread, simulating an operation// that takes timetry {// Sleep for 5 secondsThread.sleep(5 * 1000);} catch (InterruptedException e) {Log.d("NOTIFICATION", "sleep failure");}}// When the loop is finished, updates the notificationnotifyBuilder.setContentText("Download complete")// Removes the progress bar.setProgress(0, 0, false);manager.notify(213, notifyBuilder.build());}}// Starts the thread by calling the run() method in its Runnable).start();}

大概解釋一下,就是在一個非同步線程裡面每隔5秒使notification的process增加5,迴圈結束後,在重新添加一個notification來顯示下載完成。至於進度還有其他方式設定,這裡就不一一介紹了,實際使用時可以傳入具體的下載進度。

好了,感覺已經寫了好多了,最後來總結一下notification的使用吧。(一)通過以下幾個步驟就可以獲得一個notification的實現:

1.擷取NotificationManager執行個體,即獲得狀態通知欄管理權;

 /*執行個體化NotificationManager以擷取系統服務*/    manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

2.執行個體化notification的constructor

notifyBuilder = new NotificationCompat.Builder(this)

3.對notifyBuilder設定各種屬性

 /**             * 前三個屬性必須設定             */            /*設定small icon*/            .setSmallIcon(R.drawable.ic_launcher)            /*設定title*/            .setContentTitle("通知")            /*設定詳細文本*/            .setContentText("Hello world")             /*設定發出通知的時間為發出通知時的系統時間*/            .setWhen(System.currentTimeMillis())             /*設定發出通知時在status bar進行提醒*/            .setTicker("來自問月的祝福")             /*設定點擊後通知消失*/            .setAutoCancel(true)                    /**                     * 設定                     notification的預設效果有以下幾種                     Notification.DEFAULT_ALL:鈴聲、閃光、震動均系統預設。                     Notification.DEFAULT_SOUND:系統預設鈴聲。                     Notification.DEFAULT_VIBRATE:系統預設震動。                     Notification.DEFAULT_LIGHTS:系統預設閃光。                     */            .setDefaults(Notification.DEFAULT_VIBRATE)             /*setOngoing(boolean)設為true,notification將無法通過左右滑動的方式清除            * 可用於添加常駐通知,必須調用cancle方法來清除            */            .setOngoing(true)             /*設定通知數量的顯示類似於QQ那種,用於同志的合并*/            .setNumber(2);

4.顯示notification

       manager.notify(100, notifyBuilder.build());

(二)一些常用的屬性函數

.setDefaults(int defaults)用於向通知添加聲音、閃燈和震動效果的最簡單、使用預設(defaults)屬性,具體在上述代碼中有寫到。

.setVibrate(long[] pattern)用於設定是否開啟震動,true為開啟

.setSound(Uri sound)用於設定通知鈴聲

.setPriority(int pri)用於設定優先權,同樣之前有詳細講解

.setOngoing(boolean ongoing)設定為true則為常駐通知欄的通知類似於墨跡天氣那種的效果。

setProgress(int max, int progress,boolean indeterminate)

用於設定進度的notification中




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.