以前通用的实现开机自启动的做法都是监听广播,,但是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内的包名即是白名单。

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