Android Notifaction 从4.0到7.0

2023-11-16

Android Notifaction 从4.0到7.0

图1 4.0 通知样式

4.4.4

图2 5.0 6.0 通知样式

5.0 6.0

图3 7.0 通知样式

7

兼容

这么多版本,如何兼容,没关系,交给
android.support.v7.app.NotificationCompat

基本用法

    NotificationManager nm = (NotificationManager) ctx.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

通过builder设置notifaction的属性

具体方法见:官方API,国内可用

高级用法

就是这些style

NotificationCompat.BigPictureStyle

NotificationCompat.BigTextStyle

NotificationCompat.InboxStyle

NotificationCompat.MediaStyle

NotificationCompat.DecoratedCustomViewStyle

NotificationCompat.MessagingStyle

小技巧

横幅

两种方式

1.builder.setPriority(Notification.PRIORITY_MAX);

2.builder.setFullScreenIntent(intent,false);

通知无声音、无震动、无LED呼吸灯闪烁

        long[] pattern = {0,0};
        builder.setVibrate(pattern);
        builder.setLights(Color.rgb(0,0,0),0,0);
        builder.setSound(null);

异步下载通知里显示的图片

如果你是用glide图片加载框架,那么恭喜你中奖了,今天我介绍的就是glide,同样的Picasso也有类似的方法,用法基本差不多,就不多介绍了
常用的方法要不就是本地图片,要不就是先下载再显示通知,这里我们也说两种

1.直接显示,异步加载通知区域图片

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        // Inflate and set the layout for the expanded notification view
        RemoteViews expandedView = new RemoteViews(ctx.getPackageName(), R.layout.notification_expanded);
        expandedView.addView(R.id.content_rl, notification.contentView.clone());
        notification.bigContentView = expandedView;
        NotificationTarget notificationTarget = new NotificationTarget(context.getApplicationContext(), expandedView, R.id.expanded_imageView, notification, id);
        Glide.with(context.getApplicationContext()) // safer!
                .load(bigPicture)
                .asBitmap()
                .into(notificationTarget);
    }

布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right|top"
android:orientation="vertical">

<RelativeLayout
    android:id="@+id/content_rl"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"></RelativeLayout>


<ImageView
    android:id="@+id/expanded_imageView"
    android:layout_width="match_parent"
    android:layout_height="152dp"
    android:contentDescription="@string/app_name"
    android:scaleType="centerCrop" />

2.先下载后显示
重写NotificationTarget,再获取到图片后再显示通知

总有些坑在等你跳

1.设置横幅后,锁屏后解开直接触发通知的PendingIntent事件

builder.setFullScreenIntent(intent,false);

源码

/**
     * An intent to launch instead of posting the notification to the status bar.
     * Only for use with extremely high-priority notifications demanding the user's
     * <strong>immediate</strong> attention, such as an incoming phone call or
     * alarm clock that the user has explicitly set to a particular time.
     * If this facility is used for something else, please give the user an option
     * to turn it off and use a normal notification, as this can be extremely
     * disruptive.
     *
     * <p>
     * The system UI may choose to display a heads-up notification, instead of
     * launching this intent, while the user is using the device.
     * </p>
     *
     * @param intent The pending intent to launch.
     * @param highPriority Passing true will cause this notification to be sent
     *          even if other notifications are suppressed.
     *
     * @see Notification#fullScreenIntent
     */
    public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
        mFullScreenIntent = intent;
        setFlag(FLAG_HIGH_PRIORITY, highPriority);
        return this;
    }

大概意思就是:设置setFullScreenIntent后,说明这个通知的优先级跟来电话的优先级一样高,会直接显示给你,建议设置开关让用户可以自由选择
因此建议使用设置通知优先级来显示横幅

2.setContentInfo与setContentText

咋一看,貌似区别不大,实际区别如下:

在7.0以上,setContentInfo表示红框区域,setContentText表示黑框区域

/**
     * Set the large text at the right-hand side of the notification.
     */
    public Builder setContentInfo(CharSequence info) {
        mContentInfo = limitCharSequenceLength(info);
        return this;
    }





