1. 程式人生 > >關於Notification的使用和引數詳解

關於Notification的使用和引數詳解

今天在做關於Notification通知欄的開發,發現總是有這樣或者那樣的問題,最後下決心尋找關於Notification引數的一些設定。
Notification我們可以理解為有兩種:
1、使用系統預設的樣式;2、自定義;


像這種系統提供的東西大家都能理解:預設的不好看,但是簡單方便;自定義的,好看,但容易出問題。但是基本上所有的專案都會使用自定義的東西,所有我們要好好的檢視下我們所要設定的引數們。


1、使用系統預設的樣式步驟及引數詳解:


// 建立一個NotificationManager的引用,就是從系統中獲取到它的例項


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


		// 如果你不是在acitivity中或者是service中你可以在getSystemService之前加上context物件


		// NotificationManager mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);


		// 初始化Notification


		Notification notification = new Notification(R.drawable.icon,
				"Hello,there!", System.currentTimeMillis());
		
		/*
		 * 定義Notification的各種屬性
		 * 
		 * R.drawable.icon; //通知圖示
		 * 
		 * "Hello,there!"; //狀態列顯示的通知文字提示,就是在頂部顯示一下的文字
		 * 
		 * System.currentTimeMillis(); //(獲取系統時間)通知產生的時間,會在通知資訊裡顯示
		 */


		// 使用提示聲音進行通知使用者


		notification.defaults = Notification.DEFAULT_SOUND;


		/*
		 * 也可以使用uri讓使用者選擇鈴音
		 * 
		 * notification.sound = Uri.parse("file:///sdcard/XXXX.mp3");
		 * 
		 * notification.sound =
		 * Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
		 * 
		 * 如果想要讓聲音持續重複直到使用者對通知做出反應,則可以在notification的flags欄位增加"FLAG_INSISTENT"
		 * 
		 * 如果notification的defaults欄位包括了"DEFAULT_SOUND"屬性,則這個屬性將覆蓋sound欄位中定義的聲音
		 */


		// 新增振動提示功能
		notification.defaults |= Notification.DEFAULT_VIBRATE;


		/*
		 * 或者可以定義自己的振動模式:
		 * 
		 * long[] vibrate = {0,100,200,300};
		 * //0毫秒後開始振動,振動100毫秒後停止,再過200毫秒後再次振動300毫秒
		 * 
		 * notification.vibrate = vibrate;
		 * 
		 * long陣列可以定義成想要的任何長度
		 * 
		 * 如果notification的defaults欄位包括了"DEFAULT_VIBRATE",則這個屬性將覆蓋vibrate欄位中定義的振動
		 */


		// 新增LED燈提醒
		notification.defaults |= Notification.DEFAULT_LIGHTS;


		/*
		 * 或者可以自己的LED提醒模式:
		 * 
		 * notification.ledARGB = 0xff00ff00;
		 * 
		 * notification.ledOnMS = 300; //亮的時間
		 * 
		 * notification.ledOffMS = 1000; //滅的時間
		 * 
		 * notification.flags |= Notification.FLAG_SHOW_LIGHTS;
		 */


		////////////////////////////////////////////關鍵部分/////////////////////////////////////////////////////
		/*
		 * 
		 * 更多的特徵屬性
		 * 
		 * notification.flags |= FLAG_AUTO_CANCEL; //在通知欄上點選此通知後自動清除此通知
		 * 
		 * notification.flags |= FLAG_INSISTENT; //重複發出聲音,直到使用者響應此通知
		 * 
		 * notification.flags |= FLAG_ONGOING_EVENT;
		 * //將此通知放到通知欄的"Ongoing"即"正在執行"組中
		 * 
		 * notification.flags |= FLAG_NO_CLEAR; //表明在點選了通知欄中的"清除通知"後,此通知不清除,
		 * 
		 * //經常與FLAG_ONGOING_EVENT一起使用
		 * 
		 * notification.number = 1; //number欄位表示此通知代表的當前事件數量,它將覆蓋在狀態列圖示的頂部
		 * 
		 * //如果要使用此欄位,必須從1開始
		 * 
		 * notification.iconLevel = ; //
		 */
		
		// 設定通知欄下拉選單中,點選通知後響應的事件訊息
		// 這裡是使用intent進行跳轉,點選後進入你想要進入的頁面;當然在這裡你也可以做一些其他的事情;
		Intent notificationIntent = new Intent(this, MainActivity.class); // 點選該通知後要跳轉的Activity


		// 初始化PendingIntent物件,用來
		PendingIntent contentIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0);


		//這裡顯示的是下拉選單後的佈局
		notification.setLatestEventInfo(this, "天氣預報", "晴空萬里", contentIntent);


		// 把Notification傳遞給NotificationManager
		mNotificationManager.notify(0, notification);


當然上面的所有屬性有一部分你可以選擇在某些事件之後再設定,比如說:當下載完成後你要給它設定,點選後訊息在通知欄中消失,這樣就可以新增以下程式碼:
notification.flags = Notification.FLAG_AUTO_CANCEL;


當你點選了這條下拉選單欄中的訊息之後,該條訊息就會消失;



2、自定義
既然是自定義,當然要你自己寫佈局檔案了,那麼怎麼在notification中引入該佈局檔案呢?使用RemoteViews類;
要定義自己的擴充套件訊息,首先要初始化一個RemoteViews物件,然後將它傳遞給Notification的contentView欄位,再把PendingIntent傳遞給contentIntent欄位。
佈局檔案:(content_view.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:background="#00000000"
    android:orientation="vertical" 
    android:padding="5dp">


    <ImageView 
        android:id="@+id/content_view_image"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:src="@drawable/test"
        
        />
    <TextView
        android:id="@+id/content_view_text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0%"
        android:textColor="#000000"
        android:layout_toRightOf="@id/content_view_image"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="15dp"
      />
	<ProgressBar 
	android:id="@+id/content_view_progress"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:max="100"
        android:layout_below="@id/content_view_image"
        android:layout_marginTop="4dp"
	    />
	
</RelativeLayout>


接下來的步驟就是:1、初始化notification;2、給notification新增檢視;3、給檢視中的控制元件賦值;4、點選訊息所觸發的事件;5、顯示該條通知訊息;
步驟:1和2:
初始化和上面一樣,這裡關鍵就說下新增檢視吧!
updateNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
		notification = new Notification(R.drawable.test, "紫陽天氣開始下載",System.currentTimeMillis());
		notification.contentView = new RemoteViews(mContext.getPackageName(), R.layout.content_view);  
		updatePendingIntent = PendingIntent.getActivity(mContext,0, new Intent(mContext, MainActivity.class), 0);
		notification.contentIntent=updatePendingIntent;

這句:
notification.contentView = new RemoteViews(mContext.getPackageName(), R.layout.content_view); 

就相當於activity中的setContentView()方法吧!這裡一定要注意一點:當你使用了
notification.contentView
就一定要使用:
notification.contentIntent

否則會報:android.app.RemoteServiceException: Bad notification posted from package
步驟:3

notification.contentView.setTextViewText(R.id.content_view_text1, "進度為:"+length+"%");
		notification.contentView.setProgressBar(R.id.content_view_progress, 100, length, false); 

步驟4:
updatePendingIntent
就是你點選事件所觸發的事件;這個和上面使用系統預設的一樣,不做詳細介紹了!

步驟5:
最後:
// 把Notification傳遞給

NotificationManagermNotificationManager.notify(0, notification);

完成顯示;
到這裡就說完了,寫的不好,大家不要噴啊!