小工具知识网

当前位置: 首页 > 行业知识 > [AMS] Android 后台进程启动 activity 限制

行业知识

[AMS] Android 后台进程启动 activity 限制

2026-11-08 12:16:43 来源:互联网转载

https://developer.android.google.cn/guide/components/activities/background-starts?hl=lt

Android 10(API 级别 29)及更高版本对应用在后台运行时可以启动 activity 的时间施加了限制。这些限制有助于最大限度地减少对用户造成的干扰,并且可以让用户更好地控制其屏幕上显示的内容。

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

 boolean shouldAbortBackgroundActivityStart(            int callingUid,            int callingPid,            final String callingPackage,            int realCallingUid,            int realCallingPid,            WindowProcessController callerApp,            PendingIntentRecord originatingPendingIntent,            boolean allowBackgroundActivityStart,            Intent intent,            ActivityOptions checkedOptions) {        return checkBackgroundActivityStart(callingUid, callingPid, callingPackage,                realCallingUid, realCallingPid, callerApp, originatingPendingIntent,                allowBackgroundActivityStart, intent, checkedOptions) == BAL_BLOCK;    }    /**     * @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(            int callingUid,            int callingPid,            final String callingPackage,            int realCallingUid,            int realCallingPid,            WindowProcessController callerApp,            PendingIntentRecord originatingPendingIntent,            boolean allowBackgroundActivityStart,            Intent intent,            ActivityOptions checkedOptions) {        // don't abort for the most important UIDs        final int callingAppId = UserHandle.getAppId(callingUid);        final boolean useCallingUidState =                originatingPendingIntent == null                        || checkedOptions == null                        || !checkedOptions.getIgnorePendingIntentCreatorForegroundState();        if (useCallingUidState) {            if (callingUid == Process.ROOT_UID                    || callingAppId == Process.SYSTEM_UID                    || callingAppId == Process.NFC_UID) {                return logStartAllowedAndReturnCode(BAL_ALLOW_ALLOWLISTED_UID, /*background*/ false,                        callingUid, realCallingUid, intent, "Important callingUid");            }            // Always allow home application to start activities.            if (isHomeApp(callingUid, callingPackage)) {                return logStartAllowedAndReturnCode(BAL_ALLOW_ALLOWLISTED_COMPONENT,                        /*background*/ false, callingUid, realCallingUid, intent,                        "Home app");            }            // 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");            }        }        // This is used to block background activity launch even if the app is still        // visible to user after user clicking home button.        final int appSwitchState = mService.getBalAppSwitchesState();        // don't abort if the callingUid has a visible window or is a persistent system process        final int callingUidProcState = mService.mActiveUids.getUidState(callingUid);        final boolean callingUidHasAnyVisibleWindow = mService.hasActiveVisibleWindow(callingUid);        final boolean isCallingUidForeground =                callingUidHasAnyVisibleWindow                        || callingUidProcState == ActivityManager.PROCESS_STATE_TOP                        || callingUidProcState == ActivityManager.PROCESS_STATE_BOUND_TOP;        final boolean isCallingUidPersistentSystemProcess =                callingUidProcState 

上一篇:手机 宝塔面板怎么关闭?

下一篇:服务器上放冰袋怎么放?