Rk3566 android11修改源码添加白名单APP,实现APP能够接受开机自启动广播,实现开机自启动framework

以前通用的实现开机自启动的做法都是监听广播,,但是android11系统已经不再支持,如果不是系统

白名单的APP,就收不到这个开机广播,于是研究后,通过修改源码添加系统白名单。安卓开机之后会

发送一个“android.intent.action.BOOT_COMPLETED”的广播通知,具体可参考这个篇文章:安卓开机
自启动

下面是添加白名单方法:

1.在路径device/rockchip/common/下创建一个pms_sysapp_grant_permission_list.txt文本,并在TXT里

面添加白名单的APP包名,然后在device/rockchip/common/device.mk添加

PRODUCT_COPY_FILES += \
    $(LOCAL_PATH)/pms_sysapp_grant_permission_list.txt:system/etc/permissions/pms_sysapp_grant_permission_list.txt

2.在frameworks/base/services/core/java/com/android/server/wm下创建ActivityBackgroundStartCheckUtil.java,代码如下:

package com.android.server.wm;

import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.os.Process;
import android.text.TextUtils;
import android.util.Log;
import com.android.internal.util.ArrayUtils;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashSet;

public class ActivityBackgroundStartCheckUtil {
    private static String TAG = "ActivityBackgroundStartCheckUtil";
    private static final File GRANT_SYS_APP_LIST_SYSTEM = Environment.buildPath(Environment.getRootDirectory(), "etc", "permissions", "pms_sysapp_grant_permission_list.txt");
    private static HashSet<String> mCustomMadeAppSet = new HashSet<String>();

    private static String CustomeKey[] = {"android", "call"};

    public static boolean isCustomMadeApp(String callingPackage, Intent intent){
        sGetGrantSystemAppFromFile(mCustomMadeAppSet, GRANT_SYS_APP_LIST_SYSTEM);
        Log.d(TAG, "isCustomMadeApp callingPackage=" + callingPackage);
        try {
            String packageName = intent.getComponent().getPackageName();
            String className = intent.getComponent().getClassName();
            if (mCustomMadeAppSet.contains(callingPackage) ||
                mCustomMadeAppSet.contains(packageName)) {
                return true;
            }
            for (String key : CustomeKey) {
                if (className.contains(key)) {
                    return true;
                }
            }
        } catch (Exception e) {
            //e.printStackTrace();
            Log.d(TAG, e.getMessage());
        }
        return false;
    }

    /**
     * Get removable system app list from config file
     * @param resultSet
     *            Returned result list
     * @param file
     *            The config file
    */
    private static void sGetGrantSystemAppFromFile(HashSet<String> resultSet, File file) {
        resultSet.clear();
        FileReader fr = null;
        BufferedReader br = null;
        try {
            if (file.exists()) {
                fr = new FileReader(file);
            } else {
                Log.d(TAG, "file in " + file + " does not exist!");
                return;
            }
            br = new BufferedReader(fr);
            String line;
            while ((line = br.readLine()) != null) {
                line = line.trim();
                if (!TextUtils.isEmpty(line)) {
                    Log.d(TAG, "read line " + line);
                    resultSet.add(line);
                }
            }
            Log.e(TAG,"GRANT_SYS_APP_LIST_SYSTEM size=" + resultSet.size());
        } catch (Exception io) {
            Log.d(TAG, io.getMessage());
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
                if (fr != null) {
                    fr.close();
                }
            } catch (IOException io) {
                Log.d(TAG, io.getMessage());
            }
        }
    }
}

3.在frameworks/base/services/core/java/com/android/server/wm/ActivityStarter.java中调用刚创建的java类。找到方法:

boolean shouldAbortBackgroundActivityStart(int callingUid, int callingPid,
            final String callingPackage, int realCallingUid, int realCallingPid,
            WindowProcessController callerApp, PendingIntentRecord originatingPendingIntent,
            boolean allowBackgroundActivityStart, Intent intent)

就在此方法的最前面加上判定,代码如下:

// 检查自定义的白名单
if(ActivityBackgroundStartCheckUtil.isCustomMadeApp(callingPackage, intent)){
	Slog.w(TAG, "Background activity start for CustomMadeApp ,ignored");
	return false;
}

make installclean之后直接整编烧录就可以了,txt内的包名即是白名单。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值