【发布时间】:2017-07-01 14:43:01
【问题描述】:
我在前台模式下有IntentService 任务,但在 Android M+ 中,任务在打盹模式下停止。如果应用程序使用意图将自己设置在白名单中,我读到了谷歌被禁止。但是,如果我使用权限并检查 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);
}
}
}
【问题讨论】:
标签: android permissions android-6.0-marshmallow