博客
关于我
Android Notification 食用指北
阅读量:793 次
发布时间:2019-03-25

本文共 2729 字,大约阅读时间需要 9 分钟。

Notification 的基本使用与扩展功能

Notification 的创建与使用

在 Android 开发中,Notification 是一种强大的 UI 组件,能够将应用的状态信息反馈给用户。通过发送 Notification,可以在系统的通知栏中显示图标提示,用户可以通过拉下来信息查看详情。

创建基本 Notification

要创建一个基本的 Notification,主要需要设置以下属性:

  • 小图标:通过 setSmallIcon() 方法设置。
  • 标题:通过 setContentTitle() 方法设置。
  • 内容:通过 setContentText() 方法设置。
    宏观来看,这三个属性是核心配置,缺一不可。
  • 此外,为了让 Notification 与用户交互,可以通过设置 Action 来实现跳转指定页面、启动服务或发送广播。可以通过 setContentIntent() 方法设置 Pending Intent,用来指定点击后要执行的操作。

    此外,Notification 对象可以通过 setDefaults() 方法设置一些默认属性,如震动、响铃等效果。但需要注意,一旦设置默认属性,自定义效果将失效。

    Notification 的扩展功能

    设置大图标

    Notification 中,大图标可以通过 setLargeIcon() 方法设置。如果同时设置小图标和大图标,小图标会显示在右侧,largeIcon 则会放在左侧。

    以下是代码示例:

    Resources res = getResources();NotificationCompat.Builder builder = new NotificationCompat.Builder(this)    .setSmallIcon(R.drawable.small_icon)    .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable_large_icon))    .setContentTitle("标题")    .setContentText("内容");Notification notification = builder.build();mNotificationManager.notify(TAG, 0, notification);

    与用户交互

    为了使 Notification具备交互能力,需要设置相应的 Pending Intent。例如,要启动 MainActivity,可以这样做:

    PendingIntent mainPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class),     PendingIntent.FLAG_UPDATE_CURRENT);Notification notification = ...    .setContentIntent(mainPendingIntent);

    clicking the notification 将会以 mainPendingIntent 为目标执行。

    一些特殊效果

    如果需要让 Notification 有一些特殊效果,可以设置以下 Flags:

    • FLAG_NO_CLEAR:设置后,该 Notification 无法被系统清除。
    • FLAG_AUTO_CANCEL:设置后,用户点击通知后通知会自动消失。
    • FLAG_ONGOING_EVENT:表示该通知与当前正在运行的 Foreground Service 有关,无法手动清除。
    • FLAG_FOREGROUND_SERVICE:如果您的应用正在以 Foreground Service 的形式运行,设置上述 Flag,可以更好地与用户交互。

    此外,想让 Notification 有震动或响铃效果,可以通过 setVibrate()setDefaultSound() 等方法设置。

    严重的是,设置默认效果时(如 Notification.DEFAULT_VIBRATENotification.DEFAULT_SOUND),自定义设置将失效。

    进度条 Notification

    如果需要在 Notification 中显示进度条,可以这样做:

    private void showProgressBarNotification() {    count++;    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)        .setSmallIcon(R.mipmap.ic_launcher)        .setContentTitle("带进度条的通知")        .setContentText("进度: " + count + "/" + 100)        .setProgress(100, count, false);    mNotification = builder.build();    mNotificationManager.notify(TAG, 0, mNotification);}

    与此同时,可以使用 Handler 定时更新进度条:

    final Runnable updateProgress = new Runnable() {    @Override    public void run() {        showProgressBarNotification();        if (count < 100) {            postDelayed(updateProgress, 200);        }    }};

    最终,可以通过点击动画效果取自应用自身资源。

    此外,需要注意的是,通知栏的清除按钮可以清除任何可清除 notifications。如果要自定义清除行为,可以设置 setAutoCancel(true),然后单击通知即可清除。

    总结

    通过以上方法,可以非常方便地创建和定制 Notification。无论是单纯的信息通知,还是带有进度条/图片或与用户交互的通知,都可以通过 NotificationCompat.Builder readily配置。当然,在实际开发中,还需要考虑权限申请(如震动权限)以及兼容不同的安卓版本的问题。

    转载地址:http://zgauk.baihongyu.com/

    你可能感兴趣的文章
    mysqlbinlog报错unknown variable ‘default-character-set=utf8mb4‘
    查看>>
    mysqldump 参数--lock-tables浅析
    查看>>
    mysqldump 导出中文乱码
    查看>>
    mysqldump 导出数据库中每张表的前n条
    查看>>
    mysqldump: Got error: 1044: Access denied for user ‘xx’@’xx’ to database ‘xx’ when using LOCK TABLES
    查看>>
    Mysqldump参数大全(参数来源于mysql5.5.19源码)
    查看>>
    mysqldump备份时忽略某些表
    查看>>
    mysqldump实现数据备份及灾难恢复
    查看>>
    mysqldump数据库备份无法进行操作只能查询 --single-transaction
    查看>>
    mysqldump的一些用法
    查看>>
    mysqli
    查看>>
    MySQLIntegrityConstraintViolationException异常处理
    查看>>
    mysqlreport分析工具详解
    查看>>
    MySQLSyntaxErrorException: Unknown error 1146和SQLSyntaxErrorException: Unknown error 1146
    查看>>
    Mysql_Postgresql中_geometry数据操作_st_astext_GeomFromEWKT函数_在java中转换geometry的16进制数据---PostgreSQL工作笔记007
    查看>>
    mysql_real_connect 参数注意
    查看>>
    mysql_secure_installation初始化数据库报Access denied
    查看>>
    MySQL_西安11月销售昨日未上架的产品_20161212
    查看>>
    Mysql——深入浅出InnoDB底层原理
    查看>>
    MySQL“被动”性能优化汇总
    查看>>