Search code examples
androidandroid-studiolocationruntimeandroid-6.0-marshmallow

How do I get my app to request for permission?


I'm trying to get my app to be able to ask for permissions when a user clicks the request permission button (btRequest). I don't have a problem when building the app but when running it on my phone my app stops. Check permission request works just fine since when I click btCheck I'm able to get a result. What could be the problem with trying to request for permissions?

Logcat file

private Context context;
private Activity activity;
private static final int PERMISSION_REQUEST_CODE = 1;
private View view;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    Button btRequest = (Button)findViewById(R.id.btRequest);
    Button btCheck = (Button)findViewById(R.id.btCheck);
    btRequest.setOnClickListener(this);
    btCheck.setOnClickListener(this);

}

@Override
public void onClick(View v) {

    view = v;

    int id = v.getId();
    switch (id){
        case R.id.btCheck:
            if (checkPermission()) {

                Snackbar.make(view,"Permission already granted.",Snackbar.LENGTH_LONG).show();

            } else {

                Snackbar.make(view,"Please request permission.",Snackbar.LENGTH_LONG).show();
            }
            break;
        case R.id.btRequest:
            if (!checkPermission()) {

                requestPermission();

            } else {

                Snackbar.make(view,"Permission already granted.",Snackbar.LENGTH_LONG).show();

            }
            break;
    }
}

private boolean checkPermission(){
    int result = ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION);
    if (result == PackageManager.PERMISSION_GRANTED){

        return true;

    } else {

        return false;

    }
}

private void requestPermission(){

    if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,Manifest.permission.ACCESS_FINE_LOCATION)){

        Toast.makeText(context,"GPS permission allows us to access location data." +
                " Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show();

    } else {

        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_REQUEST_CODE);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case PERMISSION_REQUEST_CODE:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                Snackbar.make(view,"Permission Granted, Now you can access location data.",Snackbar.LENGTH_LONG).show();

            } else {

                Snackbar.make(view,"Permission Denied, You cannot access location data.",Snackbar.LENGTH_LONG).show();

            }
            break;
    }
}
}

Solution

  • Check build OS version (no need to ask permission for your OS version<23)

    Step 1: (Check OS version)

     if(android.os.Build.VERSION.SDK_INT >= 23) 
        {
            checkAndRequestPermissions();
        }
    

    Step 2: (Asking permission)

    private  boolean checkAndRequestPermissions() 
    {
        int permissionSendMessage = ContextCompat.checkSelfPermission(this,Manifest.permission.CAMERA);
        int locationPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
        int storagePermission = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
        List<String> listPermissionsNeeded = new ArrayList<>();
        if (locationPermission != PackageManager.PERMISSION_GRANTED) {
            listPermissionsNeeded.add(Manifest.permission.ACCESS_FINE_LOCATION);
        }
        if (permissionSendMessage != PackageManager.PERMISSION_GRANTED) {
            listPermissionsNeeded.add(Manifest.permission.CAMERA);
        }
        if (storagePermission != PackageManager.PERMISSION_GRANTED) {
            listPermissionsNeeded.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        }
        if (!listPermissionsNeeded.isEmpty()) {
            ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]),permsRequestCode);
            return false;
        }
        return true;
    }
    

    Step 3: (Override method implementation)

         @Override
        public void onRequestPermissionsResult(int requestCode,
                                               String permissions[], int[] grantResults) {
            switch (requestCode) {
                case 200: {
    
                    Map<String, Integer> perms = new HashMap<>();
                    // Initialize the map with both permissions
                    perms.put(Manifest.permission.CAMERA, PackageManager.PERMISSION_GRANTED);
                    perms.put(Manifest.permission.ACCESS_FINE_LOCATION, PackageManager.PERMISSION_GRANTED);
                    perms.put(Manifest.permission.WRITE_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
                    // Fill with actual results from user
                    if (grantResults.length > 0) {
                        for (int i = 0; i < permissions.length; i++)
                            perms.put(permissions[i], grantResults[i]);
                        // Check for both permissions
                        if (perms.get(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED
                                && perms.get(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                                && perms.get(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
                            // process the normal flow
                            //else any one or both the permissions are not granted
                            waitAndNavigateToOnboardingTutorial();
                        } else {
                            //permission is denied (this is the first time, when "never ask again" is not checked) so ask again explaining the usage of permission
    //                        // shouldShowRequestPermissionRationale will return true
                            //show the dialog or snackbar saying its necessary and try again otherwise proceed with setup.
                            if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA) || ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)
                                    || ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                                showDialogOK("Camera, Storage and Location Services Permission required for this app",
                                        new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                                switch (which) {
                                                    case DialogInterface.BUTTON_POSITIVE:
                                                        checkAndRequestPermissions();
                                                        break;
                                                    case DialogInterface.BUTTON_NEGATIVE:
                                                        // proceed with logic by disabling the related features or quit the app.
                                                        break;
                                                }
                                            }
                                        });
                            }
                            //permission is denied (and never ask again is  checked)
                            //shouldShowRequestPermissionRationale will return false
                            else {
                                Toast.makeText(this, "Go to settings and enable permissions", Toast.LENGTH_LONG)
                                        .show();
                                //proceed with logic by disabling the related features or quit the app.
                            }
                        }
                    }
                }
            }
    
        }
    

    Step 4:

     private void showDialogOK(String message, DialogInterface.OnClickListener okListener) {
        new AlertDialog.Builder(this)
                .setMessage(message)
                .setPositiveButton("OK", okListener)
                .setNegativeButton("Cancel", okListener)
                .create()
                .show();
    }