Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to request permissions in Android marshmallow at run-time ?

    • 0
    • 1
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 812
    Comment on it

    Android Marshmallow i.e. API level 23 divided the permissions into two type :-

    1. Normal permissions those permissions which do not risk user privacy . If your app lists a normal permission in its manifest, the system grants the permission automatically.
    2. Dangerous permissions those permissions which risk user privacy. They can give the app access to the user's confidential data. If you list a dangerous permission, the user has to explicitly give approval to your app.

    Location permission are comes under dangerous category.So In Marshmallow in order to get the current location of the user you have to request its approval at run time.

    Example of requesting location permission

    • Before fetching current location check if the user has give the permission, like this:-
       if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    
    /*here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
    */
        ActivityCompat.requestPermissions(this,
                        new String[]{ Manifest.permission.ACCESS_COARSE_LOCATION},
                        MY_PERMISSIONS_REQUEST_LOCATION);
    return;
        }
    
    • Now override onRequestPermissionsResult method and check if the user granted the request permission
     @Override
        public void onRequestPermissionsResult(int requestCode,
                                               String permissions[], int[] grantResults) {
            switch (requestCode) {
                case MY_PERMISSIONS_REQUEST_LOCATION: {
                    // If request is cancelled, the result arrays are empty.
                    if (grantResults.length > 0
                            && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
    
                        // permission was granted, yay! Do the
                        // contacts-related task you need to do.
                        getMyLocation();
    
                    } else {
    
                        // permission denied, boo! Disable the
                        // functionality that depends on this permission.
                        Toast.makeText(MapsActivity.this, "Permission denied", Toast.LENGTH_SHORT).show();
                    }
                    return;
                }
    
                // other 'case' lines to check for other
                // permissions this app might request
            }
        }
    

 1 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: