Android T don‘t abort background activity starts

log:

2024-08-20 15:45:12.457   581-1128  ActivityTaskManager     system_process                       I  START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 pkg=acr.browser.lightning cmp=acr.browser.lightning/.MainActivity} from uid 10056
2024-08-20 15:45:12.460   581-1128  ActivityTaskManager     system_process                       W  Background activity start [callingPackage: com.xx.xxxapp; callingUid: 10056; appSwitchState: 2; isCallingUidForeground: false; callingUidHasAnyVisibleWindow: false; callingUidProcState: FOREGROUND_SERVICE; isCallingUidPersistentSystemProcess: false; realCallingUid: 10056; isRealCallingUidForeground: false; realCallingUidHasAnyVisibleWindow: false; realCallingUidProcState: FOREGROUND_SERVICE; isRealCallingUidPersistentSystemProcess: false; originatingPendingIntent: null; allowBackgroundActivityStart: false; intent: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 pkg=acr.browser.lightning cmp=acr.browser.lightning/.MainActivity }; callerApp: ProcessRecord{4974058 1519:com.portworld.bootstartapp/u0a56}; inVisibleTask: false]
2024-08-20 15:45:12.491   581-1128  ActivityTaskManager     system_process                       E  Abort background activity starts from 10056

看日志是系统不让我们从后台启动app,跟踪源码,开始分析:

./frameworks/base/services/core/java/com/android/server/wm/ActivityStarter.java

函数流程:
executeRequest->startActivityUnchecked->startActivityInner->isAllowedToStart


private int executeRequest(Request request) {
    ...
    boolean abort = !mSupervisor.checkStartAnyActivityPermission(intent, aInfo, resultWho,
                requestCode, callingPid, callingUid, callingPackage, callingFeatureId,
                request.ignoreTargetSecurity, inTask != null, callerApp, resultRecord,
                resultRootTask);
        abort |= !mService.mIntentFirewall.checkStartActivity(intent, callingUid,
                callingPid, resolvedType, aInfo.applicationInfo);
        abort |= !mService.getPermissionPolicyInternal().checkStartActivity(intent, callingUid,
                callingPackage);

        // Merge the two options bundles, while realCallerOptions takes precedence.
        ActivityOptions checkedOptions = options != null
                ? options.getOptions(intent, aInfo, callerApp, mSupervisor) : null;

        @BalCode int balCode = BAL_ALLOW_DEFAULT;
        if (!abort) {
            try {
                Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER,
                        "shouldAbortBackgroundActivityStart");
                BackgroundActivityStartController balController =
                        mController.getBackgroundActivityLaunchController();
                //key       
                balCode =
                        balController.checkBackgroundActivityStart(
                                callingUid,
                                callingPid,
                                callingPackage,
                                realCallingUid,
                                realCallingPid,
                                callerApp,
                                request.originatingPendingIntent,
                                request.allowBackgroundActivityStart,
                                intent,
                                checkedOptions);
            } finally {
                Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
            }
        }
    ...
        
    mLastStartActivityResult = startActivityUnchecked(r, sourceRecord, voiceSession,
                request.voiceInteractor, startFlags, true /* doResume */, checkedOptions,
                inTask, inTaskFragment, balCode, intentGrants);
    ... 
    
}

//这个类是用来管理那些app可以后台启动的,checkBackgroundActivityStart里面有很多判断,符合要求的返回定义的BAL常量
/frameworks/base/services/core/java/com/android/server/wm/BackgroundActivityStartController.java

    /**
     * @return A code denoting which BAL rule allows an activity to be started,
     * or {@link BAL_BLOCK} if the launch should be blocked  
     */
    @BalCode
    int checkBackgroundActivityStart(...){
        ...
        // IME should always be allowed to start activity, like IME settings.
            final WindowState imeWindow =
                    mService.mRootWindowContainer.getCurrentInputMethodWindow();
            if (imeWindow != null && callingAppId == imeWindow.mOwnerUid) {
                return logStartAllowedAndReturnCode(BAL_ALLOW_ALLOWLISTED_COMPONENT,
                        /*background*/ false, callingUid, realCallingUid, intent,
                        "Active ime");
            }
        }

        //add text
        if(callingPackage.equals("com.xx.xx")){
            Slog.v(TAG, "Background activity start for xxx ,ignored");
            return BAL_ALLOW_DEFAULT;
        }
        //add text
        
        ...
        return BAL_BLOCK;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值