【安卓13】谷歌桌面Launcher3屏蔽全部应用里面的部分app

1、需求

我们在做谷歌桌面时,移植了一些我们自己的应用,但是有些应用是服务型的app,不需要显示在主页,要隐藏掉

2、解决方案
方法1:

解决办法很简单,阅读源码发现,谷歌桌面添加全部应用的源码在这里com/android/launcher3/model/AllAppsList.java

在这个add 方法里,根据mAppFilter里面的方法去过滤掉不需要add 的应用

这个类读取了一个数组文件R.array.filtered_components,这个文件里面定义了需要过滤的组件,我这里查看源码这个数组文件是空的,没有过滤任何app,这个文件在res/values/config.xml里面定义

可以在这里添加要过滤的包名

xml 复制代码
    <!-- 定义一个字符串数组,用于存储需要过滤的组件名称 -->
    <string-array name="filtered_components">
        <!-- 使用完整的类名,或者包名+类名的形式 -->
        <item>com.example.package.SomeActivity</item>
        <item>com.example.package.AnotherService</item>
        <item>com.example.package.YetAnotherReceiver</item>
        <!-- 添加更多需要过滤的组件 -->
    </string-array>
方法2

直接在AppFilter.java类里面过滤

java 复制代码
    private final Set<ComponentName> mFilteredComponents;
    private List<String> mFilterAppList = null;

    public AppFilter(Context context) {
        mFilteredComponents = Arrays.stream(
                context.getResources().getStringArray(R.array.filtered_components))
                .map(ComponentName::unflattenFromString)
                .collect(Collectors.toSet());
		mFilterAppList.add("你要过滤的包名");
    }

    public boolean shouldShowApp(ComponentName app) {
		if (mFilterAppList != null && mFilterAppList.contains(app.getPackageName())) {
            return false;
        }
        return !mFilteredComponents.contains(app);
    }
方法3

这个方法要修改编译文件,大家可不看,这里只提供一个思路,不同的供应商修改不一样

源码目录新增一个黑名单文件,修改编译MK文件,在MK文件里面将这个黑名单文件copy到out输出目录,比如/vendor/etc/google_hide_apk_list.txt,这样在编译固件的时候,系统的这个目录会包含这个文件,这样就可以在Launcher3里面读取这个文件找到要过滤的包名

java 复制代码
public class AppFilter {

    private final Set<ComponentName> mFilteredComponents;
    private List<String> mFilterAppList = null;

    public AppFilter(Context context) {
        mFilteredComponents = Arrays.stream(
                context.getResources().getStringArray(R.array.filtered_components))
                .map(ComponentName::unflattenFromString)
                .collect(Collectors.toSet());
		mFilterAppList = getBlackList();
    }

    public boolean shouldShowApp(ComponentName app) {
		if (mFilterAppList != null && mFilterAppList.contains(app.getPackageName())) {
            return false;
        }
        return !mFilteredComponents.contains(app);
    }

	private List<String> getBlackList() {
        mFilterAppList = new ArrayList<>();
        try (
            BufferedReader bfd = new BufferedReader(new FileReader("/vendor/etc/google_hide_apk_list.txt"))
        ) {
            String retain;
            while ((retain = bfd.readLine()) != null) {
                mFilterAppList.add(retain.trim());
                if (!TextUtils.isEmpty(retain)){
					Log.d("AppFilter", "blacklist add packageName: " + retain);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return mFilterAppList;
    }

}
总结

这是常用的几种方法,第二种简单明了,直接在类里面过滤,还有什么疑问可留言沟通

相关推荐
Oueii7 分钟前
如何为开源Python项目做贡献?
jvm·数据库·python
小鸡吃米…9 分钟前
Python 中的并发 —— 进程池
linux·服务器·开发语言·python
阿拉斯攀登11 分钟前
第 18 篇 综合项目实战:基于 RK3568 的安卓智能门禁系统,全栈开发
android·驱动开发·瑞芯微·嵌入式驱动·rk3576·安卓驱动
小王不爱笑13212 分钟前
Java 异常全解析:从原理到实战,搞定异常处理
java·开发语言
Techblog of HaoWANG13 分钟前
目标检测与跟踪(10)-- Jetson Xavier NX刷机、移植&部署YOLOv8量化模型(中)
python·yolo·目标检测·onnx·量化部署
历程里程碑14 分钟前
40 UDP - 2 C++实现英汉词典查询服务
linux·开发语言·数据结构·c++·ide·c#·vim
万里沧海寄云帆21 分钟前
Vscode解决python venv虚拟环境问题
ide·vscode·python
2401_8914821724 分钟前
Python Web爬虫入门:使用Requests和BeautifulSoup
jvm·数据库·python
超越自我肖24 分钟前
python--while循环案例九九乘法表
python
油泼辣子多加27 分钟前
【PY】数据处理函数
python·深度学习·机器学习·数据分析