AndroidAOSP定制之关闭某些app的通知

AndroidAOSP定制之关闭某些app的通知

前言:

​ 最近在做AOSP系统定制时发现gms定制好了后,Google应用商店用不了,提示此设备未获得Play保护机制认证,Google应用和服务无法在此设备上运行,查看官方文档和资料,说是由于Android设备id生成不对引起的,可以通过修改系统属性解决,需求简单描述还是如何默认关闭掉Google Play服务的通知,或者某个app的通知.

1.报错信息如下:

在这里插入图片描述

2.解决方法1:

  • 源码路径:/frameworks/base/services/core/java/com/android/server/notification/NotificationManagerService.java
  • 源码:
  • 通过包名判断过滤过需要关闭通知的应用

在这里插入图片描述

void enqueueNotificationInternal(final String pkg, final String opPkg, final int callingUid,final int callingPid, final String tag, final int id, final Notification notification,int incomingUserId, boolean postSilently) {if (HwNotificationManagerService.disableNotification()) {return;}if (DBG) {Slog.v(TAG, "enqueueNotificationInternal: pkg=" + pkg + " id=" + id+ " notification=" + notification);}if (pkg == null || notification == null) {throw new IllegalArgumentException("null not allowed: pkg=" + pkg+ " id=" + id + " notification=" + notification);}final int userId = ActivityManager.handleIncomingUser(callingPid,callingUid, incomingUserId, true, false, "enqueueNotification", pkg);final UserHandle user = UserHandle.of(userId);// Can throw a SecurityException if the calling uid doesn't have permission to post// as "pkg"final int notificationUid = resolveNotificationUid(opPkg, pkg, callingUid, userId);if (notificationUid == INVALID_UID) {throw new SecurityException("Caller " + opPkg + ":" + callingUid+ " trying to post for invalid pkg " + pkg + " in user " + incomingUserId);}checkRestrictedCategories(pkg, notification);// Fix the notification as best we can.try {fixNotification(notification, pkg, tag, id, userId);} catch (Exception e) {Slog.e(TAG, "Cannot fix notification", e);return;}mUsageStats.registerEnqueuedByApp(pkg);final StatusBarNotification n = new StatusBarNotification(pkg, opPkg, id, tag, notificationUid, callingPid, notification,user, null, mSystemClock.currentTimeMillis());// setup local book-keepingString channelId = notification.getChannelId();if (mIsTelevision && (new Notification.TvExtender(notification)).getChannelId() != null) {channelId = (new Notification.TvExtender(notification)).getChannelId();}String shortcutId = n.getShortcutId();final NotificationChannel channel = mPreferencesHelper.getConversationNotificationChannel(pkg, notificationUid, channelId, shortcutId,true /* parent ok */, false /* includeDeleted */);if (channel == null) {final String noChannelStr = "No Channel found for "+ "pkg=" + pkg+ ", channelId=" + channelId+ ", id=" + id+ ", tag=" + tag+ ", opPkg=" + opPkg+ ", callingUid=" + callingUid+ ", userId=" + userId+ ", incomingUserId=" + incomingUserId+ ", notificationUid=" + notificationUid+ ", notification=" + notification;Slog.e(TAG, noChannelStr);boolean appNotificationsOff = mPreferencesHelper.getImportance(pkg, notificationUid)== NotificationManager.IMPORTANCE_NONE;if (!appNotificationsOff) {doChannelWarningToast("Developer warning for package \"" + pkg + "\"\n" +"Failed to post notification on channel \"" + channelId + "\"\n" +"See log for more details");}return;}final NotificationRecord r = new NotificationRecord(getContext(), n, channel);r.setIsAppImportanceLocked(mPreferencesHelper.getIsAppImportanceLocked(pkg, callingUid));r.setPostSilently(postSilently);r.setFlagBubbleRemoved(false);r.setPkgAllowedAsConvo(mMsgPkgsAllowedAsConvos.contains(pkg));if ((notification.flags & Notification.FLAG_FOREGROUND_SERVICE) != 0) {final boolean fgServiceShown = channel.isFgServiceShown();if (((channel.getUserLockedFields() & NotificationChannel.USER_LOCKED_IMPORTANCE) == 0|| !fgServiceShown)&& (r.getImportance() == IMPORTANCE_MIN|| r.getImportance() == IMPORTANCE_NONE)) {// Increase the importance of foreground service notifications unless the user had// an opinion otherwise (and the channel hasn't yet shown a fg service).if (TextUtils.isEmpty(channelId)|| NotificationChannel.DEFAULT_CHANNEL_ID.equals(channelId)) {r.setSystemImportance(IMPORTANCE_LOW);} else {channel.setImportance(IMPORTANCE_LOW);r.setSystemImportance(IMPORTANCE_LOW);if (!fgServiceShown) {channel.unlockFields(NotificationChannel.USER_LOCKED_IMPORTANCE);channel.setFgServiceShown(true);}mPreferencesHelper.updateNotificationChannel(pkg, notificationUid, channel, false);r.updateNotificationChannel(channel);}} else if (!fgServiceShown && !TextUtils.isEmpty(channelId)&& !NotificationChannel.DEFAULT_CHANNEL_ID.equals(channelId)) {channel.setFgServiceShown(true);r.updateNotificationChannel(channel);}}ShortcutInfo info = mShortcutHelper != null? mShortcutHelper.getValidShortcutInfo(notification.getShortcutId(), pkg, user): null;if (notification.getShortcutId() != null && info == null) {Slog.w(TAG, "notification " + r.getKey() + " added an invalid shortcut");}r.setShortcutInfo(info);r.setHasSentValidMsg(mPreferencesHelper.hasSentValidMsg(pkg, notificationUid));r.userDemotedAppFromConvoSpace(mPreferencesHelper.hasUserDemotedInvalidMsgApp(pkg, notificationUid));if (!checkDisqualifyingFeatures(userId, notificationUid, id, tag, r,r.getSbn().getOverrideGroupKey() != null)) {return;}if (info != null) {// Cache the shortcut synchronously after the associated notification is posted in case// the app unpublishes this shortcut immediately after posting the notification. If the// user does not modify the notification settings on this conversation, the shortcut// will be uncached by People Service when all the associated notifications are removed.mShortcutHelper.cacheShortcut(info, user);}// Whitelist pending intents.if (notification.allPendingIntents != null) {final int intentCount = notification.allPendingIntents.size();if (intentCount > 0) {final ActivityManagerInternal am = LocalServices.getService(ActivityManagerInternal.class);final long duration = LocalServices.getService(DeviceIdleInternal.class).getNotificationWhitelistDuration();for (int i = 0; i < intentCount; i++) {PendingIntent pendingIntent = notification.allPendingIntents.valueAt(i);if (pendingIntent != null) {am.setPendingIntentWhitelistDuration(pendingIntent.getTarget(),WHITELIST_TOKEN, duration);am.setPendingIntentAllowBgActivityStarts(pendingIntent.getTarget(),WHITELIST_TOKEN, (FLAG_ACTIVITY_SENDER | FLAG_BROADCAST_SENDER| FLAG_SERVICE_SENDER));}}}}// Need escalated privileges to get package importancefinal long token = Binder.clearCallingIdentity();boolean isAppForeground;try {isAppForeground = mActivityManager.getPackageImportance(pkg) == IMPORTANCE_FOREGROUND;} finally {Binder.restoreCallingIdentity(token);}mHandler.post(new EnqueueNotificationRunnable(userId, r, isAppForeground));
}

