当前位置: 代码迷 >> 综合 >> Android6.0动态申请权限(简单实用)
  详细解决方案

Android6.0动态申请权限(简单实用)

热度:84   发布时间:2023-12-12 11:58:45.0

简单代码:

public class LaunchActivity extends AppCompatActivity {@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_launch);if (isGrantSDCardReadPermission()) {//} else {requestPermission();}}private boolean isGrantSDCardReadPermission() {return PermissionUtils.isGrantSDCardReadPermission(this);}private void requestPermission() {PermissionUtils.requestSDCardReadPermission(this, REQUEST_CODE_SDCARD_READ);}private static final int REQUEST_CODE_SDCARD_READ = 1;@Overridepublic void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {switch (requestCode) {case REQUEST_CODE_SDCARD_READ:if (isGrantSDCardReadPermission()) {//} else {Toast.makeText(this, "failure because without sd card read permission", Toast.LENGTH_SHORT).show();}break;default:break;}}}
public class PermissionUtils {/*** 是否有权限** @param context* @return*/public static boolean checkSelfPermission(Context context, String permission) {if (null == context) {return false;}int per = ContextCompat.checkSelfPermission(context, permission);return per == PackageManager.PERMISSION_GRANTED;}/*** Check that all given permissions have been granted by verifying that each entry in the* given array is of the value {@link PackageManager#PERMISSION_GRANTED}.** @see Activity#onRequestPermissionsResult(int, String[], int[])*/public static boolean verifyPermissions(int[] grantResults) {// At least one result must be checked.if (null == grantResults || grantResults.length < 1) {return false;}// Verify that each required permission has been granted, otherwise return false.for (int result : grantResults) {if (result != PackageManager.PERMISSION_GRANTED) {return false;}}return true;}public static boolean isGrantSDCardReadPermission(Context context) {return checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE);}public static void requestSDCardReadPermission(Activity activity, int requestCode) {ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, requestCode);}
}

最后再附上需要动态申请的权限组,每组只要有一个权限申请成功,就默认组内其他权限申请成功了。

group:android.permission-group.CONTACTSpermission:android.permission.WRITE_CONTACTSpermission:android.permission.GET_ACCOUNTS    permission:android.permission.READ_CONTACTSgroup:android.permission-group.PHONEpermission:android.permission.READ_CALL_LOGpermission:android.permission.READ_PHONE_STATE permission:android.permission.CALL_PHONEpermission:android.permission.WRITE_CALL_LOGpermission:android.permission.USE_SIPpermission:android.permission.PROCESS_OUTGOING_CALLSpermission:com.android.voicemail.permission.ADD_VOICEMAILgroup:android.permission-group.CALENDARpermission:android.permission.READ_CALENDARpermission:android.permission.WRITE_CALENDARgroup:android.permission-group.CAMERApermission:android.permission.CAMERAgroup:android.permission-group.SENSORSpermission:android.permission.BODY_SENSORSgroup:android.permission-group.LOCATIONpermission:android.permission.ACCESS_FINE_LOCATIONpermission:android.permission.ACCESS_COARSE_LOCATIONgroup:android.permission-group.STORAGEpermission:android.permission.READ_EXTERNAL_STORAGEpermission:android.permission.WRITE_EXTERNAL_STORAGEgroup:android.permission-group.MICROPHONEpermission:android.permission.RECORD_AUDIOgroup:android.permission-group.SMSpermission:android.permission.READ_SMSpermission:android.permission.RECEIVE_WAP_PUSHpermission:android.permission.RECEIVE_MMSpermission:android.permission.RECEIVE_SMSpermission:android.permission.SEND_SMSpermission:android.permission.READ_CELL_BROADCASTS