如何反复显示警告对话框用户单击确定?

时间:2017-01-04 05:54:45

标签: android android-studio android-permissions

我正在研究一个在android上询问运行时权限的演示应用程序 棉花糖。我就是当用户拒绝权限用户再次获得警告对话框或至少五次点击时。如果他拒绝超过五次申请就会停止。

 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.
                }
            }
        }
    }

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

试试这段代码:

 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();
}

如果用户按Never ask again ..权限对话框将不再显示

这是预期的行为。

From the documentation:

  

当系统要求用户授予权限时,用户具有   告诉系统不再要求该许可的选项。在   这种情况下,任何时候应用程序使用requestPermissions()来请求   再次允许,系统立即拒绝该请求。该   系统调用你的onRequestPermissionsResult()回调方法和   传递PERMISSION_DENIED,就像用户一样   明确拒绝了您的请求。这意味着当你打电话时   requestPermissions(),你不能假设任何直接的交互   用户已经发生了。

另见:Does checking the Never ask again box when asking for a runtime permission disable future dialogs?