Android之Notification的多種用法

 我們在用手機的時候,如果來了短信,而我們沒有點擊查看的話,是不是在手機的最上邊的狀態欄裏有一個短信的小圖標提示啊?你是不是也想實現這種功能呢?今天的Notification就是解決這個問題的。

       我們也知道Android系統也是在不斷升級的,有關Notification的用法也就有很多種,有的方法已經被android拋棄了,現在我實現了三種不同的方法,並適應不同的android版本。現在我就把代碼公佈出來,我喜歡把解釋寫在代碼中,在這裏我就不多說了,先看效果圖:

再看代碼,主要的代碼如下:

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. package net.loonggg.notification;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.Notification;  
  5. import android.app.NotificationManager;  
  6. import android.app.PendingIntent;  
  7. import android.content.Context;  
  8. import android.content.Intent;  
  9. import android.os.Bundle;  
  10. import android.view.View;  
  11. import android.widget.RemoteViews;  
  12.   
  13. public class MainActivity extends Activity {  
  14.     private static final int NOTIFICATION_FLAG = 1;  
  15.   
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.activity_main);  
  20.     }  
  21.   
  22.     public void notificationMethod(View view) {  
  23.         // 在Android進行通知處理,首先需要重系統哪裏獲得通知管理器NotificationManager,它是一個系統Service。  
  24.         NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  
  25.         switch (view.getId()) {  
  26.         // 默認通知  
  27.         case R.id.btn1:  
  28.             // 創建一個PendingIntent,和Intent類似,不同的是由於不是馬上調用,需要在下拉狀態條出發的activity,所以採用的是PendingIntent,即點擊Notification跳轉啓動到哪個Activity  
  29.             PendingIntent pendingIntent = PendingIntent.getActivity(this0,  
  30.                     new Intent(this, MainActivity.class), 0);  
  31.             // 下面需兼容Android 2.x版本是的處理方式  
  32.             // Notification notify1 = new Notification(R.drawable.message,  
  33.             // "TickerText:" + "您有新短消息,請注意查收!", System.currentTimeMillis());  
  34.             Notification notify1 = new Notification();  
  35.             notify1.icon = R.drawable.message;  
  36.             notify1.tickerText = "TickerText:您有新短消息,請注意查收!";  
  37.             notify1.when = System.currentTimeMillis();  
  38.             notify1.setLatestEventInfo(this"Notification Title",  
  39.                     "This is the notification message", pendingIntent);  
  40.             notify1.number = 1;  
  41.             notify1.flags |= Notification.FLAG_AUTO_CANCEL; // FLAG_AUTO_CANCEL表明當通知被用戶點擊時,通知將被清除。  
  42.             // 通過通知管理器來發起通知。如果id不同,則每click,在statu那裏增加一個提示  
  43.             manager.notify(NOTIFICATION_FLAG, notify1);  
  44.             break;  
  45.         // 默認通知 API11及之後可用  
  46.         case R.id.btn2:  
  47.             PendingIntent pendingIntent2 = PendingIntent.getActivity(this0,  
  48.                     new Intent(this, MainActivity.class), 0);  
  49.             // 通過Notification.Builder來創建通知,注意API Level  
  50.             // API11之後才支持  
  51.             Notification notify2 = new Notification.Builder(this)  
  52.                     .setSmallIcon(R.drawable.message) // 設置狀態欄中的小圖片,尺寸一般建議在24×24,這個圖片同樣也是在下拉狀態欄中所顯示,如果在那裏需要更換更大的圖片,可以使用setLargeIcon(Bitmap  
  53.                                                         // icon)  
  54.                     .setTicker("TickerText:" + "您有新短消息,請注意查收!")// 設置在status  
  55.                                                                 // bar上顯示的提示文字  
  56.                     .setContentTitle("Notification Title")// 設置在下拉status  
  57.                                                             // bar後Activity,本例子中的NotififyMessage的TextView中顯示的標題  
  58.                     .setContentText("This is the notification message")// TextView中顯示的詳細內容  
  59.                     .setContentIntent(pendingIntent2) // 關聯PendingIntent  
  60.                     .setNumber(1// 在TextView的右方顯示的數字,可放大圖片看,在最右側。這個number同時也起到一個序列號的左右,如果多個觸發多個通知(同一ID),可以指定顯示哪一個。  
  61.                     .getNotification(); // 需要注意build()是在API level  
  62.             // 16及之後增加的,在API11中可以使用getNotificatin()來代替  
  63.             notify2.flags |= Notification.FLAG_AUTO_CANCEL;  
  64.             manager.notify(NOTIFICATION_FLAG, notify2);  
  65.             break;  
  66.         // 默認通知 API16及之後可用  
  67.         case R.id.btn3:  
  68.             PendingIntent pendingIntent3 = PendingIntent.getActivity(this0,  
  69.                     new Intent(this, MainActivity.class), 0);  
  70.             // 通過Notification.Builder來創建通知,注意API Level  
  71.             // API16之後才支持  
  72.             Notification notify3 = new Notification.Builder(this)  
  73.                     .setSmallIcon(R.drawable.message)  
  74.                     .setTicker("TickerText:" + "您有新短消息,請注意查收!")  
  75.                     .setContentTitle("Notification Title")  
  76.                     .setContentText("This is the notification message")  
  77.                     .setContentIntent(pendingIntent3).setNumber(1).build(); // 需要注意build()是在API  
  78.                                                                             // level16及之後增加的,API11可以使用getNotificatin()來替代  
  79.             notify3.flags |= Notification.FLAG_AUTO_CANCEL; // FLAG_AUTO_CANCEL表明當通知被用戶點擊時,通知將被清除。  
  80.             manager.notify(NOTIFICATION_FLAG, notify3);// 步驟4:通過通知管理器來發起通知。如果id不同,則每click,在status哪裏增加一個提示  
  81.             break;  
  82.         // 自定義通知  
  83.         case R.id.btn4:  
  84.             // Notification myNotify = new Notification(R.drawable.message,  
  85.             // "自定義通知:您有新短信息了,請注意查收!", System.currentTimeMillis());  
  86.             Notification myNotify = new Notification();  
  87.             myNotify.icon = R.drawable.message;  
  88.             myNotify.tickerText = "TickerText:您有新短消息,請注意查收!";  
  89.             myNotify.when = System.currentTimeMillis();  
  90.             myNotify.flags = Notification.FLAG_NO_CLEAR;// 不能夠自動清除  
  91.             RemoteViews rv = new RemoteViews(getPackageName(),  
  92.                     R.layout.my_notification);  
  93.             rv.setTextViewText(R.id.text_content, "hello wrold!");  
  94.             myNotify.contentView = rv;  
  95.             Intent intent = new Intent(Intent.ACTION_MAIN);  
  96.             PendingIntent contentIntent = PendingIntent.getActivity(this1,  
  97.                     intent, 1);  
  98.             myNotify.contentIntent = contentIntent;  
  99.             manager.notify(NOTIFICATION_FLAG, myNotify);  
  100.             break;  
  101.         case R.id.btn5:  
  102.             // 清除id爲NOTIFICATION_FLAG的通知  
  103.             manager.cancel(NOTIFICATION_FLAG);  
  104.             // 清除所有的通知  
  105.             // manager.cancelAll();  
  106.             break;  
  107.         default:  
  108.             break;  
  109.         }  
  110.     }  
  111. }  
