Android Background App Restriction

Android O background restriction

https://developer.android.com/about/versions/oreo/background

Android 8.0: 系统不允许后台应用创建后台服务。这个应该包含android8.0及以上。

默认情况下,这些限制仅适用于针对 O 的应用。 不过,用户可以从 Settings 屏幕为任意应用启用这些限制,即使应用并不是以 O 为目标平台。

Android Background App Restriction_第1张图片
1.png
public void handlePositiveAction(Anomaly anomaly, int contextMetricsKey) {
    super.handlePositiveAction(anomaly, contextMetricsKey);
    if (anomaly.targetSdkVersion < Build.VERSION_CODES.O) {

        mAppOpsManager.setMode(**AppOpsManager.OP_RUN_IN_BACKGROUND**, anomaly.uid,

        anomaly.packageName,

        AppOpsManager.MODE_IGNORED);
    }
}

Android P background restriction

https://developer.android.com/preview/features/power

· Excessive wake locks: 1 partial wake lock held for an hour when screen is off

· Excessive background services: If app targets API levels lower than 26 and has excessive background services

int EXCESSIVE_WAKELOCK_ALL_SCREEN_OFF = 1;

int EXCESSIVE_WAKEUPS_IN_BACKGROUND = 2;

int EXCESSIVE_UNOPTIMIZED_BLE_SCAN = 3;

int EXCESSIVE_BACKGROUND_SERVICE = 4;

int EXCESSIVE_WIFI_SCAN = 5;

Android Background App Restriction_第2张图片
2.png

Action:

  1. Force stop

  2. Force app standby, disable running in background

public void setForceAppStandby(int uid, String packageName, int mode) {
    final boolean isPreOApp = isPreOApp(packageName);
    if (isPreOApp) {
        // Control whether app could run in the background if it is pre O app
        mAppOpsManager.setMode(**AppOpsManager.OP_RUN_IN_BACKGROUND**, uid, packageName, mode);
    }
    // Control whether app could run jobs in the background
    mAppOpsManager.setMode(**AppOpsManager.OP_RUN_ANY_IN_BACKGROUND**, uid, packageName, mode);
}
Android Background App Restriction_第3张图片
3.png

Settings收到广播会把异常信息存入数据库,异常信息中有此字段anomalyInfo.autoRestriction,若为true,会自动处理,即setForceAppStandby(uid, packageName, AppOpsManager.MODE_IGNORED),若为false会提示用户处理。

你可能感兴趣的:(Android Background App Restriction)