3.修改后的源码如下:

在这里插入图片描述

void enqueueNotificationInternal(final String pkg, final String opPkg, final int callingUid,final int callingPid, final String tag, final int id, final Notification notification,int incomingUserId, boolean postSilently) {if (HwNotificationManagerService.disableNotification()) {return;}if (DBG) {Slog.v(TAG, "enqueueNotificationInternal: pkg=" + pkg + " id=" + id+ " notification=" + notification);}if(pkg.equals("com.google.android.gms") || pkg.equals("com.android.vending")){return;}if (pkg == null || notification == null) {throw new IllegalArgumentException("null not allowed: pkg=" + pkg+ " id=" + id + " notification=" + notification);}final int userId = ActivityManager.handleIncomingUser(callingPid,callingUid, incomingUserId, true, false, "enqueueNotification", pkg);final UserHandle user = UserHandle.of(userId);// Can throw a SecurityException if the calling uid doesn't have permission to post// as "pkg"final int notificationUid = resolveNotificationUid(opPkg, pkg, callingUid, userId);if (notificationUid == INVALID_UID) {throw new SecurityException("Caller " + opPkg + ":" + callingUid+ " trying to post for invalid pkg " + pkg + " in user " + incomingUserId);}checkRestrictedCategories(pkg, notification);// Fix the notification as best we can.try {fixNotification(notification, pkg, tag, id, userId);} catch (Exception e) {Slog.e(TAG, "Cannot fix notification", e);return;}mUsageStats.registerEnqueuedByApp(pkg);final StatusBarNotification n = new StatusBarNotification(pkg, opPkg, id, tag, notificationUid, callingPid, notification,user, null, mSystemClock.currentTimeMillis());// setup local book-keepingString channelId = notification.getChannelId();if (mIsTelevision && (new Notification.TvExtender(notification)).getChannelId() != null) {channelId = (new Notification.TvExtender(notification)).getChannelId();}String shortcutId = n.getShortcutId();final NotificationChannel channel = mPreferencesHelper.getConversationNotificationChannel(pkg, notificationUid, channelId, shortcutId,true /* parent ok */, false /* includeDeleted */);if (channel == null) {final String noChannelStr = "No Channel found for "+ "pkg=" + pkg+ ", channelId=" + channelId+ ", id=" + id+ ", tag=" + tag+ ", opPkg=" + opPkg+ ", callingUid=" + callingUid+ ", userId=" + userId+ ", incomingUserId=" + incomingUserId+ ", notificationUid=" + notificationUid+ ", notification=" + notification;Slog.e(TAG, noChannelStr);boolean appNotificationsOff = mPreferencesHelper.getImportance(pkg, notificationUid)== NotificationManager.IMPORTANCE_NONE;if (!appNotificationsOff) {doChannelWarningToast("Developer warning for package \"" + pkg + "\"\n" +"Failed to post notification on channel \"" + channelId + "\"\n" +"See log for more details");}return;}final NotificationRecord r = new NotificationRecord(getContext(), n, channel);r.setIsAppImportanceLocked(mPreferencesHelper.getIsAppImportanceLocked(pkg, callingUid));r.setPostSilently(postSilently);r.setFlagBubbleRemoved(false);r.setPkgAllowedAsConvo(mMsgPkgsAllowedAsConvos.contains(pkg));if ((notification.flags & Notification.FLAG_FOREGROUND_SERVICE) != 0) {final boolean fgServiceShown = channel.isFgServiceShown();if (((channel.getUserLockedFields() & NotificationChannel.USER_LOCKED_IMPORTANCE) == 0|| !fgServiceShown)&& (r.getImportance() == IMPORTANCE_MIN|| r.getImportance() == IMPORTANCE_NONE)) {// Increase the importance of foreground service notifications unless the user had// an opinion otherwise (and the channel hasn't yet shown a fg service).if (TextUtils.isEmpty(channelId)|| NotificationChannel.DEFAULT_CHANNEL_ID.equals(channelId)) {r.setSystemImportance(IMPORTANCE_LOW);} else {channel.setImportance(IMPORTANCE_LOW);r.setSystemImportance(IMPORTANCE_LOW);if (!fgServiceShown) {channel.unlockFields(NotificationChannel.USER_LOCKED_IMPORTANCE);channel.setFgServiceShown(true);}mPreferencesHelper.updateNotificationChannel(pkg, notificationUid, channel, false);r.updateNotificationChannel(channel);}} else if (!fgServiceShown && !TextUtils.isEmpty(channelId)&& !NotificationChannel.DEFAULT_CHANNEL_ID.equals(channelId)) {channel.setFgServiceShown(true);r.updateNotificationChannel(channel);}}ShortcutInfo info = mShortcutHelper != null? mShortcutHelper.getValidShortcutInfo(notification.getShortcutId(), pkg, user): null;if (notification.getShortcutId() != null && info == null) {Slog.w(TAG, "notification " + r.getKey() + " added an invalid shortcut");}r.setShortcutInfo(info);r.setHasSentValidMsg(mPreferencesHelper.hasSentValidMsg(pkg, notificationUid));r.userDemotedAppFromConvoSpace(mPreferencesHelper.hasUserDemotedInvalidMsgApp(pkg, notificationUid));if (!checkDisqualifyingFeatures(userId, notificationUid, id, tag, r,r.getSbn().getOverrideGroupKey() != null)) {return;}if (info != null) {// Cache the shortcut synchronously after the associated notification is posted in case// the app unpublishes this shortcut immediately after posting the notification. If the// user does not modify the notification settings on this conversation, the shortcut// will be uncached by People Service when all the associated notifications are removed.mShortcutHelper.cacheShortcut(info, user);}// Whitelist pending intents.if (notification.allPendingIntents != null) {final int intentCount = notification.allPendingIntents.size();if (intentCount > 0) {final ActivityManagerInternal am = LocalServices.getService(ActivityManagerInternal.class);final long duration = LocalServices.getService(DeviceIdleInternal.class).getNotificationWhitelistDuration();for (int i = 0; i < intentCount; i++) {PendingIntent pendingIntent = notification.allPendingIntents.valueAt(i);if (pendingIntent != null) {am.setPendingIntentWhitelistDuration(pendingIntent.getTarget(),WHITELIST_TOKEN, duration);am.setPendingIntentAllowBgActivityStarts(pendingIntent.getTarget(),WHITELIST_TOKEN, (FLAG_ACTIVITY_SENDER | FLAG_BROADCAST_SENDER| FLAG_SERVICE_SENDER));}}}}// Need escalated privileges to get package importancefinal long token = Binder.clearCallingIdentity();boolean isAppForeground;try {isAppForeground = mActivityManager.getPackageImportance(pkg) == IMPORTANCE_FOREGROUND;} finally {Binder.restoreCallingIdentity(token);}mHandler.post(new EnqueueNotificationRunnable(userId, r, isAppForeground));
}

