公司项目中用到了android11的系统,是自己通过源码编译出来的,然后还开发了一个APP,需要APP在开机后自动启动,按照网上的资料得知,安卓开机之后会发送一个“android.intent.action.BOOT_COMPLETED”的广播通知,但是如果不是系统白名单的APP,就收不到这个通知,于是研究后,通过修改源码添加系统白名单。
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;
}
OK,至此编译后重新烧录之后,我自己写的APP就能接受到开机广播通知,然后实现开机自启动了。