標籤:
安卓中建立通知需要藉助NotificationManager來對通知進行管理。可以調用Context的getsSystemService()方法來獲得。 getsSystemService()方法接收一個參數,這個參數是字串,用於指定哪一個服務。Context.NOTIFICATION_SERVICE 就是指定通知服務。 這個方法返回一個Object對象,所欲需要進行強制轉換。
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
1.安卓API Level 11 以下
Notification notification = new Notification(R.drawable.ic_launcher, "This is ticker text", System.currentTimeMillis());
安卓文檔中寫了 Notification(int icon, CharSequence tickerText, long when) This constructor was deprecated in API level 11. Use Notification.Builder instead.
也就是說在安卓API Level 11 以上這種方法建立Notification已經無效了。
2.高於安卓API Level 11 低於 API Level 16
用Notification.Builder來構建,可以用getNotification()來擷取Notification對象。不過文檔中寫了getNotification() This method was deprecated in API level 16. Use build() instead.
也就是說getNotification()方法獲得Notification對象在安卓API Level 16以上已經無效。
3.高於安卓API Level 16
使用Notification.Builder來構建,調用Builder的build()來擷取對象。
Notification.Builder有很多方法來構建通知的布局。這裡給出一段代碼:
package com.example.notificationtest;import java.io.File;import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity implements OnClickListener{ private Button sendNotice; private int num; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); num = 0; setContentView(R.layout.activity_main); sendNotice = (Button) findViewById(R.id.send_notice); sendNotice.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.send_notice: Intent intent = new Intent(MainActivity.this,NotificationActivity.class); intent.putExtra("id", ++num); //跳轉的Intent 第一個參數Context 第二個參數文檔解釋Private request code for the sender //第三個參數intent 第四個參數flags PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); //調用提示音 Uri soundUri = Uri.fromFile(new File("/system/media/audio/ringtones/01_Piano.ogg")); //調用震動 是一個long型的數組第0個表示幾秒後震動 第1個數震動多久 第2個數等待多久 依次類推 long[] vibrates = {0,100}; NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Notification notification = new Notification.Builder(this) .setDefaults(Notification.DEFAULT_ALL) //通知預設的聲音 震動 呼吸燈 .setTicker("This is Ticker") //狀態列顯示的文字 .setContentTitle("This is title") //設定下拉欄中通知的第一行 .setContentText("This is Text") //設定下拉欄中通知的第二行 .setSubText("This is SubText") //設定下拉欄中通知的第三行 .setSmallIcon(R.drawable.ic_launcher) //設定狀態列的小表徵圖 //.setLargeIcon(b) //設定下拉欄中的大表徵圖 .setContentIntent(pi) //跳轉的Intent .setSound(soundUri) //設定通知音效 .setVibrate(vibrates) //設定震動效果 .build(); //構建Notification manager.notify(num,notification); //顯示notification 第一個參數表示id值 break; default: break; } }}
震動需要在AndroidManifest.xml中加入聲明 :
<uses-permission android:name="android.permission.VIBRATE" />
我們通過通知進入到另外一個活動中時,需要將通知從狀態列上除去。可以在啟動的活動中調用NotificationManager的cancel()方法,接收1個參數,即該通知的id值。
- PendingIntent.getActivity()方法第四個參數值:
FLAG_UPDATE_CURRENT:如果PendingIntent已經存在,保留它並且只替換它的extra資料。會更新之前PendingIntent的訊息,比如,你推送了訊息1,並在其中的Intent中putExtra了一個值“ABC”,在未點擊該訊息前,繼續推送第二條訊息,並在其中的Intent中putExtra了一個值“CBA”,好了,這時候,如果你單擊訊息1或者訊息2,你會發現,他倆個的Intent中讀取過來的資訊都是“CBA”,就是說,第二個替換了第一個的內容。
FLAG_CANCEL_CURRENT時:如果PendingIntent已經存在,那麼當前的PendingIntent會取消掉,然後產生一個新的PendingIntent依然是上面的操作步驟,這時候會發現,點擊訊息1時,沒反應,第二條可以點擊。
FLAG_ONE_SHOT:PendingIntent只能使用一次。調用了執行個體方法send()之後,它會被自動cancel掉,再次調用send()方法將失敗。
FLAG_NO_CREATE:如果PendingIntent不存在,簡單了當返回null。
到這裡學習了安卓通知的一般用法。
安卓學習之通知(Notification)