/**
     * Set the title (first row) of the notification, in a standard notification.
     */
    public Builder setContentTitle(CharSequence title) {
        mContentTitle = limitCharSequenceLength(title);
        return this;
    }

    /**
     * Set the text (second row) of the notification, in a standard notification.
     */
    public Builder setContentText(CharSequence text) {
        mContentText = limitCharSequenceLength(text);
        return this;
    }

    /**
     * Set the third line of text in the platform notification template.
     * Don't use if you're also using {@link #setProgress(int, int, boolean)};
     * they occupy the same location in the standard template.
     * <br>
     * If the platform does not provide large-format notifications, this method has no effect.
     * The third line of text only appears in expanded view.
     * <br>
     */
    public Builder setSubText(CharSequence text) {
        mSubText = limitCharSequenceLength(text);
        return this;
    }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android Notifaction 从4.0到7.0 的相关文章

  • 这个方法比 Math.random() 更快吗?

    我是一名初学者 目前已经开始开发一款使用粒子群优化算法的 Android 游戏 我现在正在尝试稍微优化我的代码 并且 for 循环中有相当多的 Math random 几乎一直在运行 所以我正在考虑一种方法来绕过并跳过所有 Math ran
  • Flutter 中的 AndroidManifest 中缺少默认通知通道元数据

    我在用firebase messaging 5 0 1软件包来实现推送通知 在 IOS 中一切正常 而在 Android 中 当我的移动应用程序在后台运行时 我收到通知 但它没有导航到相应的屏幕 它只是打开默认屏幕 如何实现到该特定屏幕的导
  • Gradle 构建错误:内存不足

    当我使用 gradle 构建时 它失败并显示以下信息 OpenJDK 64 Bit Server VM warning INFO os commit memory 0x0000000788800000 89128960 0 failed e
  • Android 应用程序在启动时打开应用程序信息屏幕,而不是启动主 Activity

    我不确定这是否是一个问题 但这是我第一次遇到这个问题 我正在开发一个应用程序 当我在进行一些编码后断开应用程序与 Android Studio 和 PC 的连接时 如果我尝试在手机上打开应用程序 它会启动app info屏幕 我们看到强制停
  • 从历史堆栈中删除活动

    我的应用程序在用户第一次运行应用程序时显示注册活动 如下所示 活动启动画面 欢迎来到游戏 注册帐户 ActivitySplashScreenSignUp 很好 填写此信息 ActivityGameMain 游戏主屏幕 因此 当用户单击每个屏
  • 如何在 Android TextView 中使用土耳其语字符,如“ş ç ı ö”?

    我想在 android TextView 中写入 ile 但它没有正确绘制 怎样才能使用这样的字符呢 例如 我将文本视图设置为 ile 它显示为 ile 我怎样才能解决这个问题 尝试以下方法 看看是否有帮助 source http grou
  • Android:我可以创建一个不是矩形的视图/画布吗?圆形的?

    我有一个圆形视图 悬停在主要内容上方 gt 从屏幕出来的 z 轴方向 当有人点击屏幕时 我希望选择主要内容或悬停在上方的视图 当它覆盖主视图时 到目前为止效果很好 我在透明画布上有一个圆形物品 这意味着您可以看到该圆圈之外的背景的所有内容
  • android中根据屏幕尺寸计算图像尺寸

    我正在尝试根据屏幕尺寸计算图像高度和宽度 我从后端获取 5 x 7 尺寸的图像 为了将像素乘以 72 进行转换 我有 360 X 504 尺寸的图像 对于 360 X 504 我的动态透明矩形区域将显示为 1 223 x 1 179 即 8
  • 位图内存不足错误

    我对这个错误有疑问 我从 URL 制作网站图标解析器 我这样做是这样的 public class GrabIconsFromWebPage public static String replaceUrl String url StringB
  • 使用 Android Firebase 堆栈推送通知

    我开发了使用 Firebase 接收推送通知的 Android 应用程序 我的代码基于 Firebase Google 官方文档 https firebase google com docs cloud messaging android
  • 使用 Android Studio 进行调试永远停留在“等待调试器”状态

    UPDATE The supposed重复是一个关于陷入 等待调试器 执行时Run 而这个问题就陷入了 等待调试器 执行时Debug 产生问题的步骤不同 解决方案也不同 每当我尝试使用Android Studio的调试功能时 运行状态总是停
  • 来自相机的 MediaCodec 视频流方向和颜色错误

    我正在尝试流式传输视频捕获直接从相机适用于 Android 设备 到目前为止 我已经能够从 Android 相机捕获每一帧预览帧 byte data Camera camera 函数 对数据进行编码 然后成功解码数据并显示到表面 我用的是安
  • android textview 有字符限制吗?

    我正在尝试在 android TextView 中输入超过 2000 3000 个字符 它不显示任何内容 任何一份指南是否对 android textview 有字符限制或什么 我在G3中做了一些小测试 我发现 如果activtiy布局中有
  • 未解决的包含:“cocos2d.h” - Cocos2dx

    当我在 Eclipse 中导入 cocos2dx android 项目时 我的头文件上收到此警告 Unresolved inclusion cocos2d h 为什么是这样 它实际上困扰着我 该项目可以正确编译并运行 但我希望这种情况消失
  • SDK >=26 仍需要 mipmap/ic_launcher.png?

    在 Android 中 有两种指定启动器图标 可以说是应用程序图标 的方法 老 方式 在 mipmap 文件夹中指定不同的 png 文件 通常命名为 ic launcher png 但可以通过以下方式设置名称android icon mip
  • 如何构建自定义摄像机应用程序?

    我正在尝试开发一个自定义摄像机录像机 当我的设备在 Activity 的 beginRecording 中执行 start MediaRecorder 方法时 应用程序崩溃 我不知道出了什么问题 因为我遵循谷歌API指南 http deve
  • Android Gradle 同步失败:无法解析配置“:classpath”的所有工件

    错误如下 Caused by org gradle api internal artifacts ivyservice DefaultLenientConfiguration ArtifactResolveException Could n
  • 在android中创建SQLite数据库

    我想在我的应用程序中创建一个 SQLite 数据库 其中包含三个表 我将向表中添加数据并稍后使用它们 但我喜欢保留数据库 就好像第一次安装应用程序时它会检查数据库是否存在 如果存在则更新它 否则如果不存在则创建一个新数据库 此外 我正在制作
  • 如何在布局编辑器中模拟沉浸式模式

    我想在布局编辑器中全屏查看我的布局 我正在使用 eclipse 插件 我已经通过选择隐藏了 ActionBar NoActionBar组合中的主题 但导航栏是一个不同的故事 AFAIK 它只能使用代码中的标志来隐藏 我需要在活动 xml 文
  • 我应该如何在 Android 上使用 Retrofit 处理“无互联网连接”

    我想处理没有互联网连接的情况 通常我会运行 ConnectivityManager cm ConnectivityManager context getSystemService Context CONNECTIVITY SERVICE N

