Android官方开发文档Training系列课程中文版:通知用户之大视图通知
原文地址:http://android.xsoftlab.net/training/notify-user/expanded.html#big-view
通知在通知欄中以兩種風格呈現:正常視圖與大視圖。只有在通知展開的時候才會展示大視圖。這只有在通知處于通知欄頂部時或者用戶點擊了通知時才會出現。
大視圖于Android 4.1開始出現,并且不支持老版本。這節課將會介紹如何使用大視圖通知。
這是正常視圖的示例:
下面是大視圖的示例:
這節課所展示的示例程序都以正常視圖和大視圖兩種方式為用戶提供相同的功能:
- 可以延遲提醒或者取消通知。
- 以一種方式展示提醒文本給用戶。
正常視圖以Activity的形式提供了以上功能。要在設計通知時記住這一點:首先在正常視圖中提供各種功能,因為這樣可以有很多用戶與通知產生交互。
設置通知啟動Activity
示例應用程序使用IntentService的子類PingService來構造并發布通知。
在下面的代碼段中,IntentService的方法onHandleIntent()指明了一個新的Activity會在用戶點擊通知的時候啟動。setContentIntent()方法中設置了在用戶點擊通知時被發布的PendingIntent,因此可以啟動Activity。
Intent resultIntent = new Intent(this, ResultActivity.class); resultIntent.putExtra(CommonConstants.EXTRA_MESSAGE, msg); resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);// Because clicking the notification launches a new ("special") activity, // there's no need to create an artificial back stack. PendingIntent resultPendingIntent =PendingIntent.getActivity(this,0,resultIntent,PendingIntent.FLAG_UPDATE_CURRENT ); // This sets the pending intent that should be fired when the user clicks the // notification. Clicking the notification launches a new activity. builder.setContentIntent(resultPendingIntent);構造大視圖通知
下面代碼中展示了如何在大視圖通知中設置一個按鈕:
// Sets up the Snooze and Dismiss action buttons that will appear in the // big view of the notification. Intent dismissIntent = new Intent(this, PingService.class); dismissIntent.setAction(CommonConstants.ACTION_DISMISS); PendingIntent piDismiss = PendingIntent.getService(this, 0, dismissIntent, 0); Intent snoozeIntent = new Intent(this, PingService.class); snoozeIntent.setAction(CommonConstants.ACTION_SNOOZE); PendingIntent piSnooze = PendingIntent.getService(this, 0, snoozeIntent, 0);下面的片段展示了如何構造Builder對象。它設置了大視圖的風格為”big text”,并設置了提醒消息的文本。它還使用了addAction()方法來添加Snooze按鈕及Dismiss按鈕,這兩個按鈕將會出現在大視圖通知上:
// Constructs the Builder object. NotificationCompat.Builder builder =new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_stat_notification).setContentTitle(getString(R.string.notification)).setContentText(getString(R.string.ping)).setDefaults(Notification.DEFAULT_ALL) // requires VIBRATE permission/** Sets the big view "big text" style and supplies the* text (the user's reminder message) that will be displayed* in the detail area of the expanded notification.* These calls are ignored by the support library for* pre-4.1 devices.*/.setStyle(new NotificationCompat.BigTextStyle().bigText(msg)).addAction (R.drawable.ic_stat_dismiss,getString(R.string.dismiss), piDismiss).addAction (R.drawable.ic_stat_snooze,getString(R.string.snooze), piSnooze);總結
以上是生活随笔為你收集整理的Android官方开发文档Training系列课程中文版:通知用户之大视图通知的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android官方开发文档Trainin
- 下一篇: shell脚本实现命令的自动执行