android10.0(Q) 后台启动Activity白名单

简介: android10.0(Q) 后台启动Activity白名单

问题log


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 就是白名单包名配置文件

目录
相关文章
|
3月前
|
Android开发
Android面试之Activity启动流程简述
Android面试之Activity启动流程简述
98 6
|
3月前
|
程序员 开发工具 Android开发
Android|WebView 禁止长按,限制非白名单域名的跳转层级
如何限制 WebView 仅域名白名单网址能随意跳转,并禁用长按选择文字。
49 2
|
3月前
|
消息中间件 Android开发 索引
Android面试高频知识点(4) 详解Activity的启动流程
Android面试高频知识点(4) 详解Activity的启动流程
34 3
|
3月前
|
缓存 前端开发 Android开发
Android实战之如何截取Activity或者Fragment的内容?
本文首发于公众号“AntDream”,介绍了如何在Android中截取Activity或Fragment的屏幕内容并保存为图片。包括截取整个Activity、特定控件或区域的方法,以及处理包含RecyclerView的复杂情况。
31 3
|
3月前
|
Android开发
Android面试之Activity启动流程简述
Android面试之Activity启动流程简述
23 0
|
4月前
|
消息中间件 Android开发 索引
Android面试高频知识点(4) 详解Activity的启动流程
讲解Activity的启动流程了,Activity的启动流程相对复杂一下,涉及到了Activity中的生命周期方法,涉及到了Android体系的CS模式,涉及到了Android中进程通讯Binder机制等等, 首先介绍一下Activity,这里引用一下Android guide中对Activity的介绍:
61 4
|
5月前
|
XML Android开发 数据格式
android中两个Activity同时设定了intent-filter的category为android.intent.category.LAUNCHER,会发生什么情况?
本文通过案例分析了在Android中当两个Activity都设置了`android.intent.category.LAUNCHER`类别时,会导致它们同时在应用启动器的"所有应用"页面显示为不同的启动入口。
131 2
android中两个Activity同时设定了intent-filter的category为android.intent.category.LAUNCHER,会发生什么情况?
|
4月前
|
Android开发 开发者
Android面试之Activity启动流程简述
每个Android开发者都熟悉的Activity,但你是否了解它的启动流程呢?本文将带你深入了解。启动流程涉及四个关键角色:Launcher进程、SystemServer的AMS、应用程序的ActivityThread及Zygote进程。核心在于AMS与ActivityThread间的通信。文章详细解析了从Launcher启动Activity的过程,包括通过AIDL获取AMS、Zygote进程启动以及ActivityThread与AMS的通信机制。接着介绍了如何创建Application及Activity的具体步骤。整体流程清晰明了,帮助你更深入理解Activity的工作原理。
64 0
|
5月前
|
API Android开发
Android P 性能优化:创建APP进程白名单,杀死白名单之外的进程
本文介绍了在Android P系统中通过创建应用进程白名单并杀死白名单之外的进程来优化性能的方法,包括设置权限、获取运行中的APP列表、配置白名单以及在应用启动时杀死非白名单进程的代码实现。
79 1
|
5月前
|
Android开发 开发者 Kotlin
Android 多进程情况下判断应用是否处于前台或者后台
本文介绍在多进程环境下判断Android应用前后台状态的方法。通过`ActivityManager`和服务信息`RunningAppProcessInfo`可有效检测应用状态,优化资源使用。提供Kotlin代码示例,帮助开发者轻松集成。
317 8