我想了解要求用户进行权限检查的最佳做法以及用户拒绝该特定权限访问时要运行的代码。
答案 0 :(得分:7)
此示例设置了CONTACTS
权限
private static final int PERMISSIONS_REQUEST_READ_CONTACTS = 1;
private static String[] PERMISSIONS_CONTACT = {Manifest.permission.READ_CONTACTS}
if (checkSelfPermission(PERMISSIONS_CONTACT)) {
Log.i(TAG,
"Contact permissions have already been granted. Displaying contact details.");
} else {
Log.i(TAG, "Contact permissions has NOT been granted. Requesting permission.");
requestPermissions(PERMISSIONS_CONTACT, PERMISSIONS_REQUEST_READ_CONTACTS);
}
…
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case PERMISSIONS_REQUEST_READ_CONTACTS: {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! do the
// calendar task you need to do.
Log.d(TAG, "permission granted");
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
Log.d(TAG, "permission denied");
}
return;
}
}
}
您可以点击此链接 http://www.applicoinc.com/blog/android-m-permissions-review/
答案 1 :(得分:2)
Dexter 是一个Android库,可以简化在运行时请求权限的过程。
答案 2 :(得分:1)
一些提示:
shouldShowRequestPermissionRationale()
方法向用户显示原因,为什么您要求许可可能并不是很明显; 答案 3 :(得分:0)
看看here - 这里有一个流程图,可以很好地解释整个过程。
根据Android的文档,根据Android的文档,您应该始终检查并请求获得许可(如果用户说从不再问,Android会在回调中自动返回DENIED如果用户过去曾拒绝过一次但没有标记永不再询问的选项,则应显示一条短信。
答案 4 :(得分:0)
示例相机运行时权限: 您需要将此代码放入相机按钮
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
givePerMissons();
} else {
openCamera();
}
public void givePerMissons() {
if ((Utils.checkPermission(MainActivity.this, Manifest.permission.CAMERA)) {
openCamera();
} else {
Utils.givePermisson(MainActivity.this, Manifest.permission.CAMERA, Utils.PERMISSOIN);
if (!Utils.permissionsList.isEmpty()) {
requestPermissions(Utils.permissionsList.toArray(new String[Utils.permissionsList.size()]), Utils.PERMISSOIN_CODE);
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case Utils.PERMISSOIN_CODE: {
if ((Utils.checkPermission(MainActivity.this, Manifest.permission.CAMERA)) {
openCamera();
} else {
Toast.makeText(getApplicationContext(),"Write Your message",Toast.LENGTH_LONG).show();
}
}
}
}
此代码必须放入您的utils类:
public static final List<String> permissionsList = new ArrayList<String>();
public static void givePermisson(Context context, String permisson, String permissonType) {
int per = context.checkSelfPermission(permisson);
if (per != PackageManager.PERMISSION_GRANTED) {
permissionsList.add(permisson);
} else if (per != PackageManager.PERMISSION_DENIED) {
}
}
public static boolean checkPermission(Context context, String permission) {
try {
PackageManager pm = context.getPackageManager();
if (pm.checkPermission(permission, context.getPackageName()) == PackageManager.PERMISSION_GRANTED) {
return true;
} } catch (Exception e) {
e.printStackTrace();
}
return false;
}
答案 5 :(得分:-1)
if(ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.CAMERA)|| ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.RECORD_AUDIO)){
Toast.makeText (this,
R.string.permissions_needed,
Toast.LENGTH_LONG).show ();
} else {
ActivityCompat.requestPermissions (
this,
new String[]{Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO},
CAMERA_MIC_PERMISSION_REQUEST_CODE);
}