การจัดการพลังงานแอพ

ใน Android 9 ขึ้นไป แพลตฟอร์มสามารถตรวจสอบแอปสำหรับพฤติกรรมที่ส่งผลเสียต่ออายุการใช้งานแบตเตอรี่ของอุปกรณ์ แพลตฟอร์มใช้และประเมินกฎการตั้งค่าเพื่อให้ขั้นตอน UX ที่ให้ผู้ใช้มีตัวเลือกในการจำกัดแอปที่ละเมิดกฎ

ใน Android 8.0 และต่ำกว่านั้น มีการจำกัดคุณสมบัติต่างๆ เช่น Doze การสแตนด์บายแอป การจำกัดพื้นหลัง และการจำกัดตำแหน่งในเบื้องหลัง อย่างไรก็ตาม แอปบางแอปยังคงแสดงพฤติกรรมที่ไม่ดี ซึ่งบางแอปได้อธิบายไว้ใน Android Vitals Android 9 แนะนำโครงสร้างพื้นฐานของระบบปฏิบัติการที่สามารถตรวจจับและจำกัดแอปตามกฎการตั้งค่าที่สามารถอัปเดตได้เมื่อเวลาผ่านไป

ข้อจำกัดเบื้องหลัง

ผู้ใช้สามารถจำกัดแอพ หรือระบบอาจแนะนำแอพที่ตรวจพบว่าส่งผลกระทบในทางลบต่อความสมบูรณ์ของอุปกรณ์

แอพที่จำกัด:

  • ผู้ใช้ยังสามารถเปิดใช้งานได้
  • ไม่สามารถเรียกใช้งาน/นาฬิกาปลุก หรือใช้เครือข่ายในพื้นหลัง
  • ไม่สามารถเรียกใช้บริการเบื้องหน้า
  • ผู้ใช้สามารถเปลี่ยนเป็นแอพที่ไม่ จำกัด

ผู้ติดตั้งอุปกรณ์สามารถเพิ่มข้อจำกัดเพิ่มเติมให้กับแอพเพื่อ:

  • จำกัดแอปไม่ให้รีสตาร์ทเอง
  • จำกัดการให้บริการจากการถูกผูกมัด (มีความเสี่ยงสูง)

แอปที่ถูกจำกัดในเบื้องหลังไม่คาดว่าจะใช้ทรัพยากรของอุปกรณ์ เช่น หน่วยความจำ CPU และแบตเตอรี่ แอปที่จำกัดในเบื้องหลังไม่ควรส่งผลกระทบต่อความสมบูรณ์ของอุปกรณ์เมื่อผู้ใช้ไม่ได้ใช้งานแอปเหล่านั้น อย่างไรก็ตาม แอปเดียวกันนี้คาดว่าจะทำงานได้อย่างสมบูรณ์เมื่อผู้ใช้เปิดแอป

การใช้การใช้งานแบบกำหนดเอง

ผู้ปรับใช้อุปกรณ์สามารถใช้วิธีการที่กำหนดเองต่อไปเพื่อใช้ข้อจำกัดในแอป

การรวมข้อ จำกัด ของแอพ

ส่วนต่อไปนี้จะสรุปวิธีการกำหนดและรวมข้อจำกัดของแอพในอุปกรณ์ของคุณ หากคุณกำลังใช้วิธีการจำกัดแอปจาก Android 8.x หรือต่ำกว่า ให้ตรวจสอบส่วนต่อไปนี้อย่างละเอียดเพื่อดูการเปลี่ยนแปลงใน Android 9 ขึ้นไป

การตั้งค่าสถานะ AppOpsManager

เมื่อแอปถูกจำกัด ให้ตั้งค่าสถานะที่เหมาะสมใน AppOpsManager ตัวอย่างโค้ดจาก packages/apps/Settings/src/com/android/settings/fuelgauge/BatteryUtils.java :

   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);
    }

การทำให้แน่ใจว่า isBackgroundRestricted จะคืนค่าเป็นจริง

เมื่อแอปถูกจำกัด ตรวจสอบให้แน่ใจว่า ActivityManager.isBackgroundRestricted() คืนค่า true

การบันทึกเหตุผลของข้อจำกัด

เมื่อแอปถูกจำกัด ให้บันทึกสาเหตุของการจำกัด ตัวอย่างโค้ดของการบันทึกจาก packages/apps/Settings/src/com/android/settings/fuelgauge/batterytip/actions/RestrictAppAction.java :