4.解决方法2:

  • 源码路径:/frameworks/base/services/core/java/com/android/server/wm/AlertWindowNotification.java
  • 源码:
void post() {// We can't create/post the notification while the window manager lock is held since it will// end up calling into activity manager. So, we post a message to do it later.mService.mH.post(this::onPostNotification);
}

在这里插入图片描述

  • 修改后的源码:
void post() {// We can't create/post the notification while the window manager lock is held since it will// end up calling into activity manager. So, we post a message to do it later.if(mPackageName.contains("com.google.android.gms") || mPackageName.contains("com.android.vending")){}else {mService.mH.post(this::onPostNotification);}
}

在这里插入图片描述

5.实现的效果截图如下:

在这里插入图片描述

6.总结:

可以看到上面的图片已经去掉了GMS默认的通知,重启模拟器和手机,打开gms通知栏都没有提示了,这样优化了体验,不让用户一直在看到一个警告提示.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/bicheng/51127.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【学习笔记】| 03 - 使用STM32CubeMX新建工程

使用STM32CubeMX新建工程是一个相对直观的过程&#xff0c;主要涉及到选择芯片型号、配置外设、时钟系统、GPIO引脚等&#xff0c;并最终生成工程代码。以下是一个详细的步骤指南&#xff1a; 一、打开STM32CubeMX并新建工程 启动STM32CubeMX&#xff1a; 打开STM32CubeMX软件…

