android 實現自定義狀態欄通知(Status Notification)

在android項目的開發中,有時爲了實現和用戶更好的交互,在通知欄這一小小的旮旯裏,我們通常需要將內容豐富起來,這個時候我們就需要去實現自定義的通知欄,例如下面360或者網易的樣式:

首先我們要了解的是 自定義佈局文件支持的控件類型:Notification的自定義佈局是RemoteViews,因此,它僅支持FrameLayout、LinearLayout、RelativeLayout三種佈局控件,同時支持AnalogClock、Chronometer、Button、ImageButton、ImageView、ProgressBar、TextView、ViewFlipper、ListView、GridView、StackView和AdapterViewFlipper這些UI控件。對於其他不支持的控件,使用時將會拋出ClassNotFoundException異常。

同時呢我們還要了解的是Notification支持的Intent類型(都是PendingIntent類的實例)。

下面就是具體的實現了:在這個通知欄裏 我們放一個進度條

//Get the notification manager
    	String ns = Context.NOTIFICATION_SERVICE;
    	NotificationManager nm = 
    		(NotificationManager)ctx.getSystemService(ns);
    	
    	//Create Notification Object
		int icon = R.drawable.robot;
		CharSequence tickerText = "Hello";
		long when = System.currentTimeMillis();
		
		Notification notification = 
			new Notification(icon, tickerText, when);

		//Set ContentView using setLatestEvenInfo
	    Intent intent = new Intent(Intent.ACTION_VIEW);
	    intent.setData(Uri.parse("http://www.google.com"));
	    PendingIntent pi = PendingIntent.getActivity(ctx, 0, intent, 0);
//	    notification.setLatestEventInfo(ctx, "title", "text", pi);// 使用默認的樣式
	    notification.contentIntent = pi;
	    notification.contentView = new RemoteViews(ctx.getPackageName(),R.layout.noti);
	    //Send notification
		nm.notify(1, notification);

實現的效果如下圖:(右邊爲系統默認的樣式)

           

這只是一個簡單的示例,爲了實現我們自己的效果 我們只需要修改佈局文件就ok了。


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