When to request permission at runtime for Android Marshmallow 6.0?

后端 未结 11 852
故里飘歌
故里飘歌 2020-12-06 05:54

I am testing my app on Marshmallow 6.0 and it\'s getting force closed for the android.permission.READ_EXTERNAL_STORAGE, even if it is defined in th

相关标签:
11条回答
  • 2020-12-06 06:10

    For requesting runtime permission i use GitHub Library

    Add library in Build.gradle file

    dependencies {
         compile 'gun0912.ted:tedpermission:1.0.3'
    }
    

    Create Activity and add PermissionListener

    public class MainActivity extends AppCompatActivity{
    
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
    
    
        PermissionListener permissionlistener = new PermissionListener() {
            @Override
            public void onPermissionGranted() {
                Toast.makeText(RationaleDenyActivity.this, "Permission Granted", Toast.LENGTH_SHORT).show();
                //Camera Intent and access Location logic here
            }
    
            @Override
            public void onPermissionDenied(ArrayList<String> deniedPermissions) {
                Toast.makeText(RationaleDenyActivity.this, "Permission Denied\n" + deniedPermissions.toString(), Toast.LENGTH_SHORT).show();
            }
        };
    
    
        new TedPermission(this)
                .setPermissionListener(permissionlistener)
                .setRationaleTitle(R.string.rationale_title)
                .setRationaleMessage(R.string.rationale_message) // "we need permission for access camera and find your location"
                .setDeniedTitle("Permission denied")
                .setDeniedMessage("If you reject permission,you can not use this service\n\nPlease turn on permissions at [Setting] > [Permission]")
                .setGotoSettingButtonText("Settings")
                .setPermissions(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .check();
       }
    }
    

    string.xml

    <resources>
    
        <string name="rationale_title">Permission required</string>
        <string name="rationale_message">we need permission for read <b>camera</b> and find your <b>location</b></string>
    
    </resources>
    
    0 讨论(0)
  • 2020-12-06 06:12

    Do like this

    private static final int  REQUEST_ACCESS_FINE_LOCATION = 111;
    

    In your onCreate

    boolean hasPermissionLocation = (ContextCompat.checkSelfPermission(getApplicationContext(),
                Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED);
        if (!hasPermissionLocation) {
            ActivityCompat.requestPermissions(ThisActivity.this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    REQUEST_ACCESS_FINE_LOCATION);
        }
    

    then check result

       @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode)
        {
    
            case REQUEST_ACCESS_FINE_LOCATION: {
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
                {
                    Toast.makeText(ThisActivity.this, "Permission granted.", Toast.LENGTH_SHORT).show();
    
                    //reload my activity with permission granted
                    finish();
                    startActivity(getIntent());
    
                } else
                {
                    Toast.makeText(ThisActivity.this, "The app was not allowed to get your location. Hence, it cannot function properly. Please consider granting it this permission", Toast.LENGTH_LONG).show();
                }
            }
    
        }
    
    }
    
    0 讨论(0)
  • 2020-12-06 06:12
    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);
            }
    
    0 讨论(0)
  • 2020-12-06 06:18

    https://material.io/guidelines/patterns/permissions.html This link will give you different type of scenario where permissions can be asked. Choose accordingly to your needs.

    0 讨论(0)
  • 2020-12-06 06:22
        Android Easy Runtime Permissions with Dexter:
        1. Dexter Permissions Library
    
        To get started with Dexter, add the dependency in your build.gradle
    
        dependencies {
            // Dexter runtime permissions
            implementation 'com.karumi:dexter:4.2.0'
        }
    
        1.1 Requesting Single Permission
        To request a single permission, you can use withPermission() method by passing the required permission. You also need a PermissionListener callback to receive the state of the permission.
    
        > onPermissionGranted() will be called once the permission is granted.
    
        > onPermissionDenied() will be called when the permission is denied. Here you can check whether the permission is permanently denied by using response.isPermanentlyDenied() condition.
    
        The below code requests CAMERA permission.
    
    Dexter.withActivity(this)
                    .withPermission(Manifest.permission.CAMERA)
                    .withListener(new PermissionListener() {
                        @Override
                        public void onPermissionGranted(PermissionGrantedResponse response) {
                            // permission is granted, open the camera
                        }
    
                        @Override
                        public void onPermissionDenied(PermissionDeniedResponse response) {
                            // check for permanent denial of permission
                            if (response.isPermanentlyDenied()) {
                                // navigate user to app settings
                            }
                        }
    
                        @Override
                        public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {
                            token.continuePermissionRequest();
                        }
                    }).check();
    
    1.2 Requesting Multiple Permissions
    To request multiple permissions at the same time, you can use withPermissions() method. Below code requests STORAGE and LOCATION permissions.
    
    Dexter.withActivity(this)
                    .withPermissions(
                            Manifest.permission.READ_EXTERNAL_STORAGE,
                            Manifest.permission.WRITE_EXTERNAL_STORAGE,
                            Manifest.permission.ACCESS_FINE_LOCATION)
                    .withListener(new MultiplePermissionsListener() {
                        @Override
                        public void onPermissionsChecked(MultiplePermissionsReport report) {
                            // check if all permissions are granted
                            if (report.areAllPermissionsGranted()) {
                                // do you work now
                            }
    
                            // check for permanent denial of any permission
                            if (report.isAnyPermissionPermanentlyDenied()) {
                                // permission is denied permenantly, navigate user to app settings
                            }
                        }
    
                        @Override
                        public void onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions, PermissionToken token) {
                            token.continuePermissionRequest();
                        }
                    })
                    .onSameThread()
                    .check();
    
    0 讨论(0)
  • 2020-12-06 06:24

    In general, request needed permissions it as soon as you need them. This way you can inform the user why you need the permission and handle permission denies much easier.

    Think of scenarios where the user revokes the permission while your app runs: If you request it at startup and never check it later this could lead to unexpected behaviour or exceptions.

    0 讨论(0)
提交回复
热议问题