Android官方开发文档Training系列课程中文版:通知用户之大视图通知

原文地址:http://android.xsoftlab.net/training/notify-user/expanded.html#big-view

通知在通知栏中以两种风格呈现:正常视图与大视图。只有在通知展开的时候才会展示大视图。这只有在通知处于通知栏顶部时或者用户点击了通知时才会出现。

大视图于Android 4.1开始出现,并且不支持老版本。这节课将会介绍如何使用大视图通知。

这是正常视图的示例:

下面是大视图的示例:

http://android.xsoftlab.net/images/training/notifications-bigview.png

这节课所展示的示例程序都以正常视图和大视图两种方式为用户提供相同的功能:

  • 可以延迟提醒或者取消通知。
  • 以一种方式展示提醒文本给用户。

正常视图以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);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值