今天我在地圖上創建一個應用程序,但它在電池保護程序打開時崩潰。如何檢查電池保護程序是在Android API <21
如何檢查每個設備上的電池保護程序啓動時的事件對我的幫助。由於
我嘗試這一點,但在API < 21不工作:
今天我在地圖上創建一個應用程序,但它在電池保護程序打開時崩潰。如何檢查電池保護程序是在Android API <21
如何檢查每個設備上的電池保護程序啓動時的事件對我的幫助。由於
我嘗試這一點,但在API < 21不工作:
嗯。這是檢查GPS的高acurary模式。這是檢查:
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();
}
}
和方法getLocationMode GPS的返回模式:
private int getLocationMode(Context context) throws Settings.SettingNotFoundException {
return Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);
}
檢查谷歌開發者:PowerManager
在左上角,您可以更改API的水平。
正如您所看到的,isPowerSaveMode()被添加到API 21(Lollipop)中。
因此它不適用於較舊的設備。
在Android版本低於5.0(棒棒糖)的手機上,每個製造商的power_saving模式都不同。然而,如果你的目標版本是5.0和更高,你可以使用PowerManager描述Android Developer
試試這個(準備複製/粘貼):
/**
* 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;
}
我結束了此功能。
注意:它不等於isPowerSaveMode()。它更像isRunningOutOfPower()或couldBePowerSaveMode()
它檢查SDK> = 21然後isPowerSaveMode功能可用。如果不是,則檢查GPS模式是否不是HIGH_ACCURACY並且電池電量低於15%,那麼它「可能是」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;
}