標籤:android style blog http java 使用 io 檔案
在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 Objectint 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 notificationnm.notify(1, notification);
實現的效果例如以:(右邊為系統預設的樣式)
這僅僅是一個簡單的示範範例,為了實現我們自己的效果 我們僅僅須要改動布局檔案就ok了。