修复:安卓6.0以下没有忽略电池优化

pull/115/head
pppscn 2 years ago
parent 9f65ae32a0
commit df685d524f

@ -99,7 +99,7 @@ public class SettingActivity extends AppCompatActivity {
//开机启动
checkWithReboot(findViewById(R.id.switch_with_reboot));
//电池优化设置
batterySetting(findViewById(R.id.switch_battery_setting));
batterySetting(findViewById(R.id.layout_battery_setting), findViewById(R.id.switch_battery_setting));
//不在最近任务列表中显示
switchExcludeFromRecents(findViewById(R.id.switch_exclude_from_recents));
//接口请求失败重试时间间隔
@ -619,7 +619,13 @@ public class SettingActivity extends AppCompatActivity {
//电池优化设置
@RequiresApi(api = Build.VERSION_CODES.M)
@SuppressLint("UseSwitchCompatOrMaterialCode")
public void batterySetting(Switch switch_battery_setting) {
public void batterySetting(LinearLayout layout_battery_setting, Switch switch_battery_setting) {
//安卓6.0以下没有忽略电池优化
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.M) {
layout_battery_setting.setVisibility(View.GONE);
return;
}
isIgnoreBatteryOptimization = KeepAliveUtils.isIgnoreBatteryOptimization(this);
switch_battery_setting.setChecked(isIgnoreBatteryOptimization);

@ -17,27 +17,39 @@ import com.idormy.sms.forwarder.R;
public class KeepAliveUtils {
@RequiresApi(api = Build.VERSION_CODES.M)
public static boolean isIgnoreBatteryOptimization(Activity activity) {
PowerManager powerManager = (PowerManager) activity.getSystemService(Context.POWER_SERVICE);
if (powerManager != null) {
return powerManager.isIgnoringBatteryOptimizations(activity.getPackageName());
} else {
//安卓6.0以下没有忽略电池优化
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.M) {
return true;
}
try {
PowerManager powerManager = (PowerManager) activity.getSystemService(Context.POWER_SERVICE);
if (powerManager != null) {
return powerManager.isIgnoringBatteryOptimizations(activity.getPackageName());
} else {
return true;
}
} catch (Exception e) {
ToastUtils.show(R.string.unsupport);
return false;
}
}
@RequiresApi(api = Build.VERSION_CODES.M)
public static void ignoreBatteryOptimization(Activity activity) {
if (isIgnoreBatteryOptimization(activity)) {
return;
}
@SuppressLint("BatteryLife") Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + activity.getPackageName()));
ResolveInfo resolveInfo = activity.getPackageManager().resolveActivity(intent, 0);
if (resolveInfo != null) {
activity.startActivity(intent);
} else {
try {
if (isIgnoreBatteryOptimization(activity)) {
return;
}
@SuppressLint("BatteryLife") Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + activity.getPackageName()));
ResolveInfo resolveInfo = activity.getPackageManager().resolveActivity(intent, 0);
if (resolveInfo != null) {
activity.startActivity(intent);
} else {
ToastUtils.show(R.string.unsupport);
}
} catch (Exception e) {
ToastUtils.show(R.string.unsupport);
}
}

@ -132,7 +132,7 @@
android:id="@+id/cbCallType3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="-15dp"
android:layout_marginStart="-10dp"
android:padding="0dp"
android:scaleX="0.7"
android:scaleY="0.7"
@ -218,7 +218,7 @@
android:id="@+id/checkbox_cancel_app_notify"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="-6dp"
android:layout_marginStart="-10dp"
android:scaleX="0.7"
android:scaleY="0.7"
android:text="@string/cancel_app_notify" />
@ -567,6 +567,7 @@
</LinearLayout>
<LinearLayout
android:id="@+id/layout_battery_setting"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"

@ -10,7 +10,7 @@ buildscript {
maven { url 'https://repo1.maven.org/maven2/' }
}
dependencies {
classpath 'com.android.tools.build:gradle:7.1.0'
classpath 'com.android.tools.build:gradle:7.1.1'
classpath 'com.chenenyu:img-optimizer:1.2.0'
}
}

Loading…
Cancel
Save