在Android中使用Notification進行提示

jopen 12年前發布 | 45K 次閱讀 Android Android開發 移動開發

用慣了Android的人在剛拿到iPhone的時候,總是會習慣性的用手指從狀態欄往下拖一下,這都是給Notification鬧的。
不過Notification也確實是1個不錯的提示工具,不干擾正常的操作,事后還可以再翻看詳細的內容,點擊后還可以進入相關的畫面查看更具體的內容。
今天我就以代碼為主的形式來介紹Notification的使用,包括基本用法,自定義的View,以及更多的控制方法。
另一種Android中常用到的提示方法Toast的用法請參見《教程:在Android中使用Toast進行提示》
我們先看下Notification的幾個主要組成部分:
Icon:不解釋
Ticker Text:Notification剛出來的時候,在狀態欄上滾動的字幕,如果很長,會自動分割滾動
Icon和Ticker Text
Content Title:Notification展開后的標題
Content Text:Notification展開后的內容
Content Title和Text

Notification的一般用法

取得NotificationManager

private NotificationManager mNotificationManager;
mNotificationManager = (NotificationManager) 
    getSystemService(Context.NOTIFICATION_SERVICE);

創建Notification并且顯示
//Notification的滾動提示
String tickerText = "My notification, It's a long text! Hello World desiyo?";
//Notification的圖標,一般不要用彩色的
int icon = R.drawable.icon_02241_3;

//contentTitle和contentText都是標準的Notification View的內容
//Notification的內容標題,拖下來后看到的標題
String contentTitle="My notification";
//Notification的內容
String contentText="Hello World!";

//Notification的Intent,即點擊后轉向的Activity
Intent notificationIntent = new Intent(this, this.getClass());
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, 
    notificationIntent, 0);

//創建Notifcation
Notification notification = new Notification(icon, tickerText, System.currentTimeMillis());
//設定Notification出現時的聲音,一般不建議自定義
notification.defaults |= Notification.DEFAULT_SOUND;
//設定如何振動
notification.defaults |= Notification.DEFAULT_VIBRATE;
//指定Flag,Notification.FLAG_AUTO_CANCEL意指點擊這個Notification后,立刻取消自身
//這符合一般的Notification的運作規范
notification.flags|=Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(this, contentTitle, contentText, contentIntent);
//顯示這個notification
mNotificationManager.notify(HELLO_ID, notification);

這是最基本的應用,可以說除了找個合適的圖標以外,其它都很簡單。

使用自定義View的Notification

同Toast一樣,我們也可以自已指定1個View來作為Notification展開后的顯示內容,比如說在Android Market中下載的時候,Notification中會顯示當前下載的進度,那么我們也來模擬1個這樣的效果吧。
首先給出View的定義文件:notification_view_sample.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="3dp"
>
    <ImageView android:id="@+id/notificationImage"

        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:src="@android:drawable/stat_sys_download"
    />
    <TextView android:id="@+id/notificationTitle"

        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_toRightOf="@id/notificationImage"
        android:layout_alignParentRight="true"
        android:paddingLeft="6dp"

        android:textColor="#FF000000"
    />
    <TextView android:id="@+id/notificationPercent"
        android:layout_width="wrap_content" android:layout_height="wrap_content"

        android:layout_below="@id/notificationImage"
        android:paddingTop="2dp"
        android:textColor="#FF000000"
    />
    <ProgressBar android:id="@+id/notificationProgress"

        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_below="@id/notificationTitle"
        android:layout_alignLeft="@id/notificationTitle"
        android:layout_alignParentRight="true"

        android:layout_alignTop="@id/notificationPercent"
        android:paddingLeft="6dp"
        android:paddingRight="3dp"
        android:paddingTop="2dp"
        style="?android:attr/progressBarStyleHorizontal"

    />
</RelativeLayout>

