0

I am working on a demo application that ask run time permission on android Marshmallow . I just when when user denial the permission user get an alert dialog again again or at least five clicks . If he denied more than five time application would stop.

 protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActionBar actionBar = getSupportActionBar();
        if(null != actionBar){
            actionBar.hide();
        }
        if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.RECORD_AUDIO}, REQUEST_INTERNET);
        }
        htmlWebView = (WebView)findViewById(R.id.webView);
        assert htmlWebView != null;
        WebSettings webSetting = htmlWebView.getSettings();
        webSetting.setJavaScriptEnabled(true);
        webSetting.setDisplayZoomControls(true);
        htmlWebView.setWebViewClient(new CustomWebViewClient());
        htmlWebView.loadUrl("https://inducesmile.com/blog");
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == REQUEST_INTERNET) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED
                    ) {
                //start audio recording or whatever you planned to do
            }else if (grantResults[0] == PackageManager.PERMISSION_DENIED){
                if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.RECORD_AUDIO)) {
                    //Show an explanation to the user *asynchronously*
                    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setMessage("This permission is important to record audio.")
                            .setTitle("Important permission required");
                    builder.setNegativeButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {

                            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.RECORD_AUDIO}, REQUEST_INTERNET);
                            dialog.dismiss();

                        }
                    }
                    );

                    builder.show();

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

                }else{
                    //Never ask again and handle your app without permission.
                }
            }
        }
    }

Thanks for any help .

Abhishek Bhardwaj
  • 1,164
  • 3
  • 14
  • 39

1 Answers1

1

try this code:

 int count =0;

public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case REQUEST_INTERNET: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                //do your task here

            } else {
                permission_denied();
            }
            break;
        }
        // other 'case' lines to check for other
        // permissions this app might request
    }
}

public void permission_denied() {
    // permission was not granted
    //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
   if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
            Manifest.permission.RECORD_AUDIO)) {
        showDialogOK("This permission is important to record audio.",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch (which) {
                            case DialogInterface.BUTTON_POSITIVE:
                                //user enables permisssion do your task..
                                break;
                            case DialogInterface.BUTTON_NEGATIVE:
                                // proceed with logic by disabling the related features or quit the app.
                                if(count==5){
                                    finish();
                                    //finish activity
                                    }
                                else{
                                     ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.RECORD_AUDIO}, REQUEST_INTERNET);
                                     ++count;
                                 }
                                break;
                        }
                    }
                });
    } //permission is denied (and never ask again is  checked)
    //shouldShowRequestPermissionRationale will return false
    else {
        Toast.makeText(getApplicationContext(), "Go to settings and enable record audio permissions", Toast.LENGTH_LONG).show();
    }
}


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

If user press Never ask again.. permission dialog will not show again

This is expected behaviour.

From the documentation:

When the system asks the user to grant a permission, the user has the option of telling the system not to ask for that permission again. In that case, any time an app uses requestPermissions() to ask for that permission again, the system immediately denies the request. The system calls your onRequestPermissionsResult() callback method and passes PERMISSION_DENIED, the same way it would if the user had explicitly rejected your request again. This means that when you call requestPermissions(), you cannot assume that any direct interaction with the user has taken place.

see this also:Does checking the Never ask again box when asking for a runtime permission disable future dialogs?

Community
  • 1
  • 1
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
  • Getting an exception : Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. – Abhishek Bhardwaj Jan 04 '17 at 08:29
  • try changing getApplicationContext() to this...@Champandorid see the edit – rafsanahmad007 Jan 04 '17 at 08:51
  • Worked fine but when user checked the checkbox He is able to go into the app I want it will popup either he checked the checkbox. and thanks for your precious time. – Abhishek Bhardwaj Jan 04 '17 at 10:19
  • Doesn't work application go ahead . and dialog box doesn't popup again. – Abhishek Bhardwaj Jan 04 '17 at 10:51
  • @Champandorid you can not show dialog after user press never ask again..you can automatically navigate the user to the settings...to enable permission from there..and also it is bad Ux practice – rafsanahmad007 Jan 04 '17 at 11:04
  • are you sure about this? sir. because one of my senior said that your have to do that and it's possible. – Abhishek Bhardwaj Jan 04 '17 at 11:07
  • no i don't think so...you can check google ...but in android developer documentation there is no feature to show permission request dialog again if user check never ask again.. – rafsanahmad007 Jan 04 '17 at 11:09
  • i also tried to show dialog after never ask again and do not find a way..user has to explicitly go to settings and enable app permission from there..thnkx – rafsanahmad007 Jan 04 '17 at 11:11
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/132283/discussion-between-champandorid-and-rafsanahmad007). – Abhishek Bhardwaj Jan 04 '17 at 11:11