>
接下來的會顯示狀態(tài)欄窗口。用戶可以打開這個窗口通過下滑狀態(tài)欄?;蛘呤褂胢enu菜單鍵選擇。
一個activity或者Service可以初始化狀態(tài)欄通知,因?yàn)閍ctivity只有在活動狀態(tài)下才能執(zhí)行一些命令,所以你需要從一個service來建立狀態(tài)通知。當(dāng)用戶啟動了其他程序或者設(shè)備已經(jīng)休眠時,通過這種方式,通知就可以在后臺被創(chuàng)建。你要用到這兩個類:Notification類和NotificationManager類。
Notification類來定義狀態(tài)通知的屬性,比如圖標(biāo),提示信息,或者提示聲音。NotificationManager是一個android系統(tǒng)的服務(wù),來管理和運(yùn)行所有通知的,他不能被實(shí)例化,你可以用getSystemService()方法獲得他的句柄。當(dāng)你想通知用戶時,調(diào)用notify()方法即可。
創(chuàng)建一個菜單欄通知:
1-獲得MotificationManager的引用。
- String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
復(fù)制代碼2-實(shí)例化Notification: - int icon = R.drawable.notification_icon;
CharSequence tickerText = "Hello";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
復(fù)制代碼3-定義Notification,如顯示icon、目標(biāo)intent等信息
- Context context = getApplicationContext();
CharSequence contentTitle = "My notification";
CharSequence contentText = "Hello World!";
Intent notificationIntent = new Intent(this, MyClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
復(fù)制代碼4-傳遞給Manager. - private static final int HELLO_ID = 1;
mNotificationManager.notify(HELLO_ID, notification);
復(fù)制代碼好了,一個通知寫完了。 NotificationManager是一個管理所有通知的系統(tǒng)服務(wù)。你可以通過getSystemService()方法得到她的句柄: - String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
復(fù)制代碼當(dāng)你想發(fā)送一個狀態(tài)欄通知時,通過notify(int,Notification)方法傳遞Notification對象給NotificationManager,第一個參數(shù)是Notification的id,第二個參數(shù)是通知的對象。id是一個唯一的標(biāo)示,當(dāng)你改變通知或者進(jìn)行一些的活動時必須要用到它。當(dāng)用戶從通知窗口選擇了某個通知時,會給這個通知添加一個“FLAG_AUTO_CANCEL”標(biāo)記來移除這個通知。你也可以使用cancel(int)方法,通過指定通知id來取消某個通知,或者干脆cancelAll()。
一個通知對象定義了顯示在狀態(tài)欄的一些細(xì)節(jié)信息,和所有通知方式,例如聲音、閃光。一個狀態(tài)欄通知必須包括以下幾點(diǎn):@ 顯示在狀態(tài)欄的圖標(biāo)@ 一個標(biāo)題(除非你自定義了提示界面)@ 一個Pending Intent,即點(diǎn)擊后要做的操作??蛇x項(xiàng)包括以下:@ 狀態(tài)欄提示文本@ 提示聲音@ 震動@ 閃光初學(xué)者套件?為新的通知包含了MNotification(int,CharSequence,long)構(gòu)造方法和setLatestEventInfo(Context,CharSequence,CharSequence,PendingIntent)方法。這些參數(shù)是一個通知的必要參數(shù)。下面的代碼片段是一個簡單的例子: - int icon = R.drawable.notification_icon; // icon from resources
CharSequence tickerText = "Hello"; // ticker-text
long when = System.currentTimeMillis(); // notification time
Context context = getApplicationContext(); // application Context
CharSequence contentTitle = "My notification"; // expanded message title
CharSequence contentText = "Hello World!"; // expanded message text
Intent notificationIntent = new Intent(this, MyClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
// the next two lines initialize the Notification, using the configurations above
Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
復(fù)制代碼你可以在事件改變時修改狀態(tài)欄的通知。例如,當(dāng)有一條未讀短信時又來了一條短信,此時狀態(tài)欄應(yīng)該顯示新的短信。像這種情況下,添加一條洗得通知不如更改原有通知更加合理,因?yàn)楹笳吣鼙苊馔ㄖ幕靵y。因?yàn)槊總€通知都有一個唯一的Id,你可以通過setLatestEventInfo()方法修改通知,然后調(diào)用notify()讓其顯示。你可以修改每個屬性,除了context和下拉狀態(tài)欄時顯示的標(biāo)題和文本。你可以通過setLatestEventInfo()方法給contentTitle和conTentText設(shè)置文本信息,然后調(diào)用notify()方法來更新通知。(當(dāng)然了你可以創(chuàng)建自己的布局文件,那樣的話更新顯示文本就沒有效果了)。 你可以使用默認(rèn)的聲音來提醒用戶有一個新通知。方法是添加一個“DEFAULT_SOUND”參數(shù)。 - notification.defaults |= Notification.DEFAULT_SOUND;
復(fù)制代碼如果使用非默認(rèn)的聲音,需要傳遞一個URI資源引用。例如: - notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");
復(fù)制代碼下個例子中,音頻文件從MediaStore類中獲取: - notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
復(fù)制代碼這種情況下,資源id為6的音頻文件時已知的,并且已添加到content的URI中。如果你不知道exact ID,你必須在MediaStore中查詢所有可用的資源。參考 Content Providers文檔。如果你想讓提示聲音一直播放知道用戶響應(yīng)位置,你可以添加“FLAG_INSISTENT”參數(shù)。 注意:如果包含“DEFAULT_SOUND”參數(shù),那么默認(rèn)聲音會覆蓋其他的聲音設(shè)置。 你可以使用默認(rèn)的震動方式提示用戶: - notification.defaults |= Notification.DEFAULT_VIBRATE;
復(fù)制代碼 或者使用自己的振動方式,例如: - long[] vibrate = {0,100,200,300};
notification.vibrate = vibrate;
復(fù)制代碼 這個數(shù)組定義了交替的震動和關(guān)閉,一毫秒為單位。第一個值是等待多久開始震動,第二個值是第一次震動的時間,第三個是停止震動的時間,以此類推。定義多長時間都行,但是不能設(shè)置為重復(fù)。注意:如果設(shè)置了默認(rèn),則其他振動方式無效。 讓手機(jī)閃動LED燈,你可以實(shí)現(xiàn)默認(rèn)的閃動色繪制?;蛘叨x自己的閃動效果。使用默認(rèn)的閃光效果,代碼如下: - notification.defaults |= Notification.DEFAULT_LIGHTS;
復(fù)制代碼定義自己的效果,可以設(shè)置顏色、定義顯示的時間,一些常用的值如下: - notification.ledARGB = 0xff00ff00;
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
復(fù)制代碼 在這個例子里,綠燈先顯示300毫秒然后關(guān)閉一秒鐘,設(shè)備不是支持所有的顏色,而且不是所有的設(shè)備都支持相同顏色。所以設(shè)備會按照指定值顯示最接近的顏色。綠色是常見的顏色。 你可以添加幾個特性到你的通知中去。下面是一些有用的設(shè)置: "FLAG_AUTO_CANCEL"標(biāo)記 這個標(biāo)記會在用戶選擇查看通知窗口后自動關(guān)閉通知。 "FLAG_INSISTENT" 在用戶響應(yīng)之前一直重復(fù) "FLAG_ONGOING_EVENT" 添加這個標(biāo)記把通知分在“正在運(yùn)行”組中,說明程序正在運(yùn)行,或者后臺運(yùn)行,甚至當(dāng)程序不可見時,例如播放音樂或者接聽電話時。 "FLAG_NO_CLEAR" 這個標(biāo)記讓你的通知不會被Clear按鈕所取消。對一直進(jìn)行中的通知非常有用。 number field 標(biāo)記表示當(dāng)前通知所代表的事件數(shù)。這個數(shù)字顯示在icon之上。如果你打算這樣使用,當(dāng)通知第一次建立時必須從1開始計數(shù)。如果你改變了他的值從0或其他值開始,那么他將不會顯示。 iconLevel field這個值指當(dāng)前icon的LevelListDrawable等級。通過改變這個值,你可以讓狀態(tài)欄的icon顯示動畫。 更多詳細(xì)的特性和使用方法餐卡Notification類。
默認(rèn)情況下,下拉的Notification窗口包含一個標(biāo)題和文本信息。setLatestEventInfo()有兩個默認(rèn)的參數(shù)contentTitle和contentText 。然而你通過RemoteViews也可以定義自己下拉通知視圖。上賣弄的截圖就顯示了一個自定義布局的通知視圖,包括一個TextView和ImageView。
定義自己的通知視圖,實(shí)例化RemoteViews對象并且傳遞給contentView。給contentIntent字段傳遞PendingIntent值。創(chuàng)建一個自定義的通知視圖最好的理解方法就是寫一個例子:
1-建立xml布局文件。例如:custom_notification_layout.xml: - <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="3dp"
>
<ImageView android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="10dp"
/>
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#000"
/>
</LinearLayout>
復(fù)制代碼這是一個自定義的擴(kuò)展通知的視圖,但是ImageView和TextView仍然需要程序定義。RemoteViews提供一些方便的方法來讓你定義content。 2-下面的代碼,使用RemoveViews 的方法定義image和text。然后傳遞RemoteViews 對象給通知的contentView字段。如下: - RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout);
contentView.setImageViewResource(R.id.image, R.drawable.notification_image);
contentView.setTextViewText(R.id.text, "Hello, this message is in a custom expanded view");
notification.contentView = contentView;
復(fù)制代碼就像顯示的那樣,傳遞程序的包名,布局的資源id給RemoteViews的構(gòu)造方法。然后,定義ImageView和TextView的content,使用setImageViewResource()和setTextViewText().這種情況下,傳遞你要設(shè)置的view對象的引用id。最后把RemoteViews 對象傳遞給contentView。 3-因?yàn)槟悴恍枰猻etLatestEventInfo()了,你必須為通知定義一個intent,例如: - Intent notificationIntent = new Intent(this, MyClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
復(fù)制代碼 4-通知可以這樣調(diào)用: - mNotificationManager.notify(CUSTOM_VIEW_ID, notification);
復(fù)制代碼 RemoteViews 類也包含一些方法讓你輕松地添加Chronometer或者ProgressBar在你的通知view里。注意:當(dāng)建立一個自定義的通知view,你必須特別小心自定義的布局來適用不同設(shè)備的分辨率。尤其這種情況下,更要特別的小心,過于復(fù)雜的布局一定要多多測試。
|