Android marshmallow request permission?

后端 未结 24 2057
無奈伤痛
無奈伤痛 2020-11-21 06:40

I am currently working on an application that requires several \"dangerous\" permissions. So I tried adding \"ask for permission\" as required in Android Marshmallow(API Lev

24条回答
  •  无人共我
    2020-11-21 07:18

    Starting in Android Marshmallow, we need to request the user for specific permissions. We can also check through code if the permission is already given. Here is a list of commonly needed permissions:

    • android.permission_group.CALENDAR

      • android.permission.READ_CALENDAR
      • android.permission.WRITE_CALENDAR
    • android.permission_group.CAMERA

      • android.permission.CAMERA
    • android.permission_group.CONTACTS

      • android.permission.READ_CONTACTS
      • android.permission.WRITE_CONTACTS
      • android.permission.GET_ACCOUNTS
    • android.permission_group.LOCATION

      • android.permission.ACCESS_FINE_LOCATION
      • android.permission.ACCESS_COARSE_LOCATION
    • android.permission_group.MICROPHONE

      • android.permission.RECORD_AUDIO
    • android.permission_group.PHONE

      • android.permission.READ_PHONE_STATE
      • android.permission.CALL_PHONE
      • android.permission.READ_CALL_LOG
      • android.permission.WRITE_CALL_LOG
      • android.permission.ADD_VOICEMAIL
      • android.permission.USE_SIP
      • android.permission.PROCESS_OUTGOING_CALLS
    • android.permission_group.SENSORS

      • android.permission.BODY_SENSORS
    • android.permission_group.SMS

      • android.permission.SEND_SMS
      • android.permission.RECEIVE_SMS
      • android.permission.READ_SMS
      • android.permission.RECEIVE_WAP_PUSH
      • android.permission.RECEIVE_MMS
      • android.permission.READ_CELL_BROADCASTS
    • android.permission_group.STORAGE

      • android.permission.READ_EXTERNAL_STORAGE
      • android.permission.WRITE_EXTERNAL_STORAGE

    Here is sample code to check for permissions:

    if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
        if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, Manifest.permission.WRITE_CALENDAR)) {
            AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
            alertBuilder.setCancelable(true);
            alertBuilder.setMessage("Write calendar permission is necessary to write event!!!");
            alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
                public void onClick(DialogInterface dialog, int which) {
                    ActivityCompat.requestPermissions((Activity)context, new String[]{Manifest.permission.WRITE_CALENDAR}, MY_PERMISSIONS_REQUEST_WRITE_CALENDAR);
                }
            });
        } else {
            ActivityCompat.requestPermissions((Activity)context, new String[]{Manifest.permission.WRITE_CALENDAR}, MY_PERMISSIONS_REQUEST_WRITE_CALENDAR);
        }
    }
    

提交回复
热议问题