接下來是Java代碼片段:
//Notification的滾動提示
String tickerText1 = "Custom view for download notification";
//Notification的圖標,一般不要用彩色的
int icon1 = android.R.drawable.stat_sys_download;

//Notification的Intent,即點擊后轉向的Activity
Intent notificationIntent1 = new Intent(this, this.getClass());
notificationIntent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent1 = PendingIntent.getActivity(this, 0, notificationIntent1, 0);

//創建Notifcation
Notification notification1 = new Notification(icon1, tickerText1, System.currentTimeMillis());
//設定Notification出現時的聲音,一般不建議自定義
notification1.defaults |= Notification.DEFAULT_SOUND;
//設定是否振動
notification1.defaults |= Notification.DEFAULT_VIBRATE;
//notification.number=numbers++;
//指定Flag,Notification.FLAG_AUTO_CANCEL意指點擊這個Notification后,立刻取消自身
//這符合一般的Notification的運作規范
notification1.flags|=Notification.FLAG_ONGOING_EVENT;

//創建RemoteViews用在Notification中
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification_view_sample);
contentView.setTextViewText(R.id.notificationTitle, "Download:非死book for android");
contentView.setTextViewText(R.id.notificationPercent, "35%");
contentView.setProgressBar(R.id.notificationProgress, 100, 35, false);

notification1.contentView = contentView;
notification1.contentIntent=contentIntent1;

//顯示這個notification
mNotificationManager.notify(CUSTOM_VIEW_ID, notification1);

注意以上代碼中使用的是RemoteViews,而不是普通的View,另外使用的是PendingIntent而不是普通的Intent,這都說明了Notification是1個“遠程”的東西,其中能夠使用的控件是受限制的,比如說TableLayout就不能使用。看下效果圖,是不是和 Market中的界面很接近呢?
自定義View的Notification效果圖

更好的控制Notification

動畫圖標怎么做?

和selector類似,定義1個XML文件放在drawable下,下面是之前用到的stat_sys_download的定義:

<animation-list
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:oneshot="false">
    <item android:drawable="@drawable/stat_sys_download_anim0" android:duration="200" />

    <item android:drawable="@drawable/stat_sys_download_anim1" android:duration="200" />
    <item android:drawable="@drawable/stat_sys_download_anim2" android:duration="200" />

    <item android:drawable="@drawable/stat_sys_download_anim3" android:duration="200" />
    <item android:drawable="@drawable/stat_sys_download_anim4" android:duration="200" />

    <item android:drawable="@drawable/stat_sys_download_anim5" android:duration="200" />
</animation-list>

如何更新Notification?

注意到前面的代碼中用到的CUSTOM_VIEW_ID,這是Notification的ID,如果2次彈出的Notification的ID相同,那么Notification就只會更新而不會再次滾動提醒。之前給出的ProgressBar是不會動的,利用這個方法就可以讓它動起來(或者也可以直接調用RemoteView的set方法來直接更新?未試驗)

如何自定義提示的聲音和振動?
//自定義提示音
notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");
//自定義振動方式
long[] vibrate = {0,100,200,300};
notification.vibrate = vibrate;

請注意:如果使用了DEFAULT_SOUND或DEFAULT_VIBRATE,則自定義的提示音和振動無效。

在類似于短消息的應用中如何提示數量?

使用Notification的number屬性,默認為0,如果是1或更大的數字,則會在圖標上覆蓋顯示這個數字。
notification.number=notificationNumber;

Flag的使用

notification有1個flag屬性,除了DEFAULT_SOUND之外,還有幾個很有用的屬性。
FLAG_AUTO_CANCEL:自動清除Notification,前面的例子中有用到
FLAG_INSISTENT:提示音一直不停,直至用戶響應(很吵吧!)
FLAG_ONGOING_EVENT:表示這是1個正在進行的任務,不可以清除,第2個例子中有用到
FLAG_NO_CLEAR:不可以清除

文章出處:http://www.learningandroid.net/blog/others/tutorial-android-notification-sample/

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!