我在前台模式下有IntentService任务,但在Android M+中任务会在Doze模式下停止。如果应用程序使用意图将自己设置在白名单中,我会看到谷歌被禁止。但是如果我使用permission并检查GRANT或DENIED,我会得到授权的结果,但是什么也不会发生。我在白名单中看不到我的应用。如何才能在白名单中添加应用而不被屏蔽?(我在AndroidManifest.xml中添加了权限)
if(Build.VERSION.SDK_INT>=23){
int permissionCheck= ContextCompat
.checkSelfPermission(this, Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
if(permissionCheck == PackageManager.PERMISSION_DENIED){
//Should we show an explanation
if(ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)){
//Show an explanation
final String message = "";
Snackbar.make(coordinatorLayoutView,message,Snackbar.LENGTH_LONG)
.setAction("GRANT", new View.OnClickListener() {
@Override
public void onClick(View v) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{ Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS }, PERMISSION_REQUEST_CODE);
}
})
.show();
}else{
//No explanation need,we can request the permission
ActivityCompat.requestPermissions(this, new String[]{ Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS }, PERMISSION_REQUEST_CODE);
}
}
}发布于 2017-07-01 22:55:57
REQUEST_IGNORE_BATTERY_OPTIMIZATIONS不是dangerous权限。您不需要或不想要这些代码。引用the documentation for REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
应用程序必须拥有才能使用ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS的
权限。这是一个正常的权限:请求它的应用程序将始终被授予权限,而不需要用户批准或查看。
所以,删除所有代码。
我在白名单中看不到我的应用。
显然,这是因为用户没有将您添加到白名单中。
从安全的角度来看,请求REQUEST_IGNORE_BATTERY_OPTIMIZATIONS会授予您使用an ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS Intent启动活动的权限。请确保将应用程序包作为Uri包含在内
startActivity(new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS, Uri.parse("package:"+getPackageName())));用户将被带到一个屏幕,在那里他们可以指示他们愿意在您的应用程序上暂停部分Doze模式效果。
如何才能将应用添加到白名单中而不被封禁?
如果你不想被禁止,请不要这样做。在您的应用程序中包含可启动an ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS Intent活动的内容。这将引导用户进入整个应用程序列表,用户可以在其中切换哪些应用程序在白名单上,哪些不在白名单上。这不需要任何权限。
在清单中请求REQUEST_IGNORE_BATTERY_OPTIMIZATIONS的行为是what may get you banned。
发布于 2018-06-13 23:08:52
请注意在活动中使用Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS intent,这并不是在所有手机上都有效,您将获得android.content.ActivityNotFoundException。特别是,它在运行Android6的三星手机上不起作用。我发现在这些手机上唯一起作用的组合是在清单中声明REQUEST_IGNORE_BATTERY_OPTIMIZATIONS,然后使用intent Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS.启动一个活动即Google不喜欢的组合。
https://stackoverflow.com/questions/44862176
复制相似问题