標籤:notification android 通知 android的通知 通知操作
通知(Notification)是android系統中比較有特色的一個功能,當某個應用程式希望向使用者發出一些提示資訊,而該應用程式又不在前台運行時,就可以藉助通知來實現。發出一條通知後,手機最上方的狀態列會顯示一個通知的表徵圖,下拉狀態後可以看到通知的詳細內容。
通知的基本用法
通知可以在活動,廣播接收器,和服務裡面建立。無論在哪裡建立通知,整體的步驟都是相同的,下面我們來學習詳細的步驟。
/* * 首先需要一個NotificationManager來對通知進行管理,可以調用Context的getSystemService()方法 * 擷取到。getSystemService方法接收一個字串參數用於確定擷取系統的哪個服務,這裡我們傳入的 * Context.NOTIFICATION_SERVICE即可。因此擷取NotificationManager的執行個體就可以寫成: * NotificationManager manager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); * 接下來要建立一個Notification對象,這個對象用於儲存通知所需的各種資訊,我們使用它的建構函式來進行建立。 * Notification的有參建構函式接收三個參數,第一個參數用於指定通知的表徵圖,比如項目的res/drawable目錄 * 下有一張ic_launcher圖片,那麼這裡就可以傳入R.drawable.ic_launcher,第二個參數用於指定通知 * 的ticker內容,當通知剛被建立的時候,它會在系統的狀態列一閃而過,屬於瞬時的提示資訊。第三個參數用於 * 指定通知被建立的時間,以毫秒為單位,當下拉系統狀態列時,這裡指定的時間會顯示在相應的通知上。因此建立 * 一個Notification對象就可以寫成: * Notification notification=new Notification(R.drawable.ic_launcher,"this is ticker text",System.currentTimeMillis());* 建立好了Notification對象後,我們還需要對通知的布局進行設定,這裡只需要調用Notification的* setLatestEventInfo()方法就可以給通知設定一個標準布局。這個方法接收四個參數,第一個參數* 是Context。第二個參數用於指定通知的標題內容,下拉系統狀態列就可以看到這部分內容。第三個參數用於指定* 通知的本文內容,同樣下拉系統狀態列就可以看到這部分內容。第四個參數我們暫時用不到,可以傳入null。因此* 對通知的布局進行設定就可以寫成:* notification.setLatestEventInfo(this, "this is content title","this is content text", null); 以上工作都做完了後,只需要調用NotificationManager的notify()方法就可以讓通知顯示出來了。 notify()方法接收2個參數,第一個參數是id,要保證為每個通知所指定的id都是不同的。第二個參數則是 Notification對象,這裡直接將我們剛剛建立好的Notification對象傳入即可。因此,顯示一個通知就可以 寫成: manager.notify(1,notification);* */
建立一個項目,項目名為NotificationTest,並修改activity_main.xml中的代碼,如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/send_notice" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="send notice" /></LinearLayout>
修改MainActivity中的代碼:
package com.jack.notificationtest;import android.os.Bundle;import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.content.Context;import android.content.IntentSender.SendIntentException;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity implements OnClickListener{/* * 首先需要一個NotificationManager來對通知進行管理,可以調用Context的getSystemService()方法 * 擷取到。getSystemService方法接收一個字串參數用於確定擷取系統的哪個服務,這裡我們傳入的 * Context.NOTIFICATION_SERVICE即可。因此擷取NotificationManager的執行個體就可以寫成: * NotificationManager manager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); * 接下來要建立一個Notification對象,這個對象用於儲存通知所需的各種資訊,我們使用它的建構函式來進行建立。 * Notification的有參建構函式接收三個參數,第一個參數用於指定通知的表徵圖,比如項目的res/drawable目錄 * 下有一張ic_launcher圖片,那麼這裡就可以傳入R.drawable.ic_launcher,第二個參數用於指定通知 * 的ticker內容,當通知剛被建立的時候,它會在系統的狀態列一閃而過,屬於瞬時的提示資訊。第三個參數用於 * 指定通知被建立的時間,以毫秒為單位,當下拉系統狀態列時,這裡指定的時間會顯示在相應的通知上。因此建立 * 一個Notification對象就可以寫成: * Notification notification=new Notification(R.drawable.ic_launcher,"this is ticker text",System.currentTimeMillis());* 建立好了Notification對象後,我們還需要對通知的布局進行設定,這裡只需要調用Notification的* setLatestEventInfo()方法就可以給通知設定一個標準布局。這個方法接收四個參數,第一個參數* 是Context。第二個參數用於指定通知的標題內容,下拉系統狀態列就可以看到這部分內容。第三個參數用於指定* 通知的本文內容,同樣下拉系統狀態列就可以看到這部分內容。第四個參數我們暫時用不到,可以傳入null。因此* 對通知的布局進行設定就可以寫成:* notification.setLatestEventInfo(this, "this is content title","this is content text", null); 以上工作都做完了後,只需要調用NotificationManager的notify()方法就可以讓通知顯示出來了。 notify()方法接收2個參數,第一個參數是id,要保證為每個通知所指定的id都是不同的。第二個參數則是 Notification對象,這裡直接將我們剛剛建立好的Notification對象傳入即可。因此,顯示一個通知就可以 寫成: manager.notify(1,notification);* */private Button sendNotice;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);sendNotice=(Button) findViewById(R.id.send_notice);sendNotice.setOnClickListener(this);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubswitch(v.getId()){case R.id.send_notice:NotificationManager manager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);Notification notification=new Notification(R.drawable.ic_launcher,"this is ticker text",System.currentTimeMillis());notification.setLatestEventInfo(this, "this is content title","this is content text", null);manager.notify(1,notification);break;default:break;}}}
運行程式,點擊森達 notice介面如下:
下拉系統狀態列可以看到該通知的詳細資料,如下所示:
上面的通知,我們不能點擊,如果要想實現通知的點擊效果,我們就要在代碼中進行相應的設定,這涉及到了PendingIntent。PendingIntent和Intent有些類似,它們之間也確實存在在不少共同點。比如它們都可以去指明一個“意圖”,都可以用於啟動活動,啟動服務以及發送廣播等。不同的是Intent更傾向於立刻執行某個動作,而PendingIntent更傾向於在某個合適的時機去執行某個動作。所以也可以把PendingIntent簡單的理解為順延強制的Intent。
PendingIntent的用法比較簡單,它主要提供了幾個靜態方法用於擷取PendingIntent的執行個體,可以根據需求來選擇是使用getActivity()方法,getBroadcast()方法,還是getService()方法。這幾個方法所接收的參數都是相同的,第一個參數依舊是Context。第二個參數一般用不到,通常傳入0即可。第三個參數是一個Intent對象,我們可以通過這個對象構建出PendingIntent的“意圖”。第四個參數用於確定PendingIntent的行為,有FLAG_ONE_SHOT,FLAG_NO_CREATE,FLAG_CANCEL_CURRENT 和 FLAG_UPDATE_CURRENT這四種值可選,每種值的含義,可以自己查詢下。
還記得Notification的setLatestEventInfo()方法。剛才我們將setLatestEventInfo()的第四個參數忽略掉了,直接傳入了null,現在你會發現,第四個參數正是一個PendingIntent對象。因此,這裡就可以通過PendingIntent構建出一個順延強制的“意圖”,當使用者點擊這條通知時就會執行相應的邏輯。
現在我們來最佳化下NotificationTest項目,給剛才的通知加上點擊功能,讓使用者點擊它的時候可以啟動另一個活動。
首先需要準備好一個活動,這裡建立布局檔案notification_layout.xml,代碼如下所示:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="24sp" android:text="this is notificaiton layout" /></LinearLayout>
建立NotificationActivity類繼承Activity,載入上面定義的布局,代碼如下所示:
package com.jack.notificationtest;import android.app.Activity;import android.os.Bundle;public class NotificationActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.notification_layout);}}修改AndroidManifest.xml中的代碼,在裡面加入NotificationActivity的註冊聲明:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.jack.notificationtest" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="13" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.jack.notificationtest.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.jack.notificationtest.NotificationActivity" ></activity> </application></manifest>
下面在修改MainActivity中的代碼給通知加入點擊功能,如下所示:
@Overridepublic void onClick(View v) {// TODO Auto-generated method stubswitch(v.getId()){case R.id.send_notice:NotificationManager manager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);Notification notification=new Notification(R.drawable.ic_launcher,"this is ticker text",System.currentTimeMillis());Intent intent=new Intent(this,NotificationActivity.class);PendingIntent pi=PendingIntent.getActivity(this, 0,intent, PendingIntent.FLAG_CANCEL_CURRENT);notification.setLatestEventInfo(this, "this is content title","this is content text", pi);manager.notify(1,notification);break;default:break;}}
重新運行下程式,並點擊send notice按鈕,依舊會發出一條通知,讓回下拉系統狀態列,點擊下該通知,就會看到NotificationActivity這個活動介面了。如下所示:
注意看,你會發現系統狀態上面的表徵圖還沒有消失,這是為什麼了?這是因為,如果我們沒有在代碼中對該通知進行取消,它會一直在系統的狀態列上顯示。解決的方法也比較簡單的,調用NotificationManager的cancel()方法就可以取消通知了。修改NotificationActivity中的代碼,如下所示:
package com.jack.notificationtest;import android.app.Activity;import android.app.NotificationManager;import android.content.Context;import android.os.Bundle;public class NotificationActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.notification_layout);NotificationManager manager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);manager.cancel(1);/* * 我們在cancel()方法中傳入了1,這個1是什麼意思了?這是我們給這條通知設定的id就是1。 * 因此如果,你想要取消哪一條通知,就在cancel()方法中傳入該通知的id就行了。 * */}}
通知的進階技巧
觀察Notification這個類,你會發現我們還有很多的屬性,沒有使用。先來看看sound這個屬性吧,它可以在通知發出的時候播出一段音頻,這樣就能夠更好地告知使用者有通知到來。sound這個屬性是一個Uri對象,所以在指定音頻檔案的時候還需要先擷取音頻檔案對應的URI。比如說手機的/system/media/audio/ringtongs目錄下有一個basic_tone.ogg音頻檔案,那麼在代碼中這樣就可以這樣指定:
Uri soundUri=Uri.parse(new File("/system/media/audio/ringtongs/basic_tone.ogg"));
notification.sound=soundUri;
除允許播放音頻以外,我們還可以在通知到來的時候讓手機進行震動,使用的是vibrate這個屬性。它是一個長整形的數組,由於設定手機靜止和震動的時間長度,以毫秒為單位。下標0的值表示手機靜止的時間長度,下標為1的值表示手機震動的時間長度,下標為2的值又表示手機靜止的時間長度,以此類推。所以,如果想要手機在通知到來的時候立刻震動1秒,然後靜止1秒,在震動,代碼就可以寫成:
long[] vibrates={0,1000,1000,1000};
notification.vibrate=vibrates;
不過想要控制手機震動還需要聲明許可權,因此,我們還得編輯AndroidManifest.xml檔案,加入如下說明:
<uses-permission android.name="android.permission.VIBRATE"/>
學會控制通知的聲音和震動,下面我們來看看如何在通知到來的時候控制手機LED燈的顯示。
現在的手機基本上都會前置一個LED燈,當有未接電話或未讀簡訊,而此時手機又處於鎖屏狀態時LED燈就會不停地閃爍,提醒使用者去查看。我們可以使用ledARGB,ledOnMS,ledOffMS以及flags這幾個屬性來實現這種效果。ledARGB用於控制LED燈的顏色,一般有紅綠藍三種顏色可選。ledOnMS用於指定LED燈亮起的時間長度,以毫秒為單位。edOffMS由於指定LED燈暗去的時間長度,也是以毫秒為單位。flags可用於指定通知的一些行為,其中就包括顯示LED燈這一選項。所以,等通知到來時,如果想要實現LED燈以綠色的燈一閃一閃的效果,就可以寫成:
notification.ledARGB=Color.GREEN;
notification.ledOnMS=1000;
notification.ledOffMS=1000;
notification.flags=Notification.FLAG_SHOW_LIGHTS;
當然,如果你不想進行那麼多繁瑣的設定,也可以直接使用通知的預設效果,它會根據當前手機的環境來決定播放什麼鈴聲,以及如何震動,寫法如下:
notification.defaults=Notification.DEFAULT_ALL;
注意:以上所涉及的這些進階技巧都要在真機上面運行,才能看到效果,模擬器無法表現出震動,以及LED燈閃爍等功能。
通知就總結到這了額,下面一篇將進行簡訊的接收與發送總結。
轉載請註明:http://blog.csdn.net/j903829182/article/details/41181277
android學習十三(android的通知使用)