You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何测试Doze与Standby模式?Appium支持情况及优化咨询

Doze & App Standby: Testing, Appium Support, and Optimization

Hey there, let's break down your questions one by one—these battery optimization features are critical for Android apps, so it's smart to dig into the details!

1. How to Test in Doze and Standby Modes?

You’ve got two reliable approaches: manual testing with controlled triggers, and automated testing via ADB commands (way faster for repeatable test runs).

Doze Mode Testing

  • Quick Manual Trigger
    1. Enable USB debugging on your device and connect it to your computer.
    2. Lock the device screen (Doze only activates when the screen is off).
    3. Run this ADB command to force the device into Doze mode immediately:
      adb shell dumpsys deviceidle force-idle
      
    4. To exit Doze, either unlock the device or run:
      adb shell dumpsys deviceidle unforce
      
  • Simulate Real-World Trigger
    If you want to mimic actual user conditions, leave the device unplugged, locked, and unused for ~30 minutes (timing varies slightly by device)—it’ll enter Doze automatically.

App Standby Testing

  • Force Standby for a Specific App
    Use these commands to mark your app as inactive (Standby mode):
    # First, simulate the device being unplugged (required for Standby to activate)
    adb shell dumpsys battery unplug
    # Mark your app as inactive
    adb shell am set-inactive com.your.package.name true
    
    To reactivate the app and reset battery settings:
    adb shell am set-inactive com.your.package.name false
    adb shell dumpsys battery reset
    

2. Does Appium Support Doze/Standby Modes?

Appium doesn’t have built-in APIs to directly trigger Doze or Standby, but you can easily work around this by executing ADB commands through your test script. Here’s how to do it in common languages:

Python Example

# Force the device into Doze mode
driver.execute_script('mobile: shell', {'command': 'dumpsys deviceidle force-idle'})

# Trigger Standby for your target app
driver.execute_script('mobile: shell', {'command': 'dumpsys battery unplug'})
driver.execute_script('mobile: shell', {'command': 'am set-inactive com.your.package.name true'})

Java Example

// Force Doze mode
driver.executeScript("mobile: shell", ImmutableMap.of("command", "dumpsys deviceidle force-idle"));

// Trigger Standby for your app
driver.executeScript("mobile: shell", ImmutableMap.of("command", "dumpsys battery unplug"));
driver.executeScript("mobile: shell", ImmutableMap.of("command", "am set-inactive com.your.package.name true"));

After triggering the mode, run your test assertions (e.g., check if background syncs are paused, push notifications behave as expected). Just remember to reset the device state after testing to avoid breaking subsequent runs!

3. Optimization Tips for Doze & App Standby

These tweaks will help your app respect battery optimizations while still delivering the features users care about:

For Doze Mode

  • Replace AlarmManager with WorkManager/JobScheduler: Doze restricts exact alarms, so use WorkManager (recommended for most use cases) or JobScheduler to schedule deferred tasks—they’ll run during Doze’s maintenance windows.
  • Use FCM High-Priority Messages Sparingly: If you need to wake the device for critical updates (like a security alert), send FCM high-priority messages—but don’t overdo it, as frequent wake-ups drain battery.
  • Minimize Wakelocks: Avoid holding partial wakelocks unless absolutely necessary—they prevent the device from entering Doze entirely.

For App Standby

  • Cut Unnecessary Background Activity: If users haven’t opened your app in days, skip periodic syncs or background jobs unless they’re critical to core functionality.
  • Guide Users to Whitelist (When Required): If your app needs constant background access (e.g., a fitness tracker), gently direct users to add it to the battery optimization whitelist (Settings > Battery > Battery Optimization > All Apps > Select Your App > Don’t Optimize).
  • Use FCM to Trigger Syncs: Instead of polling your server repeatedly, use FCM messages to trigger syncs only when new data is available—this cuts down on unnecessary wake-ups.

General Best Practices

  • Analyze with Battery Historian: Use Android Studio’s Battery Historian tool to pinpoint when your app is being restricted by Doze/Standby and adjust your code accordingly.
  • Test Edge Cases: Verify that your app resumes smoothly after exiting Doze/Standby, and that critical features (like notifications) work as expected when the device is in these modes.

内容的提问来源于stack exchange,提问作者the_prole

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:43:05