android 6.0 动态权限

时间:2021-12-20 20:49:41

Android 6.0 动态权限:

除了要在AndroidManifest.xml中申请外,还需使用时,请求用户允许授权。

以下是需要单独申请的权限,共分为9组,每组只要有一个权限申请成功了,就默认整组权限都可以使用了。

如果将targetSdkVersion设置为>=23,则必须按照Android谷歌的要求,动态的申请权限,如果你暂时不打算支持动态权限申请,则targetSdkVersion最大只能设置为22.

 group:android.permission-group.CONTACTS
permission:android.permission.WRITE_CONTACTS
permission:android.permission.GET_ACCOUNTS
permission:android.permission.READ_CONTACTS group:android.permission-group.PHONE
permission:android.permission.READ_CALL_LOG
permission:android.permission.READ_PHONE_STATE
permission:android.permission.CALL_PHONE
permission:android.permission.WRITE_CALL_LOG
permission:android.permission.USE_SIP
permission:android.permission.PROCESS_OUTGOING_CALLS
permission:com.android.voicemail.permission.ADD_VOICEMAIL group:android.permission-group.CALENDAR
permission:android.permission.READ_CALENDAR
permission:android.permission.WRITE_CALENDAR group:android.permission-group.CAMERA
permission:android.permission.CAMERA group:android.permission-group.SENSORS
permission:android.permission.BODY_SENSORS group:android.permission-group.LOCATION
permission:android.permission.ACCESS_FINE_LOCATION
permission:android.permission.ACCESS_COARSE_LOCATION group:android.permission-group.STORAGE
permission:android.permission.READ_EXTERNAL_STORAGE
permission:android.permission.WRITE_EXTERNAL_STORAGE group:android.permission-group.MICROPHONE
permission:android.permission.RECORD_AUDIO group:android.permission-group.SMS
permission:android.permission.READ_SMS
permission:android.permission.RECEIVE_WAP_PUSH
permission:android.permission.RECEIVE_MMS
permission:android.permission.RECEIVE_SMS
permission:android.permission.SEND_SMS
permission:android.permission.READ_CELL_BROADCASTS

其它的权限,不用动态申请权限,在AndroidManifest.xml中申请即可。

 public class PermissionUtils {

     public static final int REQUEST_LOCATION_PERMISSION = 999;

     public static boolean requestPermission(final Activity activity, String requestPermission) {
if (Build.VERSION.SDK_INT < 23) {
return true;
} if (activity == null) {
return false;
} /**
(1)如果是6.0以下的手机,ActivityCompat.checkSelfPermission()会始终等于PERMISSION_GRANTED,
(2)如果用户关闭了你申请的权限(如在安装的时候,将一些权限关闭了) 使用ActivityCompat.checkSelfPermission()
则可能会导致程序崩溃(java.lang.RuntimeException: Unknown exception code: 1 msg null);
**/ int checkSelfPermission;
try {
// 检查是否有此权限ActivityCompat.checkSelfPermission(...),如果已经开启,则可以做此权限下的功能。
checkSelfPermission = ActivityCompat.checkSelfPermission(activity, requestPermission);
} catch (RuntimeException e) {
openSettingActivity(activity, activity.getResources().getString(R.string.permission_request));
return false;
} if (checkSelfPermission != PackageManager.PERMISSION_GRANTED) {
// 如果未开启,则判断是否需要向用户解释为何申请权限shouldShowRequestPermissionRationale
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, requestPermission)) {
// 如果需要(返回true),则可以弹出对话框提示用户申请权限原因,用户确认后申请权限requestPermissions().
showMessage(activity, REQUEST_LOCATION_PERMISSION, requestPermission);
} else {
// 如果不需要(返回false),则直接申请权限requestPermissions()。
ActivityCompat.requestPermissions(activity, new String[] {requestPermission}
, REQUEST_LOCATION_PERMISSION);
}
return false; } else {
return true;
}
} private static void openSettingActivity(final Activity activity, String message) { showMessageOKCancel(activity, message, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", activity.getPackageName(), null);
intent.setData(uri);
activity.startActivity(intent);
}
});
} private static void showMessage(final Activity activity, final int requestCode, final String requestPermission) {
String permissionsHint = activity.getResources().getString(R.string.location_permission_tip);
showMessageOKCancel(activity, permissionsHint
, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(activity,
new String[]{requestPermission},
requestCode);
}
});
} private static void showMessageOKCancel(final Activity context, String message, DialogInterface.OnClickListener okListener) {
new AlertDialog.Builder(context)
.setMessage(message)
.setPositiveButton(android.R.string.ok, okListener)
.setNegativeButton(android.R.string.cancel, null)
.create()
.show();
}
}

使用:请求位置权限:

 if (PermissionUtils.requestPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)) {
// todo
}

第三方SDK:EasyPermissions

EasyPermissions 。和系统提供的权限管理很相似,简化权限管理。

(1) 在build.gradle文件中引入包:

 dependencies {
compile 'pub.devrel:easypermissions:0.1.7'
}

(2) 在需要申请权限的 Activity implements EasyPermissions.PermissionCallbacks

 public class MainActivity extends Activity implements EasyPermissions.PermissionCallbacks {
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults); // 调用EasyPermissions的onRequestPermissionsResult方法,参数和系统方法保持一致,然后就不要关心具体的权限申请代码了
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}
@Override
public void onPermissionsGranted(int requestCode, List<String> list) {
// 此处表示权限申请已经成功,可以使用该权限完成app的相应的操作了
// TODO
}
@Override
public void onPermissionsDenied(int requestCode, List<String> list) {
// 此处表示权限申请被用户拒绝了,此处可以通过弹框等方式展示申请该权限的原因,以使用户允许使用该权限
// TODO
}

(3) 使用:AfterPermissionGranted注解是可选的,如果有该注解的话,那么当request值对应的权限申请通过的话会自动调用该方法。

 @AfterPermissionGranted(RC_CAMERA_PERM)
public void cameraTask() {
if (EasyPermissions.hasPermissions(this, Manifest.permission.CAMERA)) {
// 已经有摄像头权限了,可以使用该权限完成app的相应的操作 } else {
// app还没有使用摄像头的权限,调用该方法进行申请,同时给出了相应的说明文案,提高用户同意的可能性
EasyPermissions.requestPermissions(this, getString(R.string.rationale_camera),
RC_CAMERA_PERM, Manifest.permission.CAMERA);
}
}

Android6.0动态权限申请步骤以及需要注意的一些坑

Android 6.0开始动态请求权限

Android 权限最佳实践和代码实例分析