FPGA实验6: 有时钟使能两位十进制计数器的设计

一、实验目的与要求 1.. 熟练掌握使用原理图设计较复杂电路&#xff1b; 2. 学习原理图设计中总线的表示以及使用方法。 二、实验原理 运用Quartus II 集成环境下的图形设计方法设计有时钟使能的两位十进制计数器。进行波形仿真和分析、引脚分配并下载到实验设备上进行功能…

[ECharts] There is a chart instance already initialized on the dom. 已存在图表,渲染重复

报错&#xff1a;已存在图表&#xff0c;渲染重复 解决: 在合适的时机执行 dispose 方法即可 // echarts 全局存入 实例 let myChart: any;// 在你的 initChart 初始化 Echarts 方法中 先执行清理方法 const initChart () > {// 执行清理方法然后初始化if(myChart){cons…

AndroidStudio 开发环境搭建

文章目录 AndroidStudio 开发环境搭建JDK 下载与安装&#xff0c;配置环境变量JDK1.8 下载安装配置环境变量新建JAVA_HOME编辑Path 下载AndroidStudio最新版本历史版本先安装JDK&#xff0c;后启动AS以管理员身份运行打开解决双击打不开的问题Error:你的主机中的软件中止了一个…

Coggle数据科学 | 大模型技术内参:39 种提示工程 在 29 种 NLP 任务精度对比

