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

來源:互聯網
上載者:User

標籤: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了。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.