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

This commit is contained in:
pppscn 2022-02-06 10:20:00 +08:00
parent 9f65ae32a0
commit df685d524f
4 changed files with 38 additions and 19 deletions

View File

@ -99,7 +99,7 @@ public class SettingActivity extends AppCompatActivity {
//开机启动 //开机启动
checkWithReboot(findViewById(R.id.switch_with_reboot)); 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)); switchExcludeFromRecents(findViewById(R.id.switch_exclude_from_recents));
//接口请求失败重试时间间隔 //接口请求失败重试时间间隔
@ -619,7 +619,13 @@ public class SettingActivity extends AppCompatActivity {
//电池优化设置 //电池优化设置
@RequiresApi(api = Build.VERSION_CODES.M) @RequiresApi(api = Build.VERSION_CODES.M)
@SuppressLint("UseSwitchCompatOrMaterialCode") @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); isIgnoreBatteryOptimization = KeepAliveUtils.isIgnoreBatteryOptimization(this);
switch_battery_setting.setChecked(isIgnoreBatteryOptimization); switch_battery_setting.setChecked(isIgnoreBatteryOptimization);

View File

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

View File

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

View File

@ -10,7 +10,7 @@ buildscript {
maven { url 'https://repo1.maven.org/maven2/' } maven { url 'https://repo1.maven.org/maven2/' }
} }
dependencies { 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' classpath 'com.chenenyu:img-optimizer:1.2.0'
} }
} }