W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
編寫:fastcome1985 - 原文:http://developer.android.com/training/notify-user/expanded.html
Notification抽屜中的Notification主要有兩種視覺展示形式,normal view(平常的視圖,下同) 與 big view(大視圖,下同)。Notification的 big view樣式只有當Notification被擴展時才能出現(xiàn)。當Notification在Notification抽屜的最上方或者用戶點擊Notification時才會展現(xiàn)大視圖。
Big views在Android4.1被引進的,它不支持老版本設備。這節(jié)課叫你如何讓把big view notifications合并進你的APP,同時提供normal view的全部功能。更多信息請見Notifications API guide 。
這是一個 normal view的例子
圖1 Normal view notification.
這是一個 big view的例子
圖2 Big view notification.
在這節(jié)課的例子應用中, normal view 與 big view給用戶相同的功能:
這個例子應用用IntentService的子類(PingService)來構(gòu)造以及發(fā)布notification。 在這個代碼片段中,IntentService中的方法onHandleIntent()) 指定了當用戶點擊notification時啟動一個新的activity。方法setContentIntent())定義了pending intent在用戶點擊notification時被激發(fā),因此登陸這個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);
這個代碼片段展示了如何在big view中設置buttons
// 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);
這個代碼片段展示了如何構(gòu)造一個Builder對象,它設置了big view 的樣式為"big text",同時設置了它的內(nèi)容為提醒文字。它使用addAction())方法來添加將要在big view中出現(xiàn)的Snooze與Dismiss按鈕(以及它們相關(guān)聯(lián)的pending intents).
// 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);
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: