googlesamples / easypermissions

Simplify Android M system permissions
https://firebaseopensource.com/projects/googlesamples/easypermissions/
Apache License 2.0
9.87k stars 1.46k forks source link

how to define RC_CAMERA_AND_LOCATION, and what is the value of 'camera_and_location_rationale' in example #329

Closed astraydog closed 3 years ago

astraydog commented 3 years ago

Basic Information

Device type: Emulator____ OS version: android 9.0 EasyPermissions version: 2.0.1

Describe the problem

What I want is record the screen actions and save it to /storage/emulated/0/Download/. how to define RC_CAMERA_AND_LOCATION? It says Cannot resolve symbol 'RC_CAMERA_AND_LOCATION'. And what is the value of 'R.string.camera_and_location_rationale'

I have define permissions in AndroidManifest.xml.

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" 

Code and logs

// TODO(you): show the code that produces the problem,
@AfterPermissionGranted(RC_CAMERA_AND_LOCATION)
private void methodRequiresTwoPermission() {
    String[] perms = {Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION};
    if (EasyPermissions.hasPermissions(this, perms)) {
        // Already have permission, do the thing
        // ...
    } else {
        // Do not have permissions, request them now
        EasyPermissions.requestPermissions(this, getString(R.string.camera_and_location_rationale),
                RC_CAMERA_AND_LOCATION, perms);
    }
}
samtstern commented 3 years ago

@astraydog RC_CAMERA_AND_LOCATION can be any integer. RC_ means "Request Code" and it's how we'll uniquely identify the permission request.

So you can just do:

// Value really doesn't matter as long as it's unique!
public static final int RC_CAMERA_AND_LOCATION = 123;

R.string_camera_and_location_rationale is just a custom string you want to show to the user explaining why you want the permission. It could be something like this: https://github.com/googlesamples/easypermissions/blob/2b2b5c40d11caf601c89da0af995f57772a2e52b/app/src/main/res/values/strings.xml#L6