3

How to detect "power save mode" in Android "Xiaomi MIUI" devices?

I use this code to detect "power save mode" in Android 5,6:

PowerManager powerManager = (PowerManager)
    getActivity().getSystemService(Context.POWER_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
    && powerManager.isPowerSaveMode())
{
    //power saving mode
}

But it's not work in "Xiaomi MIUI" devices.

A. Lab
  • 31
  • 3

2 Answers2

2
private void isPowerSaveModeHuaweiXiaomi(){
  if (Build.MANUFACTURER.equalsIgnoreCase("Xiaomi")) {
    try {
       int value = android.provider.Settings.System.getInt(getContext().getContentResolver(), "POWER_SAVE_MODE_OPEN");
               
            } catch (Settings.SettingNotFoundException e) {
                Log.d("Valor modo bateria:", "Error");
            }
        }else if (Build.MANUFACTURER.equalsIgnoreCase("Huawei")){
            try {
                int value = android.provider.Settings.System.getInt(getContext().getContentResolver(), "SmartModeStatus");
                
            } catch (Settings.SettingNotFoundException e) {
                Log.d("Valor modo bateria:", "Error");
            }
        }
    }
1

For Xiaomi ROM, a system setting name POWER_SAVE_MODE_OPEN will be used to save the power save mode, so we can use this name to detect if it's on or off through the system settings api.

android.provider.Settings.System#getInt(android.content.ContentResolver, java.lang.String, int)

To monitor power save mode changing, we can register a intent receiver for miui.intent.action.POWER_SAVE_MODE_CHANGED or use ContentObserver for uri content://settings/system/POWER_SAVE_MODE_OPEN

see detail code implementations from this answer.

alijandro
  • 11,627
  • 2
  • 58
  • 74