【安卓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;
    }

}
总结

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

相关推荐
棉猴7 分钟前
玩游戏学Python9-算术运算符应用1
python·游戏·pygame·玩游戏·游戏编程
微笑星河q27 分钟前
C++类和对象(下)初始化列表、类型转换、static、友元、内部类、匿名对象
开发语言·c++
凤年徐30 分钟前
【Python】Python 语法速通指南
开发语言·python
stevenzqzq33 分钟前
android 对渠道的理解
android
quantdash_cc34 分钟前
量化数据清洗应该放在数据层还是策略层?从系统架构看清职责边界
开发语言·python·数据分析·系统架构·量化交易·股票数据·quantdash
梅雅达编程笔记38 分钟前
Day 06 · 第一个 API 接口:从 Model 到 JSON
python·django·drf·后端开发·restful api·序列化器
QEasycloud41 分钟前
BOM接口抛转失败钉钉通知策略实战教程:MySQL异常日志到钉钉消息的端到端配置
android·mysql·钉钉
yi01141 分钟前
DAY28: LeetCode 111. 二叉树的最小深度
人工智能·笔记·python·算法·leetcode·二叉树·递归
|_⊙1 小时前
C++ 类和对象零散知识点
开发语言·c++
AIsoft_86891 小时前
医患交流内容如何做好记录?
android·iphone