如何使应用程序检查权限是否被授予(6,x +)Android
问题描述:
我有一个短信应用程序,现在在6.0用户必须允许应用程序发送短信。 现在我有这个代码,我从link here得到。如何使应用程序检查权限是否被授予(6,x +)Android
int MY_PERMISSIONS_REQUEST_READ_CONTACTS=0;
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.SEND_SMS)) {
// Show an expanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.SEND_SMS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
但是,当我拒绝接取(如果用户不小心点击按钮)逝去的将几乎使应用程序崩溃(因为短信按钮不会工作)。因此,当用户在用户拒绝访问后启动/重新启动应用程序时,我希望应用程序再次提问。现在,当我尝试在我的模拟器上没有对话框会再次出现,所以我不得不去设置/应用程序并设置权限。或者卸载并重新安装。
答
您目前正在做的是询问用户权限,但不检查权限是否被实际授予。您可以通过在请求权限的活动中实施onRequestPermissionsResult
方法来完成此操作。
有关更多信息,请参见https://stackoverflow.com/a/34722591/3872500。
答
使用不同的权限从你需要进行更改评论的工作应用的一个例子:?Android的棉花糖请求允许]
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
progressLayout.setVisibility(View.GONE);
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.ACCESS_COARSE_LOCATION)) {
//This is where you need to ask again if they want to allow the permission,
//you should show a dialog here to ask if they're sure
//and explain why you need the permission.
requestDialog = new AlertDialog.Builder(this)
.setTitle(getString(R.string.device_setup_location_permission_title))
.setMessage(getString(R.string.device_setup_location_permission_message))
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//They clicked yes, they want to enable the permission,
//so let's show them the dialog again.
ActivityCompat.requestPermissions(DeviceSetupActivity.this,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
REQUEST_LOCATION_RESPONSE);
}
})
.setNegativeButton(getString(R.string.later), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//This means they don't want to grant the permission,
//you should exit the app here, perhaps share another dialog saying that in
//order to use the app, the permission must be granted.
finish();
}
})
.show();
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
REQUEST_LOCATION_RESPONSE);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode == REQUEST_LOCATION_RESPONSE){
if(hasPermission(Manifest.permission.ACCESS_COARSE_LOCATION)){
startScan();
}else{
//exit the activity
finish();
}
}
}
private boolean hasPermission(String perm) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return(PackageManager.PERMISSION_GRANTED==checkSelfPermission(perm));
}
return true;
}
的可能的复制(http://stackoverflow.com/questions/33666071/Android的棉花糖请求 - 许可) – Rohan