随机推荐

  • 毕业设计 单片机与OpenMV机器视觉目标跟踪系统

    文章目录 0 前言 课题简介 设计框架 3 openMV实现舵机定位色块STM32 3 硬件设计 4 软件设计 4 1 硬件连接 4 2 软件代码 OpenMV端 4 3 软件代码 STM32端 4 4 利用PC端测试数据数据是否发送接收正
  • 《银行法律法规》一、经济金融基础知识——3、金融市场

    第三章 金融市场 第一节 金融市场概述 考点1 金融市场功能 概念 金融市场是指货币资金融通和金融工具交易的场所 金融市场的融资行为既包括以银行等金融机构为信用媒介的间接融资行为 也包括各类交易主体之间的直接融资行为 主体 是各类融资活动的
  • 运维企业实战Shell脚本合集+万能工具箱

    文章目录 系统维护篇 服务器日常巡检脚本 下线登录用户 企业级Linux日常自动抓取服务器巡检 登录 执行命令记录 备份脚本 终端对话 广播消息 批量查询IP归属地 手机号归属地信息 Linux开机后自动执行命令或脚本 一键自动格式化输出S
  • Anaconda中安装指定版本的tensorflow1.14.0/tensorflow-gpu1.14.0

    在运行github中一个项目时 由于其使用的tensorflow的版本是1 14 0 而我的版本是2 6 0的版本 因为版本过高导致运行失败 所以需要安装tensorflow1 14 0 首先在anaconda的命令行中输入如下命令 pip
  • 【Qt】【CMake】【CMakeLists.txt】-PROJECT_NAME 和 CMAKE_PROJECT_NAME 的区别

    Qt CMake CMakeLists txt PROJECT NAME 和 CMAKE PROJECT NAME 的区别 原帖 https stackoverflow com questions 38938315 difference b
  • 2000+Docker镜像,Kolla是如何管理的

    根据 DockerHub 上的数据 整个 Kolla 项目管理的 镜像有 2000 多个 这么多的镜像 是怎么定义 又是如何构建的呢 简介 我们一直在说的 Kolla 通常情况下泛指 包括了 Kolla 和 Kolla Ansible 两个
  • 二进制部署K8s

    一 环境需求 节点IP 节点名称 所需组件 192 168 248 11 k8s master docker etcd apiserver controller manager scheduler kube proxy flannel 19
  • cobra库:基于cobra-cli命令行生成项目结构

    cobra库 基于cobra cli命令行生成项目结构 一 新建go项目 在F盘创建文件夹cobra started 1 使用mod对go项目进行管理 go mod init cobra started 二 使用cobra cli代码生成
  • 手写嵌入式操作系统(基于stm8单片机)

    include
  • maven学习总结

    众所周知 maven的两大作用是项目构建和依赖管理 除此之外 基于多模块项目 maven常用的功能还有模块化管理 项目构建 Maven是一个构建工具 可以根据项目中的配置文件 pom xml 来自动执行项目的构建过程 它可以将源代码编译 运
  • win10 win7局域网、AD域内共享文件夹方法

    第一 确保访问电脑和被访问电脑同在域中 可右击此电脑 属性 域 查看 第二 确保防火墙关闭 如图均已关闭 第三 选择要共享的文件夹 右击 属性 共享 高级共享 全新 Everyone或指定个人 第四 分享地址 即 本机IP地址 win r输
  • 惠普 g5 服务器 centos安装系统,hp 380G5 安装centos 7

    最近给服务器升级操作系统 发现hp的老机器安装centos 7时不能识别硬盘 原因 hp的服务器G5 使用的是CCISS driver 新的机器使用的是HPSA driver RHEL7 已经移除了 cciss 的支持 处理 安装时候 修改
  • 常数据成员、常成员函数

    定义常数据成员 类型 const 对象名 或者 const 类型 对象名 例如 const clock c1 9 9 9 或者 clock const c2 10 10 10 常对象的几条特殊规则 1 常对象 不能被赋值 2 常对象 不能访
  • 【Pytorch Lighting】第 7 章:半监督学习

    大家好 我是Sonhhxg 柒 希望你看完之后 能对你有所帮助 不足请指正 共同学习交流 个人主页 Sonhhxg 柒的博客 CSDN博客 欢迎各位 点赞 收藏 留言 系列专栏 机器学习 ML 自然语言处理 NLP 深度学习 DL fore
  • 2、halcon+利用光流场检测运动的物体

    这个事例是应用optical flow mg这个算子来在一个图像序列中计算其光溜 并且分割其运动物体 dev update off 把程序窗口 变量窗口 显示窗体变为off状态 dev close window 关闭显示窗口 read im
  • Bert Estimator input_fn 函数调用逻辑

    目录 Bert Estimator input fn 函数调用逻辑 Bert Estimator input fn 函数调用逻辑 网上有很多讲 Bert 源码的 本身代码难度不大 主要两个重点 一个是数据集的处理 以满足 masked LM
  • 整理极客冠军方案

    总结 通常套路就是 1 先观察整体的数据分布 数据集数量 标签数量 标注框数量 数据集数量 数量大 大模型训练缓慢 标签数量 各类的分布情况 gt 分布是否平衡 focal损失函数 长尾分布 样本分布不均衡 mosaic 小目标相对较多 m
  • springboot MongoDB 主从 多数据源

    上一篇 我写了关于用一个map管理mongodb多个数据源 每个数据源 只有单例 的内容 springboot mongodb 配置多数据源 临到部署到阿里云的测试环境 发现还需要考虑一下主从的问题 阿里云买的数据库 不是说让你无感知的 而
  • curl 支持 http2

    源码安装 安装 nghttp2 git clone https github com tatsuhiro t nghttp2 git cd nghttp2 autoreconf i automake autoconf configure m
  • Android Notifaction 从4.0到7.0

    Android Notifaction 从4 0到7 0 图1 4 0 通知样式 图2 5 0 6 0 通知样式 图3 7 0 通知样式 兼容 这么多版本 如何兼容 没关系 交给 android support v7 app Notific