一. 前言

[定制需求描述]:在插入SD后,  锁屏状态下, 去掉提示“SD卡可用于传输照片和媒体文件”

需求拆解:  要求正常显示在SystemUI下拉状态栏,  只需要屏蔽在锁屏状态下的通知.

二. 发送通知

首先来找找这个字符串"可用于传输照片和媒体文件"

是在/frameworks/base/core/res/res/values-zh-rCN.xml 中

在源码中搜索引用该字符串的文件为:StorageNotification.java     路径为:

frameworks/base/packages/SystemUI/src/com/android/systemui/usb/StorageNotification.java

当插入SD后的逻辑代码为:

 private void onVolumeStateChangedInternal(VolumeInfo vol) {switch (vol.getType()) {case VolumeInfo.TYPE_PRIVATE:onPrivateVolumeStateChangedInternal(vol);break;//SD卡case VolumeInfo.TYPE_PUBLIC:onPublicVolumeStateChangedInternal(vol);break;}}

接下来, 待SD卡挂载成功后,就会走 onVolumeMounted(vol)方法

private Notification onVolumeMounted(VolumeInfo vol) {
....
Notification.Builder builder = buildNotificationBuilder(vol, title, text)
....
}

方法中,会创建通知builder对象

private Notification.Builder buildNotificationBuilder(VolumeInfo vol, CharSequence title,CharSequence text) {Notification.Builder builder =new Notification.Builder(mContext, NotificationChannels.STORAGE).setSmallIcon(getSmallIcon(vol.getDisk(), vol.getState())).setColor(mContext.getColor(R.color.system_notification_accent_color)).setContentTitle(title).setContentText(text).setStyle(new Notification.BigTextStyle().bigText(text)).setVisibility(Notification.VISIBILITY_PUBLIC).setLocalOnly(true).extend(new Notification.TvExtender());overrideNotificationAppName(mContext, builder, false);return builder;}

接下来回到 onPublicVolumeStateChangedInternal中, 当builder创建成功后,接下来就是发送通知

 private void onPublicVolumeStateChangedInternal(VolumeInfo vol) {
....
mNotificationManager.notifyAsUser(vol.getId(), SystemMessage.NOTE_STORAGE_PUBLIC,notif, UserHandle.of(vol.getMountUserId()));
....
}

三. 显示通知

熟悉SystemUI模块的开发人员, 可以直接看管理锁屏状态下通知显示的文件:

frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/NotificationLockscreenUserManager.java  它是一个接口文件, 里面有这两个方法:

一个是通过userId 来过滤,  一个是字符串String key 来过滤隐藏通信

    boolean shouldHideNotifications(int userId);boolean shouldHideNotifications(String key);

具体的实现文件为NotificationLockscreenUserManagerImpl.java文件

我是通过用key这个方法来过滤的

public boolean shouldHideNotifications(String key) {if (getEntryManager() == null) {Log.wtf(TAG, "mEntryManager was null!", new Throwable());return true;}return isLockscreenPublicMode(mCurrentUserId)&& getEntryManager().getNotificationData().getVisibilityOverride(key) ==Notification.VISIBILITY_SECRET;}

通过全局搜索这个方法被调用的地方

在NotificationFilter.java文件中的shouldFilterOut 方法中

  /*** @return true if the provided notification should NOT be shown right now.*/public boolean shouldFilterOut(NotificationEntry entry) {final StatusBarNotification sbn = entry.notification;if (!(getEnvironment().isDeviceProvisioned()|| showNotificationEvenIfUnprovisioned(sbn))) {return true;}if (!getEnvironment().isNotificationForCurrentProfiles(sbn)) {return true;}if (getUserManager().isLockscreenPublicMode(sbn.getUserId())&& (sbn.getNotification().visibility == Notification.VISIBILITY_SECRET|| getUserManager().shouldHideNotifications(sbn.getUserId())|| getUserManager().shouldHideNotifications(sbn.getKey()))) {return true;}
....}

这个方法表达的意思就是 在特定条件下过滤掉通知, 所以这样子就有解决方法了, 我们在这个方法中根据特定的判断条件,返回true,表示需要在锁屏界面上过滤掉这个通知显示.

四. 解决方案

既然是通过key作为判断条件, 在发送通知的地方加上一个key相关的方法

 private Notification.Builder buildNotificationBuilder(VolumeInfo vol, CharSequence title,CharSequence text) {Notification.Builder builder =new Notification.Builder(mContext, NotificationChannels.STORAGE).setSmallIcon(getSmallIcon(vol.getDisk(), vol.getState())).setColor(mContext.getColor(R.color.system_notification_accent_color)).setContentTitle(title).setContentText(text).setStyle(new Notification.BigTextStyle().bigText(text)).setVisibility(Notification.VISIBILITY_PUBLIC).setLocalOnly(true)//加上这个自定义的key  不需要显示在锁屏界面.setGroup("not_show_on_lockscreen").extend(new Notification.TvExtender());overrideNotificationAppName(mContext, builder, false);return builder;}

然后在显示通知的过滤方法中,加入判断条件并返回true

    public boolean shouldFilterOut(NotificationEntry entry) {final StatusBarNotification sbn = entry.notification;if (!(getEnvironment().isDeviceProvisioned()|| showNotificationEvenIfUnprovisioned(sbn))) {return true;}//在锁屏界面不显示SD卡的通知if ("not_show_on_lockscreen".equals(sbn.getGroupKey())) {return true;}//....}

Android 屏蔽锁屏界面上的通知显示相关推荐

  1. android 系统 锁屏界面,在安卓手机系统使用Ubuntu漂亮的锁屏界面攻略

    如果你了解Ubuntu系统,那么绝对会被它那漂亮的锁屏界面所吸引.今天我们就让广大Android用户能够提前感受一下Ubuntu系统的锁屏界面,并且不会有任何功能上的影响,所有的通知与信息都可以正常显 ...

  2. android 游戏 锁屏界面开发,android 锁屏程序开发

    参考http://blog.csdn.net/wdaming1986/article/details/8837023 锁屏程序的步骤如下: 1.替换系统锁屏 2.屏蔽Home键,back键.menu键 ...

  3. 安卓12锁屏界面上滑解锁困难优化

    1. 参考链接 SystemUI 上滑解锁困难容易失败的分析与解决方案 Android Q 滑屏解锁误操作判断规则 Android S 滑动解锁概率失败 2. 前情摘要 目前MTK安卓12平台,部分项 ...

  4. android屏蔽锁屏广播,android禁止锁屏保持常亮(示例代码)

    在播放video的时候在mediaplayer mMediaPlayer.setScreenOnWhilePlaying(true); 已经设置了,在原生的android没有问题和在defy上也没有问 ...

  5. 《SystemUI》修改SystemUI锁屏界面时间格式

    要求:修改SystemUI锁屏界面时间格式 Android P 上Sysyemui锁屏界面上的日期显示不在DateView处理了,使用KeyguardSliceProvider来处理,继承Conten ...

  6. Android 10.0锁屏界面默认不显示Notification通知

    在系统开机以后,默认在锁屏界面如果有通知会显示的,但是这样客户觉得非常不方便,要求去掉显示的所有通知,为了满足客户需求 所以就要实现这个功能 在StatusBarNotificationPresent ...

  7. android锁屏界面快捷键,Funtouch新特性 锁屏快捷键可以自定义

    凤凰数码讯 5月1日 vivo的Funtouch OS系统日前曝出新特性,用户可以自定义锁屏快捷键,大大提升了锁屏界面的易用性. 中关村在线消息,由于设计风格华丽.UI特色鲜明,vivo基于Andro ...

  8. Android 原生锁屏页面音乐控制

    Android5.0 提出了全新的MediaSession概念用于播放器与控制器之间进行交互,它取代之前的RemoteControlClient,并提供了更为灵活的客户端受控端模型. 但是MediaS ...

  9. android 闹钟锁屏页,Android AlarmManager 锁屏显示闹钟

    锁屏状态下点亮屏幕,并弹出闹钟提示信息,可以在锁屏界面上取消闹钟:使用广播接收闹钟定时: 下面是例子里的核心代码如下 android 设置定时闹钟(包括提醒一次和循环提醒): Intent inten ...

最新文章

  1. python-如何解决python执行pip install 命令的时候出现 File“<stdin>“,line 1 pip install XXX的问题
  2. 参加第六届ITAT C语言程序设计大赛复赛-----数学溃败
  3. Tomcat的系统架构(以Tomcat5为基础)
  4. 深度学习目标检测相关论文资源合辑
  5. EISCONN的故事
  6. 【LeetCode】【HOT】208. 实现 Trie (前缀树)
  7. python数字组合算法_python - 简单算法题 - 求三位数组合
  8. 盘点一款手机Python编程神器——AidLearning
  9. PDF控件Aspose.Pdf 12月新版17.12发布 | 附下载
  10. 360安全卫士清理C盘
  11. 2018java程序员面试题整理
  12. 笔记本安装PCMCIA并口卡
  13. python金融分析小知识(6)——偏度与峰度的计算
  14. 易友八字合婚系统发布(支持同性基友拉拉兼容)1.01发布
  15. 517编程 【初级班】 第八课 D. 结尾0的个数
  16. 计算购买商品总金额的程序
  17. 计算机应用基础时间,《计算机应用基础》考试时间安排
  18. java 与或_Java 语言中的逻辑与 () 和逻辑或 (||) 运算采用 方式进行运算。_学小易找答案...
  19. Google Earth Engine(GEE)——图像位移与配准!
  20. 计算思维-程序设计方法论-Python笔记

热门文章

  1. [附源码]java毕业设计房屋租赁管理系统
  2. 服务器这么做网站,用服务器做网站空间
  3. ASEMI代理AD8603AUJZ-REEL7原装ADI车规级AD8603AUJZ-REEL7
  4. 穷查理宝典-读书笔记
  5. 电商行业前景怎么样?
  6. Unity Shader之uv旋转
  7. QT Review之 QSlider(滑块)
  8. 读小说摘抄 —— 钱钟书的《围城》
  9. vue使用axios配置多域名
  10. 替代人工操作,模拟浏览器的“行为”