mBatteryUtils.setForceAppStandby(mBatteryUtils.getPackageUid(packageName), packageName,AppOpsManager.MODE_IGNORED);
if (CollectionUtils.isEmpty(appInfo.anomalyTypes)) {
  // Only log context if there is no anomaly type
  mMetricsFeatureProvider.action(mContext,
    MetricsProto.MetricsEvent.ACTION_TIP_RESTRICT_APP, packageName,
    Pair.create(MetricsProto.MetricsEvent.FIELD_CONTEXT,metricsKey));
            } else {
  // Log ALL the anomaly types
  for (int type : appInfo.anomalyTypes) {
    mMetricsFeatureProvider.action(mContext,
      MetricsProto.MetricsEvent.ACTION_TIP_RESTRICT_APP, packageName,
      Pair.create(MetricsProto.MetricsEvent.FIELD_CONTEXT, metricsKey),
      Pair.create(MetricsProto.MetricsEvent.FIELD_ANOMALY_TYPE, type));
  }

แทนที่ type ด้วยค่าจาก AnomalyType

ผู้ดำเนินการอุปกรณ์สามารถใช้ค่าคงที่ที่กำหนดไว้ใน src/com/android/settings/fuelgauge/batterytip/StatsManagerConfig.java :

public @interface AnomalyType {
        // This represents an error condition in the anomaly detection.
        int NULL = -1;
         // The anomaly type does not match any other defined type.
        int UNKNOWN_REASON = 0;
         // The application held a partial (screen off) wake lock for a period of time that
         // exceeded the threshold with the screen off when not charging.
        int EXCESSIVE_WAKELOCK_ALL_SCREEN_OFF = 1;
         // The application exceeded the maximum number of wakeups while in the background
         // when not charging.
        int EXCESSIVE_WAKEUPS_IN_BACKGROUND = 2;
         // The application did unoptimized Bluetooth scans too frequently when not charging.
        int EXCESSIVE_UNOPTIMIZED_BLE_SCAN = 3;
         // The application ran in the background for a period of time that exceeded the
         // threshold.
        int EXCESSIVE_BACKGROUND_SERVICE = 4;
         // The application exceeded the maximum number of wifi scans when not charging.
        int EXCESSIVE_WIFI_SCAN = 5;
         // The application exceed the maximum number of flash writes
        int EXCESSIVE_FLASH_WRITES = 6;
         // The application used more than the maximum memory, while not spending any time
         // in the foreground.
        int EXCESSIVE_MEMORY_IN_BACKGROUND = 7;
         // The application exceeded the maximum percentage of frames with a render rate of
         // greater than 700ms.
        int EXCESSIVE_DAVEY_RATE = 8;
         // The application exceeded the maximum percentage of frames with a render rate
         // greater than 16ms.
        int EXCESSIVE_JANKY_FRAMES = 9;
         // The application exceeded the maximum cold start time - the app has not been
         // launched since last system start, died or was killed.
        int SLOW_COLD_START_TIME = 10;
         // The application exceeded the maximum hot start time - the app and activity are
         // already in memory.
        int SLOW_HOT_START_TIME = 11;
         // The application exceeded the maximum warm start time - the app was already in
         // memory but the activity wasn't created yet or was removed from memory.
        int SLOW_WARM_START_TIME = 12;
         // The application exceeded the maximum number of syncs while in the background.
        int EXCESSIVE_BACKGROUND_SYNCS = 13;
         // The application exceeded the maximum number of gps scans while in the background.
        int EXCESSIVE_GPS_SCANS_IN_BACKGROUND = 14;
         // The application scheduled more than the maximum number of jobs while not charging.
        int EXCESSIVE_JOB_SCHEDULING = 15;
         // The application exceeded the maximum amount of mobile network traffic while in
         // the background.
        int EXCESSIVE_MOBILE_NETWORK_IN_BACKGROUND = 16;
         // The application held the WiFi lock for more than the maximum amount of time while
         // not charging.
        int EXCESSIVE_WIFI_LOCK_TIME = 17;
         // The application scheduled a job that ran longer than the maximum amount of time.
        int JOB_TIMED_OUT = 18;
         // The application did an unoptimized Bluetooth scan that exceeded the maximum
         // time while in the background.
        int LONG_UNOPTIMIZED_BLE_SCAN = 19;
         // The application exceeded the maximum ANR rate while in the background.
        int BACKGROUND_ANR = 20;
         // The application exceeded the maximum crash rate while in the background.
        int BACKGROUND_CRASH_RATE = 21;
         // The application exceeded the maximum ANR-looping rate.
        int EXCESSIVE_ANR_LOOPING = 22;
         // The application exceeded the maximum ANR rate.
        int EXCESSIVE_ANRS = 23;
         // The application exceeded the maximum crash rate.
        int EXCESSIVE_CRASH_RATE = 24;
         // The application exceeded the maximum crash-looping rate.
        int EXCESSIVE_CRASH_LOOPING = 25;
         // The application crashed because no more file descriptors were available.
        int NUMBER_OF_OPEN_FILES = 26;
    }

เมื่อผู้ใช้หรือระบบลบข้อจำกัดของแอพ คุณต้องบันทึกเหตุผลในการลบข้อจำกัด ตัวอย่างโค้ดของการบันทึกจาก packages/apps/Settings/src/com/android/settings/fuelgauge/batterytip/actions/UnrestrictAppAction.java :

public void handlePositiveAction(int metricsKey) {
        final AppInfo appInfo = mUnRestrictAppTip.getUnrestrictAppInfo();
        // Clear force app standby, then app can run in the background
        mBatteryUtils.setForceAppStandby(appInfo.uid, appInfo.packageName,
                AppOpsManager.MODE_ALLOWED);
        mMetricsFeatureProvider.action(mContext,
                MetricsProto.MetricsEvent.ACTION_TIP_UNRESTRICT_APP, appInfo.packageName,
                Pair.create(MetricsProto.MetricsEvent.FIELD_CONTEXT, metricsKey));
    }

ทดสอบข้อจำกัดของแอพ

หากต้องการทดสอบการทำงานของข้อจำกัดแอพใน Android 9 ขึ้นไป ให้ใช้คำสั่งใดคำสั่งหนึ่งต่อไปนี้:

  • ใส่แอปลงในข้อจำกัด:
    appops set package-name RUN_ANY_IN_BACKGROUND ignore
  • นำแอปออกจากข้อจำกัดและกู้คืนการทำงานเริ่มต้น:
    appops set package-name RUN_ANY_IN_BACKGROUND allow
  • ทำให้แอปในพื้นหลังหยุดทำงานทันที:
    am make-uid-idle [--user user-id | all | current] package-name
  • เพิ่มแพ็คเกจไปยัง tempwhitelist ในช่วงเวลาสั้น ๆ:
    cmd deviceidle tempwhitelist [-u user] [-d duration] [package package-name]
  • เพิ่ม/ลบแพ็คเกจออกจากรายการที่อนุญาตพิเศษของผู้ใช้:
    cmd deviceidle whitelist [+/-]package-name
  • ตรวจสอบสถานะภายในของ jobscheduler และตัวจัดการสัญญาณเตือน:
    dumpsys jobscheduler
    dumpsys alarm

สแตนด์บายแอป

สแตนด์บายแอปช่วยยืดอายุแบตเตอรี่โดยเลื่อนกิจกรรมเครือข่ายพื้นหลังและงานสำหรับแอปที่ผู้ใช้ไม่ได้ใช้งานอยู่

วงจรชีวิตสแตนด์บายแอป

แพลตฟอร์มจะตรวจจับแอปที่ไม่ได้ใช้งานและวางไว้ในโหมดสแตนด์บายของแอปจนกว่าผู้ใช้จะเริ่มมีส่วนร่วมกับแอป

ในระหว่าง ขั้นตอน การตรวจจับ แพลตฟอร์มจะตรวจพบว่าแอปไม่ทำงานเมื่ออุปกรณ์ไม่ได้ชาร์จ และ ผู้ใช้ไม่ได้เปิดแอปโดยตรงหรือโดยอ้อมตามระยะเวลานาฬิกาที่กำหนด ตลอดจนระยะเวลาในการเปิดหน้าจอที่กำหนด . (การเปิดโดยอ้อมเกิดขึ้นเมื่อแอปส่วนหน้าเข้าถึงบริการในแอปที่สอง)

ระหว่าง สแตนด์บายแอป แพลตฟอร์มจะป้องกันไม่ให้แอปเข้าถึงเครือข่ายมากกว่าวันละครั้ง ทำให้การซิงค์แอปและงานอื่นๆ ล่าช้า

แพลตฟอร์ม ออกจากแอปจากโหมดสแตนด์บาย เมื่อ:

  • แอปจะเปิดใช้งาน
  • อุปกรณ์เสียบปลั๊กและชาร์จแล้ว

แอปที่ใช้งานอยู่จะไม่ได้รับผลกระทบจากการสแตนด์บายของแอป แอปจะ ทำงาน เมื่อมี:

  • กระบวนการที่อยู่เบื้องหน้าในปัจจุบัน (ไม่ว่าจะเป็นกิจกรรมหรือบริการเบื้องหน้า หรือใช้โดยกิจกรรมอื่นหรือบริการเบื้องหน้า) เช่น ผู้ฟังการแจ้งเตือน บริการการเข้าถึง วอลล์เปเปอร์เคลื่อนไหว เป็นต้น
  • การแจ้งเตือนที่ผู้ใช้ดู เช่น ในหน้าจอล็อกหรือถาดการแจ้งเตือน
  • เปิดตัวโดยผู้ใช้อย่างชัดเจน

แอปจะ ไม่ทำงาน หากไม่มีกิจกรรมข้างต้นเกิดขึ้นในช่วงระยะเวลาหนึ่ง

กำลังทดสอบแอปสแตนด์บาย

คุณสามารถทดสอบแอปสแตนด์บายด้วยตนเองโดยใช้คำสั่ง adb ต่อไปนี้:

adb shell dumpsys battery unplug
adb shell am set-idle package-name true
adb shell am set-idle package-name false
adb shell am get-idle package-name