再看主佈局文件:

[html] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical"  
  6.     tools:context=".MainActivity" >  
  7.   
  8.     <Button  
  9.         android:id="@+id/btn1"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:onClick="notificationMethod"  
  13.         android:text="默認通知(已被拋棄,但是通用)" />  
  14.   
  15.     <Button  
  16.         android:id="@+id/btn2"  
  17.         android:layout_width="fill_parent"  
  18.         android:layout_height="wrap_content"  
  19.         android:onClick="notificationMethod"  
  20.         android:text="默認通知(API11之後可用)" />  
  21.   
  22.     <Button  
  23.         android:id="@+id/btn3"  
  24.         android:layout_width="fill_parent"  
  25.         android:layout_height="wrap_content"  
  26.         android:onClick="notificationMethod"  
  27.         android:text="默認通知(API16之後可用)" />  
  28.   
  29.     <Button  
  30.         android:id="@+id/btn4"  
  31.         android:layout_width="fill_parent"  
  32.         android:layout_height="wrap_content"  
  33.         android:onClick="notificationMethod"  
  34.         android:text="自定義通知" />  
  35.   
  36.     <Button  
  37.         android:id="@+id/btn5"  
  38.         android:layout_width="fill_parent"  
  39.         android:layout_height="wrap_content"  
  40.         android:onClick="notificationMethod"  
  41.         android:text="清除通知" />  
  42.   
  43. </LinearLayout>  
還有一個是:自定義通知的佈局文件my_notification.xml,代碼如下:

[html] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:background="#ffffff"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <TextView  
  9.         android:id="@+id/text_content"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:textSize="20sp" />  
  13.   
  14. </LinearLayout>  
轉載請註明出處:http://blog.csdn.net/loongggdroid/article/details/17616509
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章