4

today i create a app in map , but it crash when battery saver is on .

How to check event when battery saver in on, for every device, help me. Thanks

i try this, but not working in API <21:

                PowerManager powerManager = (PowerManager)
                        this.getSystemService(Context.POWER_SERVICE);
                if ( powerManager.isPowerSaveMode()) {
                    // Animations are disabled in power save mode, so just show a toast instead.
                    Toast.makeText(customer_textReport.this, "Vui lòng tắt chế độ tiết kiệm pin", Toast.LENGTH_SHORT).show();
                }
                else {

                    Intent intentvitri = new Intent(customer_textReport.this, CustomerGetLocation.class);
                    startActivityForResult(intentvitri, 111);
                }
AndroidLTG
  • 91
  • 1
  • 11

5 Answers5

1

I ended up with this function.

Note: It's not equal to isPowerSaveMode(). It's more like isRunningOutOfPower() or couldBePowerSaveMode()

It checks if the SDK >= 21 then isPowerSaveMode function is available. if not, then check if the GPS mode is not HIGH_ACCURACY and battery level is less than 15% then it "could be" powerSaveMode.

public static boolean couldBePowerSaveMode(Context context) {
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  if (powerManager != null) {
    return powerManager.isPowerSaveMode();
  }
}

if (getLocationMode(context) != Settings.Secure.LOCATION_MODE_HIGH_ACCURACY) {
  return getBatteryPercentage(context) <= 15;
}
return getBatteryPercentage(context) <= 15;

}

MojAmiri
  • 506
  • 7
  • 8
0

Check Google Developer: PowerManager

In the left top, you can change API level.

As you can see, isPowerSaveMode(), is added in API 21(Lollipop).

So it won't work on older devices.

Geert Berkers
  • 653
  • 7
  • 19
0

On Mobiles with an Android version below 5.0 (Lollipop) the power_saving mode is different for each manufactor. However if your target version is 5.0 and higher you can use the PowerManager as descripted in Android Developer

Nico
  • 1,727
  • 1
  • 24
  • 42
0

Hmm.This is check high acurary mode in GPS. This is check:

   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    try {
                        if (getLocationMode(getApplicationContext()) != 3) {
                            tvmessage.setText("Please turn on GPS high Acurary");
                            btcancel_Dialog.setText("OK");
                            btcancel_Dialog.setOnClickListener(new OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                                    dlg.dismiss();
                                }
                            });
                            dlg.show();
                        } else {

                            Intent intentvitri = new Intent(customer_textReport.this, CustomerGetLocation.class);
                            startActivityForResult(intentvitri, 111);
                        }
                    } catch (Settings.SettingNotFoundException e) {
                        e.printStackTrace();
                    }
                }

and method getLocationMode return mode of GPS:

 private int getLocationMode(Context context) throws Settings.SettingNotFoundException {
    return Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);

}
AndroidLTG
  • 91
  • 1
  • 11
0

Try this (ready for copy/paste):

/**
 * Checks if device is in power save mode. For older versions that do not support this API, returns false.
 * @return true if it is, false otherwise.
 */
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static boolean isPowerSaveMode(Context context)
{
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
    {
        PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        return powerManager.isPowerSaveMode();
    }

    // For older versions, we just say that device is not in power save mode
    return false;
}
Mixaz
  • 4,068
  • 1
  • 29
  • 55