From df685d524f04048d596930f3b7c3289cad468d54 Mon Sep 17 00:00:00 2001 From: pppscn <35696959@qq.com> Date: Sun, 6 Feb 2022 10:20:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=EF=BC=9A=E5=AE=89=E5=8D=936.?= =?UTF-8?q?0=E4=BB=A5=E4=B8=8B=E6=B2=A1=E6=9C=89=E5=BF=BD=E7=95=A5?= =?UTF-8?q?=E7=94=B5=E6=B1=A0=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../idormy/sms/forwarder/SettingActivity.java | 10 ++++- .../sms/forwarder/utils/KeepAliveUtils.java | 40 ++++++++++++------- app/src/main/res/layout/activity_setting.xml | 5 ++- build.gradle | 2 +- 4 files changed, 38 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/com/idormy/sms/forwarder/SettingActivity.java b/app/src/main/java/com/idormy/sms/forwarder/SettingActivity.java index 217f7d6c..e2cb9249 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/SettingActivity.java +++ b/app/src/main/java/com/idormy/sms/forwarder/SettingActivity.java @@ -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); diff --git a/app/src/main/java/com/idormy/sms/forwarder/utils/KeepAliveUtils.java b/app/src/main/java/com/idormy/sms/forwarder/utils/KeepAliveUtils.java index ef1215ad..77cea331 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/utils/KeepAliveUtils.java +++ b/app/src/main/java/com/idormy/sms/forwarder/utils/KeepAliveUtils.java @@ -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); } } diff --git a/app/src/main/res/layout/activity_setting.xml b/app/src/main/res/layout/activity_setting.xml index 7e9811ec..338c36b4 100644 --- a/app/src/main/res/layout/activity_setting.xml +++ b/app/src/main/res/layout/activity_setting.xml @@ -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 @@