Notification抽屜中的Notification主要有兩種視覺展示形式,normal view(平常的視圖,下同) 與 big view(大視圖,下同)。Notification的 big view樣式只有當(dāng)Notification被擴(kuò)展時(shí)才能出現(xiàn)。當(dāng)Notification在Notification抽屜的最上方或者用戶點(diǎn)擊Notification時(shí)才會(huì)展現(xiàn)大視圖。
Big views在Android4.1被引進(jìn)的,它不支持老版本設(shè)備。這節(jié)課叫你如何讓把big view notifications合并進(jìn)你的APP,同時(shí)提供normal view的全部功能。更多信息請(qǐng)見Notifications API guide 。
這是一個(gè) normal view的例子
圖1 Normal view notification.
這是一個(gè) big view的例子
圖2 Big view notification.
在這節(jié)課的例子應(yīng)用中, normal view 與 big view給用戶相同的功能:
繼續(xù)小睡或者消除Notification
一個(gè)查看用戶設(shè)置的類似計(jì)時(shí)器的提醒文字的方法,
normal view 通過當(dāng)用戶點(diǎn)擊Notification來啟動(dòng)一個(gè)新的activity的方式提供這些特性,記住當(dāng)你設(shè)計(jì)你的notifications時(shí),首先在normal view 中提供這些功能,因?yàn)楹芏嘤脩魰?huì)與notification交互。
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);
構(gòu)造big view
這個(gè)代碼片段展示了如何在big view中設(shè)置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);
// 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);
更多建議: