zoukankan      html  css  js  c++  java
  • Android 编程下的持久通知

    NotificationManager 在执行 notify(int id, Notification notification) 方法时,有 2 个细节要注意,因为这两个细节可以实现类似墨迹天气在接收到消息推送后发出的常驻通知,这样的通知一直在状态栏显示而不会消失,当接收到新的消息推送后,发出的新通知中所携带的内容会更新状态栏的常驻通知,这样会带给用户比较良好的体验,用户只需要下拉查看状态栏就能很快捷的看到实时的天气情况,而不需要去打开墨迹天气这个软件。下面来看 API 对这个方法的介绍:

    void android.app.NotificationManager.notify(int id, Notification notification)

    Post a notification to be shown in the status bar. If a notification with the same id has already been posted by your application and has  not yet been canceled, it will be replaced by the updated information.

    Parameters:

    id An identifier for this notification unique within your application. 

    notification A Notification object describing what to show the user. Must not be null. 

    void android.app.NotificationManager.notify(int id, Notification notification)

    发送一个显示在状态栏的通知。如果一个新的通知携带的 ID 和一个已经发送到 你手机而没有被取消的通知所携带的 ID 相同,那么新的通知将会更新旧通知的内容。

    参数:

    id 当前通知与应用关联的唯一标识。

    notification 一个通知对象的描述,用来展示给用户,不能为空。

    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    应用场景:墨迹天气

    应用效果:在接收到新的通知时,状态栏下始终保持这个应用的一个通知被显示,并在接收到新通知时来更新当前通知的内容。

     

    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    代码实现

    1. 通过设置 Notification.FLAG_NO_CLEAR 实现通知查看后不被删除

    notification.flags |= Notification.FLAG_AUTO_CANCEL;        // 通知被查看后自动删除
    notification.flags |= Notification.FLAG_ONGOING_EVENT;      // 将通知放到通知栏的 "Ongoing" 即 "正在运行" 组中
    notification.flags |= Notification.FLAG_NO_CLEAR;           // 通知被查看后保持显示,经常与 FLAG_ONGOING_EVENT 一起使用

    2. 通过设置 ID 为常量来实现新通知更新之前未删除的通知内容

    notificationManager.notify(random.nextInt(), notification);    // 显示多条通知
    notificationManager.notify(100, notification);                 // 显示单条通知
  • 相关阅读:
    APS.NET MVC + EF (14)---项目框架搭建
    APS.NET MVC + EF (11)---过滤器
    APS.NET MVC + EF (10)---使用AJAX
    APS.NET MVC + EF (06)---模型
    APS.NET MVC + EF (08)---数据注解和验证
    APS.NET MVC + EF (07)---表单和HTML辅助方法
    APS.NET MVC + EF (05)---控制器
    错误 1 error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
    VS2010中项目配置引入GDAL
    Win7(32/64)VS2010配置编译GDAL环境(图文教程+亲测可用!)
  • 原文地址:https://www.cnblogs.com/sunzn/p/2871969.html
Copyright © 2011-2022 走看看