如何通过终端或其他方式测试ACTION_POWER_SAVE_MODE_CHANGED接收器?
Absolutely! There are a few reliable ways to test your ACTION_POWER_SAVE_MODE_CHANGED receiver, both via terminal commands and manual system interactions. Here are the most effective methods:
1. Directly Send the Broadcast via ADB
You can manually trigger the exact broadcast using the adb shell am broadcast command, which lets you simulate both entering and exiting power save mode events.
To send the broadcast when entering power save mode:
adb shell am broadcast -a android.os.action.POWER_SAVE_MODE_CHANGED --ez android.os.extra.POWER_SAVE_MODE true
To send the broadcast when exiting power save mode:
adb shell am broadcast -a android.os.action.POWER_SAVE_MODE_CHANGED --ez android.os.extra.POWER_SAVE_MODE false
The --ez flag passes a boolean extra that matches the PowerManager.EXTRA_POWER_SAVE_MODE key the system uses for this broadcast. Make sure your receiver is set up to read this extra if your logic depends on it.
2. Toggle Power Save Mode Manually in System Settings
This is the most realistic test scenario, as it mimics how a user would actually trigger the mode change:
- Open your device’s Settings app
- Navigate to Battery (or "Battery & Device Care" on some OEM skins)
- Look for Power Saving Mode (or "Battery Saver") and toggle it on/off
Your receiver should pick up the broadcast immediately when the mode changes. For faster testing, you can also use the quick settings tile (swipe down from the top of the screen) to toggle battery saver.
3. Use ADB to Toggle System-Wide Power Save Mode
Some devices let you directly enable/disable power save mode via ADB, which will automatically trigger the broadcast. Try these commands:
Enable power save mode:
adb shell settings put global low_power 1
Disable power save mode:
adb shell settings put global low_power 0
Note: OEMs might use different setting keys (e.g., power_saving_mode on some Samsung devices). If the above doesn’t work, check your device’s current power save setting with:
adb shell settings get global low_power
Quick Troubleshooting Tip
If your receiver still isn’t picking up the broadcast, double-check:
- If using static registration in
AndroidManifest.xml, ensure you’ve declared the correct action and that your receiver isn’t restricted by Android O+ background broadcast limitations (you might need dynamic registration for background apps). - Verify your receiver has the necessary permissions (though this broadcast doesn’t require special permissions in most cases).
内容的提问来源于stack exchange,提问作者hello_world

