How to check battery conservation in android API <21

Today I create an application on the map, but it crashes when the battery is on.

How to check the event when the battery is on, for each device, help me. Thanks

I am trying to do this, but not working on 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); } 
+2
source share
5 answers

Hmm This is a test of the high acoustic mode in GPS. This is a test:

  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 the getLocationMode method returns GPS mode:

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

Check Google Developer: PowerManager

In the upper left corner you can change the API level.

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

Thus, this will not work on older devices.

0
source

On mobile phones with Android version lower than 5.0 (Lollipop), power_saving mode is different for each manufacturer. However, if your target version is 5.0 or higher, you can use the PowerManager as described in Android Developer

0
source

Try it (ready to 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.get.getSystemService(Context.POWER_SERVICE); return powerManager.isPowerSaveMode(); } // For older versions, we just say that device is not in power save mode return false; } 
0
source

I have finished this function.

Note: it is not equal to isPowerSaveMode (). This is more like isRunningOutOfPower () or canBePowerSaveMode ()

It checks if the SDK> = 21 function is available, then isPowerSaveMode . if not, check if the GPS mode is not HIGH_ACCURACY, but the battery level is less than 15%, then it can 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; 

}

0
source

All Articles