初識Notification,notification

來源:互聯網
上載者:User

初識Notification,notification
通知(Notification)是 Android 系統中比較有特色的一個功能,當某個應用程式希望向使用者發出一些提示資訊,而該應用程式又不在前台運行時,就可以藉助通知來實現。發出一條通知後,手機最上方的狀態列中會顯示一個通知的表徵圖,下拉狀態列後可以看到通知的詳細內容。 通知的基本用法 既可以在活動裡建立,也可以在廣播接收器裡建立,相比於廣播接收器和服務,在活動裡建立通知的情境還是比較少的,因為一般只有當程式進入到背景時候我們才需要使用通知。論是在哪裡建立通知,整體的步驟都是相同的。

  • 首先需要一個 NotificationManager 來對通知進行管理,可以調用 Context 的getSystemService()方法擷取到。getSystemService()方法接收一個字串參數用於確定擷取系統的哪個服務,這裡我們傳入 Context.NOTIFICATION_SERVICE 即可。因此,擷取NotificationManager 的執行個體就可以寫成:
                NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
  • 接下來需要建立一個 Notification 對象,這個對象用於儲存通知所需的各種資訊,我們可以使用它的有參建構函式來進行建立。Notification 的有參建構函式接收三個參數,第一個參數用於指定通知的表徵圖,比如項目的 res/drawable 目錄下有一張 icon.png 圖片,那麼這裡就可以傳入 R.drawable.icon。第二個參數用於指定通知的 ticker 內容,當通知剛被建立的時候,它會在系統的狀態列一閃而過,屬於一種瞬時的提示資訊。第三個參數用於指定通知被建立的時間,以毫秒為單位,當下拉系統狀態列時,這裡指定的時間會顯示在相應的通知上。因此,建立一個 Notification 對象就可以寫成:    
                Notification notification = new Notification(R.drawable.icon, "This is ticker text",System.currentTimeMillis());
  • 建立好了 Notification 對象後,我們還需要對通知的布局進行設定,這裡只需要調用Notification 的 setLatestEventInfo()方法就可以給通知設定一個標準的布局。這個方法接收四個參數,第一個參數是 Context,這個沒什麼好解釋的。第二個參數用於指定通知的標題內容,下拉系統狀態列就可以看到這部分內容。第三個參數用於指定通知的本文內容,同樣下拉系統狀態列就可以看到這部分內容。第四個參數我們暫時還用不到,可以先傳入 null。因此,對通知的布局進行設定就可以寫成:
                notification.setLatestEventInfo(context, "This is content title", "This iscontent text", null);
  • 以上工作都完成之後,只需要調用 NotificationManager 的 notify()方法就可以讓通知顯示出來了notify()方法接收兩個參數,第一個參數是 id,要保證為每個通知所指定的 id 都是不同的。第二個參數則是 Notification 對象,這裡直接將我們剛剛建立好的 Notification 對象傳入即可。因此,顯示一個通知就可以寫成:
                manager.notify(1, notification); 
  1. import android.app.Activity;
  2. import android.app.Notification;
  3. import android.app.NotificationManager;
  4. import android.app.PendingIntent;
  5. import android.content.Intent;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.view.View.OnClickListener;
  9. import android.widget.Button;
  10. import android.widget.RemoteViews;
  11. publicclassMyNotificationActivityextendsActivity{
  12. privateButton btn_notify1;
  13. privateNotificationManager nManager;
  14. privateNotification notification ;
  15. @Override
  16. protectedvoid onCreate(Bundle savedInstanceState){
  17. // TODO Auto-generated method stub
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.layout_notification);
  20. //得到notification管理器
  21. nManager =(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  22. btn_notify1 =(Button)findViewById(R.id.btn_notify1);
  23. btn_notify1.setOnClickListener(newOnClickListener(){
  24. @Override
  25. publicvoid onClick(View v){
  26. // TODO Auto-generated method stub
  27. PendingIntent piIntent =PendingIntent.getActivity(MyNotificationActivity.this,1,newIntent(MyNotificationActivity.this,FormActivity.class),1);
  28. /*Notification notification = new Notification(R.drawable.p2409, "You have a message", System.currentTimeMillis());
  29. notification.setLatestEventInfo(MyNotificationActivity.this, "Racoon", "Love U", piIntent);
  30. */
  31. //建立notification執行個體
  32. notification =newNotification.Builder(MyNotificationActivity.this)
  33. .setContentText("Love U")
  34. .setContentTitle("little Racoon")
  35. .setTicker("You have a new message")
  36. .setSmallIcon(R.drawable.peasy)//狀態列的表徵圖
  37. .setContentIntent(piIntent)
  38. .getNotification();
  39. notification.contentView =newRemoteViews(getPackageName(), R.layout.layout_customnotification);
  40. //把notification發布到狀態列
  41. nManager.notify(1, notification);
  42. }
  43. });
  44. }
  45. @Override
  46. protectedvoid onStop(){
  47. // TODO Auto-generated method stub
  48. nManager.cancelAll();
  49. super.onStop();
  50. }
  51. }
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical">
  6. <Button
  7. android:id="@+id/btn_notify1"
  8. style="?android:attr/buttonStyleSmall"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:text="Common notify"/>
  12. </LinearLayout>
現在就可以來運行一下程式了,點擊 Common notify 按鈕,就會看到有一條通知在系統狀態列顯示出來,。  下拉系統狀態列可以看到該通知的詳細資料,  如果你使用過 Android 手機,此時應該會下意識地認為這條通知是可以點擊的。但是當你去點擊它的時候,你會發現沒有任何效果。不對啊,好像每條通知點擊之後都應該會有反應的呀?其實要想實現通知的點擊效果,我們還需要在代碼中進行相應的設定,這就涉及到了一個新的概念,PendingIntent。  PendingIntent 從名字上看起來就和 Intent 有些類似,它們之間也確實存在著不少共同點。比如它們都可以去指明某一個“意圖”,都可以用於啟動活動、啟動服務以及發送廣播等。不同的是,Intent 更加傾向於去立即執行某個動作,而 PendingIntent 更加傾向於在某個合適的時機去執行某個動作。所以,也可以把 PendingIntent 簡單地理解為順延強制的 Intent。PendingIntent並不是Intent!  PendingIntent 的用法同樣很簡單, 它主要提供了幾個靜態方法用於擷取 PendingIntent 的執行個體,可以根據需求來選擇是使用 getActivity()方法、getBroadcast()方法、還是 getService() 方法。這幾個方法所接收的參數都是相同的,第一個參數依舊是 Context,不用多做解釋。第二個參數一般用不到,通常都是傳入 0 即可。第三個參數是一個 Intent 對象,我們可以通過這個對象構建出 PendingIntent 的“意圖”。第四個參數用於確定 PendingIntent 的行為,有FLAG_ONE_SHOT、FLAG_NO_CREATE、FLAG_CANCEL_CURRENT 和 FLAG_UPDATE_CURRENT 這四種值可選,每種值的含義你可以查看文檔。  怎麼系統狀態上的通知表徵圖還沒有消失呢?是這樣的,如果我們沒有在代碼中對該通知進行取消,它就會一直顯示在系統的狀態列上顯示。解決的方法也很簡單,調用NotificationManager 的 cancel()方法就可以取消通知了。  



來自為知筆記(Wiz)



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.