今天我在地图中创建了一个应用程序,但是当打开电池保护程序时它会崩溃。
如果每个设备都打开电池节电器,如何检查事件对我有帮助。感谢
我试试这个,但没有使用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);
}
答案 0 :(得分:0)
检查Google开发人员:PowerManager
在左上角,您可以更改API级别。
如您所见,isPowerSaveMode()在API 21(Lollipop)中添加。
因此它不适用于较旧的设备。
答案 1 :(得分:0)
在Android版本低于5.0(Lollipop)的手机上,每个制造商的power_saving模式都不同。但是,如果目标版本为5.0及更高版本,则可以使用PowerManager,如Android Developer
中所述答案 2 :(得分:0)
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);
}
答案 3 :(得分:0)
试试这个(准备复制/粘贴):
/**
* 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;
}
答案 4 :(得分:0)
我最终得到了这个功能。
注意:它不等于isPowerSaveMode()。它更像是isRunningOutOfPower()或canBePowerSaveMode()
检查SDK> = 21,然后 isPowerSaveMode 功能是否可用。如果没有,那么检查GPS模式是否不是HIGH_ACCURACY并且电池电量低于15%那么它可能是"省电模式。
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;
}