赞
踩
小米手机Note 3有个“电量和性能”设置,如果没有把app设置为无限制,侧app在后台很容易被杀,导致每次重新启动app,针对该情况,做了个判断
- /**
- * 电池优化设置
- */
- object BatteryOptimization {
-
-
-
- /**
- * 判断当前APP是否有加入电池优化的白名单(目前只限于小米 Note3手机)
- */
- fun isIgnoreBatteryOptimization(activity: Activity?):Boolean {
- val powerManager: PowerManager? = activity?.getSystemService(Context.POWER_SERVICE) as? PowerManager
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
- val hasIgnored: Boolean? = powerManager?.isIgnoringBatteryOptimizations(activity?.packageName)
- return hasIgnored ?: true
- }
- return true
- }
-
- /**
- * 弹出加入电池优化的白名单的设置对话框(目前只限于小米 Note3手机)
- * @param appName app名字
- */
- fun ignoreBatteryOptimizationSetting(activity: Activity?,appName: String) {
- val intent = Intent(activity?.packageName)
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
- //电量和性能--小马达配置
- val componentName = ComponentName("com.miui.powerkeeper", "com.miui.powerkeeper.ui.HiddenAppsConfigActivity")
- intent.component = componentName
- intent.putExtra("package_name", activity?.packageName);
- intent.putExtra("package_label", appName)//activity.resources.getString(R.string.app_name)
- val resolveInfos: List<ResolveInfo>? = activity?.packageManager?.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)
- if (resolveInfos.isNullOrEmpty().not()) {
- activity?.startActivity(intent)
- }
-
- }
- }
https://blog.csdn.net/wxz1179503422/article/details/56671609/
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。