Android中通知欄Notification詳解以及自定義Notification

手機通知欄中的顯示效果


一 直接使用Notification

其相關屬性:

audioStreamType 當聲音響起時,所用的音頻流的類型 
contentIntent 當通知條目被點擊,就執行這個被設置的Intent. 
contentView 當通知被顯示在狀態條上的時候,同時這個被設置的視圖被顯示. 
defaults 指定哪個值要被設置成默認的. 
deleteIntent 當用戶點擊"Clear All Notifications"按鈕區刪除所有的通知的時候,這個被設置的Intent被執行. 
icon 狀態條所用的圖片. 
iconLevel 假如狀態條的圖片有幾個級別,就設置這裏. 
ledARGB LED燈的顏色. 
ledOffMS LED關閉時的閃光時間(以毫秒計算) 
ledOnMS LED開始時的閃光時間(以毫秒計算) 
number 這個通知代表事件的號碼 
sound 通知的聲音 
tickerText 通知被顯示在狀態條時,所顯示的信息 
vibrate 振動模式. 
when 通知的時間戳. 

1.默認聲音

   notification.defaults |= Notification.DEFAULT_SOUND; 
   如果要使用自定義聲音,那麼就要用到sound了。如下: 
   soundnotification.sound=Uri.parse("file:///sdcard/notification/apple.mp3"); 
   需要注意一點,如果default、sound同時出現,那麼sound無效,會使用默認鈴聲。 
2.震動

  如果是使用默認的振動方式,那麼同樣也是使用default。 
  notification.defaults|=Notification.DEFAULT_VIBRATE; 
  自己定義振動形式,這邊需要用到Long型數組
  long[]vibrate={0,100,200,300}; 
  notification.vibrate=vibrate; 
  這邊的Long型數組中,第一個參數是開始振動前等待的時間,第二個參數是第一次振動的時間,第三個參數是第二     次振動的時間,以此類推,隨便定義多長的數組。但是採用這種方法,沒有辦法做到重複振動。 
   同樣,如果default、vibrate同時出現時,會採用默認形式。

【說明】:加入手機震動,一定要在manifest.xml中加入權限:
<uses-permission android:name="android.permission.VIBRATE" />
3.閃光

  使用默認的燈光
   notification.defaults|=Notification.DEFAULT_LIGHTS; 
   自定義閃光
   notification.ledARGB=0xff00ff00; 
   notification.ledOnMS=300; 
   notification.ledOffMS=1000; 
   notification.flags|=Notification.FLAG_SHOW_LIGHTS; 
   其中ledARGB表示燈光顏色、ledOnMS亮持續時間、ledOffMS暗的時間。 
   【說明】:這邊的顏色跟設備有關,不是所有的顏色都可以,要看具體設備。
    DEFAULT_ALL    使用所有默認值,比如聲音,震動,閃屏等等
4.NotificationManager常用方法介紹:
public void cancelAll() 移除所有通知(只是針對當前Context下的Notification)
public  void cancel(int id) 移除標記爲id的通知 (只是針對當前Context下的所有Notification)
public  void notify(String tag ,int id, Notification notification) 將通知加入狀態欄,標籤爲tag,標記爲id
public  void notify(int id, Notification notification) 將通知加入狀態欄,標記爲id

5.系統默認樣式通知代碼

