3
我使用following method请求白名单为我的应用程序:电池优化对话框
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
String packageName = getApplicationContext().getPackageName();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!pm.isIgnoringBatteryOptimizations(packageName)) {
Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + packageName));
startActivity(intent);
}
}
我还添加了清单中的权限:
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
(是的,我知道此权限会让您在Google Play中遭到禁止,此代码仅用于测试目的)。
在Android Oreo中,显示对话框,并且在用户接受它之后没有任何反应。该应用程序未添加到白名单。但是,从那里调用PackageManager.isIgnoringBatteryOptimizations
总是会按预期返回true。
这是一个错误还是让他们在Oreo中删除了这个功能?