本文来源公众号“Coggle数据科学”&#xff0c;仅用于学术分享&#xff0c;侵权删&#xff0c;干货满满。 原文链接&#xff1a;大模型技术内参&#xff1a;39 种提示工程 在 29 种 NLP 任务精度对比 大语言模型&#xff08;LLMs&#xff09;在许多不同的自然语言处理&#x…

Linux网络-netstat命令

作者介绍&#xff1a;简历上没有一个精通的运维工程师。希望大家多多关注我&#xff0c;我尽量把自己会的都分享给大家&#xff0c;下面的思维导图也是预计更新的内容和当前进度(不定时更新)。 Linux服务器作为一个常用的网络服务器&#xff0c;主要的作用就是向客户端提供网络…

1143. 最长公共子序列(详细版)

目录 dp解法&#xff1a; 1.状态代表什么&#xff1a; 2. 状态转移方程 3.初始化 3. so为什么要这样&#xff1f; 代码实现&#xff1a; 给定两个字符串 text1 和 text2&#xff0c;返回这两个字符串的最长 公共子序列 的长度。如果不存在 公共子序列 &#xff0c;返回 0…

从小白到架构师:万字长文 | 社交媒体应用系统设计

移动互联网时代&#xff0c;社交媒体应用彻底改变了我们联系和共享信息的方式。这些平台在幕后处理庞大的用户群、数据存储和实时交互。 在本文中&#xff0c;我们将深入探讨如何设计一个可扩展且高性能的社交媒体应用系统。我们将探讨关键组件、流程图、功能需求以及容量规划…

科普文:分布式数据一致性协议Paxos

1 什么是Paxos Paxos协议其实说的就是Paxos算法, Paxos算法是基于消息传递且具有高度容错特性的一致性算 法&#xff0c;是目前公认的解决分布式一致性问题最有效的算法之一。 Paxos由 莱斯利兰伯特(Leslie Lamport)于1998年在《The Part-Time Parliament》论文中首次公 开&…

Gitops-Argo-Cli安装与使用

一、安装Argo-Cli工具 Release v2.9.21 argoproj/argo-cd GitHub **选择合适的符合你操作系统以及CPU架构的二进制文件 #依v2.9.21-X86-64-Linux操作系统为例 wget https://github.com/argoproj/argo-cd/releases/download/v2.9.21/argocd-linux-amd64 #添加执行权限并且移…

论文中的流程图参考图片

