英文:
Permission to disable battery optimization keeps appearing on Android API 28 despite not being changed
问题
这种情况发生在我的小米设备上,至少在模拟器上不会发生。
为了更清楚地说明发生的情况:
我需要禁用电池优化,以便正确运行应用程序中的某些服务。
但是在不更改配置的情况下,发生的情况是,当我再次启动应用程序时,会收到有关电池未被禁用的警告对话框,并且需要在再次启动应用程序时使用电池,就好像它已经被重新启用一样。
我不太在意是否由于手机中的个性化层引起的,通常在其他设备上都可以正常工作,因为我不能指望在任何手机终端上使应用程序100%工作,但是如果在大多数设备上都没有警告地再次启用电池优化,这可能会成为一个问题。
有没有办法避免自动启用电池优化?或者,至少,在使用该API的少数设备上是否经常出现这种情况?
英文:
This is happening me in a Xiaomi device, at least in emulator does not happen.
To be more clear what's happening is the following:
I need that battery optimization disabled to properly run some services from my app.
But without changing configuration what happens is that I get the dialog to warns about battery not being disabled and the need to use it when I start app again, as it had been enabled again.
I don't care much if this is due to personalization layer in the phone and it's usually working on other devices, as I cannot hope to make the app work 100% in any phone terminal, but it would be a problem if battery optimization is enabled again without warning in most devices.
Is there any way to avoid that automatic battery optimization enabling? Or, at the very least, does this usually happen just in a few devices with that API?
答案1
得分: 4
很遗憾,对于一些供应商来说,电池优化权限实际上并不允许您的应用忽略电池优化
华为、小米、魅族和其他一些供应商可能会阻止您的应用正常运行,即使它拥有所有权限。因此,开发者必须考虑一些解决方法。您还可以联系小米技术支持,询问他们是否有办法使您的应用程序正常工作。有时候,他们可以提供帮助。
至于请求权限,我们在我们的应用程序中使用以下代码,并且还没有收到任何投诉:
if (isMarshmallowOrHigher()) {
val pm = getSystemService(POWER_SERVICE) as PowerManager
val isIgnoringBatteryOptimizations = pm.isIgnoringBatteryOptimizations(
packageName
)
if (!isIgnoringBatteryOptimizations) {
val intent = Intent()
intent.action = Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
intent.data = Uri.parse("package:$packageName")
startActivityForResult(intent, ignoreOptimizationsRequest)
}
}
英文:
Sadly, for some vendors battery optimizations permissions don't actually allow your application to ignore battery optimizations
Huawei, Xiaomi, Meizu and some other vendors can block your application from doing it's job even though it has all the permissions. So developers has to think of some workarounds. Also you could reach the Xiaomi tech support to ask them if there is any way to make your application work. Sometimes they can help.
As for requesting the permissions. We use the following code in our application for it and haven't received any complaints yet:
if (isMarshmallowOrHigher()) {
val pm = getSystemService(POWER_SERVICE) as PowerManager
val isIgnoringBatteryOptimizations = pm.isIgnoringBatteryOptimizations(
packageName
)
if (!isIgnoringBatteryOptimizations) {
val intent = Intent()
intent.action = Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
intent.data = Uri.parse("package:$packageName")
startActivityForResult(intent, ignoreOptimizationsRequest)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论