2020-06-08 10:00:05.600 1110-1654/system_process W/ActivityTaskManager: Background activity start [callingPackage: com.android.callthird; callingUid: 10109; isCallingUidForeground: false; isCallingUidPersistentSystemProcess: false;
realCallingUid: 10109;
isRealCallingUidForeground: false;
isRealCallingUidPersistentSystemProcess: false;
originatingPendingIntent: null;
isBgStartWhitelisted: false;
intent: Intent { flg=0x10000000 cmp=com.android.callthird/.CallPageActivity (has extras) };
callerApp: ProcessRecord{8718d1f 4138:com.android.callthird/u0a109}]
原因从 AndroidQ 开始不允许在后台启动Activity,具体可看google文档
https://developer.android.google.cn/guide/components/activities/background-starts
网上也有一些通过 fullscreen intent 来曲线救国的。毕竟我们有源码,直接定制就完事了。
两种思路,
可提供一个 ContentProvider 给第三方添加,WM中查询是否在Provider数据库中,这种更为合理
仿照之前读取vender下的权限包名.txt文件,包含则允许后台启动,这里我们采用第二种方式
找到日志打印地方 ActivityStarter 中 shouldAbortBackgroundActivityStart()
frameworks\base\services\core\java\com\android\server\wm\ActivityStarter.java
boolean shouldAbortBackgroundActivityStart(int callingUid, int callingPid,
final String callingPackage, int realCallingUid, int realCallingPid,
WindowProcessController callerApp, PendingIntentRecord originatingPendingIntent,
boolean allowBackgroundActivityStart, Intent intent) {
.....
//cczheng add for custom app can backgroundstartActivity S
if(ActivityBackgroundStartCheckUtil.isCustomMadeApp(callingPackage, intent)){
Slog.w(TAG, "Background activity start for CustomMadeApp ,ignored");
return false;
}
//E
// don't abort if the callingUid is the device owner
if (mService.isDeviceOwner(callingUid)) {
return false;
}
.....
// don't abort if the callingUid has SYSTEM_ALERT_WINDOW permission
if (mService.hasSystemAlertWindowPermission(callingUid, callingPid, callingPackage)) {
Slog.w(TAG, "Background activity start for " + callingPackage
+ " allowed because SYSTEM_ALERT_WINDOW permission is granted.");
return false;
}
// anything that has fallen through would currently be aborted
Slog.w(TAG, "Background activity start [callingPackage: " + callingPackage
+ "; callingUid: " + callingUid
+ "; isCallingUidForeground: " + isCallingUidForeground
+ "; isCallingUidPersistentSystemProcess: " + isCallingUidPersistentSystemProcess
+ "; realCallingUid: " + realCallingUid
+ "; isRealCallingUidForeground: " + isRealCallingUidForeground
+ "; isRealCallingUidPersistentSystemProcess: "
+ isRealCallingUidPersistentSystemProcess
+ "; originatingPendingIntent: " + originatingPendingIntent
+ "; isBgStartWhitelisted: " + allowBackgroundActivityStart
+ "; intent: " + intent
+ "; callerApp: " + callerApp
+ "]");
// log aborted activity start to TRON
if (mService.isActivityStartsLoggingEnabled()) {
mSupervisor.getActivityMetricsLogger().logAbortedBgActivityStart(intent, callerApp,
callingUid, callingPackage, callingUidProcState, callingUidHasAnyVisibleWindow,
realCallingUid, realCallingUidProcState, realCallingUidHasAnyVisibleWindow,
(originatingPendingIntent != null));
}
return true;
}
同级目录新增 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";
//copy from vendor\mediatek\proprietary\frameworks\base\services\core\java\com\mediatek\server\pm\PmsExtImpl.java
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());
}
}
}
}
pms_sysapp_grant_permission_list.txt 就是白名单包名配置文件