写论文的时候&#xff0c;在绘制流程图时&#xff0c;一直纠结n是大写还是小写&#xff0c;用不用斜体&#xff0c;号两边要不要空格。今天找到了一张标准的流程图来参考。图片来自 Zhi-Chang Ba et al, Combination of DCE-MRI and NME-DWI via Deep Neural Network for Predi…

虚拟机复制后网络不可用,报错“network.service - LSB: Bring up/down networking”

查询IP地址&#xff0c;eth33 没有显示IP地址 尝试重启&#xff0c;有报错&#xff0c;并且有提示&#xff0c;按照提示执行下看看 解决办法 chkconfig NetworkManager offsystemctl disable NetworkManager.serviceservice NetworkManager stopservice network restart 之后检…

边缘计算网关项目(含上报进程、32Modbus采集进程、设备搜索响应进程源码)

目录 边缘层 架构说明 包含知识点 数据上报进程 功能描述 功能开发 上报线程 数据存储线程 指令处理线程 项目源码 上报模块.c代码&#xff1a; 上报模块Makefile代码&#xff1a; STM32采集模块.c代码 设备搜索响应模块Linux部分.c代码 设备搜索响应模块Qt端代码.h …

C语言画蜡烛图

GPT-4o (OpenAI) 在C语言中&#xff0c;绘制蜡烛图&#xff08;Candlestick Chart&#xff09;不是直接的任务&#xff0c;因为C语言本身不包含高级图形绘制库。然而&#xff0c;可以通过某些图形库来完成这项任务&#xff0c;例如使用GTK、SDL、OpenGL等。 以下是通过GTK库绘…

Hive3:Hive初体验

1、创建表 CREATE TABLE test(id INT, name STRING, gender STRING);2、新增数据 INSERT INTO test VALUES(1, 王力红, 男); INSERT INTO test VALUES(2, 钉钉盯, 女); INSERT INTO test VALUES(3, 咔咔咔, 女);3、查询数据 简单查询 select * from test;带聚合函数的查询 …

论文写作之latex配置(VSCODE+TEXT LIVE)

1.overleaf 初学者学习latex可以用这个练习&#xff0c;可以在线编辑十分方便&#xff0c;但是编译时间受限制 网站&#xff1a;https://www.overleaf.com/project 2.Tex live 选择一个.iso文件下载 网站&#xff1a;Index of /CTAN/systems/texlive/Images/ 下载成功&am…

2024第三届钉钉杯大学生大数据挑战赛【A题】完整分享

2024第三届钉钉杯大学生大数据挑战赛已经开赛&#xff0c;小编给大家带来非常实用的助力【A题】完整&#xff0c;&#xff08;看图片下方的说明&#xff09;&#xff0c;资料预览&#xff1a; 微信公众号

进阶篇,内附代码:锂电池二阶模型-离线与在线参数辨识

锂电池二阶模型-在线参数辨识 背景二阶等效电路模型介绍二阶模型的离线参数辨识二阶模型的RLS表达式递推代码已知问题背景 锂电池一阶戴维南等效模型的参数辨识方法,已经在前面两期详细地讲解了一轮。 一阶模型-离线参数辨识一阶模型-在线参数辨识本期继续讲解一下如何进行二…

多线程初阶(三)- 线程案例

目录 1.单例模式 &#xff08;1&#xff09;饿汉模式 &#xff08;2&#xff09;懒汉模式 前言 懒汉式1-synchronized加锁 懒汉式2-双重if保障 懒汉式3-volatile防止误判 2.生产者消费者模型 &#xff08;1&#xff09;阻塞队列 &#xff08;2&#xff09;优点 解耦…

鸿蒙OpenHarmony Native API【结构体】 头文件

OH_Drawing_BitmapFormat Overview Related Modules: [Drawing] Description: 结构体用于描述位图像素的格式&#xff0c;包括颜色类型和透明度类型 Since: 8 Version: 1.0 Summary Public Attributes Public Attribute NameDescriptioncolorFormatalphaFormat De…