protected void setNotification() {
		// TODO Auto-generated method stub
		id++;
		Notification notification=new Notification();
		notification.icon=R.drawable.a;//通知圖標
		notification.tickerText="來了一條消息";//狀態欄顯示的通知文本提示
		notification.when=System.currentTimeMillis();//通知產生的時間,會在通知信息裏顯示
		notification.flags=Notification.FLAG_AUTO_CANCEL;//點擊後消失
	    notification.flags |= Notification.FLAG_SHOW_LIGHTS; //LED燈閃爍
        notification.defaults |= Notification.DEFAULT_LIGHTS; 
        notification.defaults |= Notification.DEFAULT_SOUND; //發出提示音
        notification.defaults |= Notification.DEFAULT_VIBRATE;

		Intent intent=new Intent(this,ShowMainActivity.class);
		PendingIntent pendingIntent=PendingIntent.getActivity(this,
				0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		notification.setLatestEventInfo(this, "contentTitle", "contentText", pendingIntent);
		NotificationManager manager=(NotificationManager) getSystemService(getApplicationContext().NOTIFICATION_SERVICE);
		manager.notify(id, notification);
	}

1>在設置圖標時,ic_launcher.png(隨意自己的圖標,只是名字ic_launcher.png)手機頂部狀態欄提示的圖標和通知欄的圖標顯示的就是這個ic_launcher.png,圖標是一致的,大部分應用也是一致處理的;倘若手機頂部狀態欄提示圖標和通知欄的圖標不想一樣,那麼如上代碼notification.icon=R.drawable.a;這裏是狀態欄的提示圖標,通知欄中顯示的是系統默認的ic_launcher.png,想換什麼圖標都行,名字不能改,同時drawable下的都要更換,小米手機上是這樣,但是在酷派上提示圖標和通知欄圖標顯示的都是notification.icon=R.drawable.ic_launcher;不知道爲什麼;

2>manager.notify(id, notification);若id設置爲個常量,那麼通知欄只顯示一條通知,若要每條都顯示,定義static int id=0;進行id++;最後賦id到此方法中,如上代碼;

3>PendingIntent是表示一個在將來某個待定的時刻發生,而Intent是立刻發生;

    PendingIntent支出三種特定的意圖:啓動Activity,啓動Service,發生廣播,對應方法如下:

PendingIntent pendingIntent=PendingIntent.getActivity(context, requestCode, intent, flags);

PendingIntent pendingIntent=PendingIntent.getService(context, requestCode, intent, flags);

PendingIntent pendingIntent=PendingIntent.getService(context, requestCode, intent, flags);

二.使用RemoteViews自定義樣式通知

//自定義通知
	protected void setSelfNotification() {
		// TODO Auto-generated method stub
		sid++;
		Notification notification=new Notification();
		notification.icon=R.drawable.b;//通知圖標
		notification.tickerText="來了一條消息";//狀態欄顯示的通知文本提示
		notification.when=System.currentTimeMillis();//通知產生的時間,會在通知信息裏顯示
		notification.flags=Notification.FLAG_AUTO_CANCEL;//點擊後消失
		notification.flags |= Notification.FLAG_SHOW_LIGHTS; //LED燈閃爍
		notification.defaults |= Notification.DEFAULT_LIGHTS; 
		notification.defaults |= Notification.DEFAULT_SOUND; //發出提示音
		notification.defaults |= Notification.DEFAULT_VIBRATE;

		Intent intent=new Intent(this,ShowMainActivity.class);
		PendingIntent pendingIntent=PendingIntent.getActivity(this,
				0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		
		RemoteViews remoteViews=new RemoteViews(getPackageName(), R.layout.remote_view);
		remoteViews.setTextViewText(R.id.tv_title, "火影");
		remoteViews.setTextViewText(R.id.tv_content, "鳴人大戰佩恩");
		remoteViews.setImageViewResource(R.id.icon, R.drawable.b);
		Intent intent2=new Intent(this,ShowTwoMainActivity.class);
		//remoteViews的意圖
		PendingIntent pendingIntent2=PendingIntent.getActivity(
				this, 0, intent2, PendingIntent.FLAG_UPDATE_CURRENT);
		//給我remoteViews上的控件tv_content添加監聽事件
		remoteViews.setOnClickPendingIntent(R.id.tv_content, pendingIntent2);
		
		notification.contentView=remoteViews;
		notification.contentIntent=pendingIntent;
		NotificationManager manager=(NotificationManager)
				getSystemService(getApplicationContext().NOTIFICATION_SERVICE);
		manager.notify(sid, notification);
		
	}

點擊通知欄會調轉到helloworld界面,點擊通知內容會跳轉另一個showtwo界面,意思就是這裏可以設置自定義佈局中控件的監聽事件,通過remoteViews.setOnClickPendingIntent(R.id.tv_content, pendingIntent2);

三 使用Notification.Builder

     這是3.0中引入的,簡化了配置Notification的標誌,選項,內容,佈局過程

protected void setNotificationBuilder() {
		// TODO Auto-generated method stub
		id++;
		Notification.Builder builder=new Notification.Builder(this);
		builder.setSmallIcon(R.drawable.ic_launcher)
		.setTicker("大蛇丸")
		.setWhen(System.currentTimeMillis())
		.setContentTitle("鳴人")
		.setContentText("分身術")
		.setContentInfo("火影")
		.setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_VIBRATE)
		.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
		.setVibrate(new long[]{1000,1000,1000,1000})
		.setLights(Color.RED, 0, 1);
		Notification notification=builder.build();
		NotificationManager manager=(NotificationManager) getSystemService(getApplicationContext().NOTIFICATION_SERVICE);
		manager.notify(id, notification);
	}

四 帶有進度條的通知

	protected void setNotificationProgrss() {
		// TODO Auto-generated method stub
		id++;
		Intent intent=new Intent(this,ShowMainActivity.class);
		PendingIntent pendingIntent=PendingIntent.getActivity(this,
				0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		Notification.Builder builder=new Notification.Builder(this);
		builder.setSmallIcon(R.drawable.ic_launcher)
		.setTicker("大蛇丸")
		.setWhen(System.currentTimeMillis())
		.setContentTitle("卡卡西")
		.setContentText("卡卡西和帶土")
		.setContentInfo("火影")
		.setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_VIBRATE)
		.setProgress(100, 50, false)
		.setContentIntent(pendingIntent);
		Notification notification=builder.build();
		NotificationManager manager=(NotificationManager) getSystemService(getApplicationContext().NOTIFICATION_SERVICE);
		manager.notify(id, notification);
	}


代碼下載


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章