ActivityCompat.requestPermissions not showing dialog box

前端 未结 20 1447
野趣味
野趣味 2020-12-07 17:15
if (ContextCompat.checkSelfPermission(RegisterActivity.this,      Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_DENIED){
            ActivityCom         


        
相关标签:
20条回答
  • 2020-12-07 18:05

    Here's an example of using requestPermissions():

    First, define the permission (as you did in your post) in the manifest, otherwise, your request will automatically be denied:

    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    

    Next, define a value to handle the permission callback, in onRequestPermissionsResult():

    private final int REQUEST_PERMISSION_PHONE_STATE=1;
    

    Here's the code to call requestPermissions():

    private void showPhoneStatePermission() {
        int permissionCheck = ContextCompat.checkSelfPermission(
                this, Manifest.permission.READ_PHONE_STATE);
        if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.READ_PHONE_STATE)) {
                showExplanation("Permission Needed", "Rationale", Manifest.permission.READ_PHONE_STATE, REQUEST_PERMISSION_PHONE_STATE);
            } else {
                requestPermission(Manifest.permission.READ_PHONE_STATE, REQUEST_PERMISSION_PHONE_STATE);
            }
        } else {
            Toast.makeText(MainActivity.this, "Permission (already) Granted!", Toast.LENGTH_SHORT).show();
        }
    }
    

    First, you check if you already have permission (remember, even after being granted permission, the user can later revoke the permission in the App Settings.)

    And finally, this is how you check if you received permission or not:

    @Override
    public void onRequestPermissionsResult(
            int requestCode,
            String permissions[],
            int[] grantResults) {
        switch (requestCode) {
            case REQUEST_PERMISSION_PHONE_STATE:
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(MainActivity.this, "Permission Granted!", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "Permission Denied!", Toast.LENGTH_SHORT).show();
                }
        }
    }
    
    private void showExplanation(String title,
                                 String message,
                                 final String permission,
                                 final int permissionRequestCode) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(title)
                .setMessage(message)
                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        requestPermission(permission, permissionRequestCode);
                    }
                });
        builder.create().show();
    }
    
    private void requestPermission(String permissionName, int permissionRequestCode) {
        ActivityCompat.requestPermissions(this,
                new String[]{permissionName}, permissionRequestCode);
    }
    
    0 讨论(0)
  • 2020-12-07 18:06

    This just happened to me. It turned out I was requesting ALL permissions, when I needed to filter to just DANGEROUS permissions, and it suddenly started working.

    fun requestPermissions() {
        val missingDangerPermissions = PERMISSIONS
                .filter { ContextCompat.checkSelfPermission(this, it) != PackageManager.PERMISSION_GRANTED }
                .filter { this.getPackageManager().getPermissionInfo(it, PackageManager.GET_META_DATA).protectionLevel == PermissionInfo.PROTECTION_DANGEROUS } // THIS FILTER HERE!
    
        if (missingDangerPermissions.isNotEmpty()) {
            Log.i(TAG, "Requesting dangerous permission to $missingDangerPermissions.")
            ActivityCompat.requestPermissions(this,
                    missingDangerPermissions.toTypedArray(),
                    REQUEST_CODE_REQUIRED_PERMISSIONS);
            return
        } else {
            Log.i(TAG, "We had all the permissions we needed (yay!)")
        }
    }
    
    0 讨论(0)
  • 2020-12-07 18:06

    Here is another experience I'd like to share with you guys. The problem showed up after I implemented the following code to check for BLE access permission:

    final String requiredPermission = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && targetSdkVersion >= Build.VERSION_CODES.Q) ?
                android.Manifest.permission.ACCESS_FINE_LOCATION :
                android.Manifest.permission.ACCESS_COARSE_LOCATION;
    

    I meant to distinguish between FINE and COARSE location permission requests, both had been defined in the manifest. When checking for ACCESS_COARSE_LOCATION permission, the "Request permission" dialog never poped up but onRequestPermissionsResult was always called with PERMISSION_DENIED (in case permission was not enabled in app settings). So I removed the check for BUILD.SDK and checked only for ACCESS_FINE_LOCATION and voilla the missing dialog showed up.

    final String requiredPermission = android.Manifest.permission.ACCESS_FINE_LOCATION;
    

    did the trick.

    The Manifest source contains:

    <uses-permission android:name="android.permission.COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    

    but the apk contained android.permission.ACCESS_FINE_LOCATION only. Sometime during build, the COARSE_LOCATION had been removed.

    I hope this might help somebody having the same issue.

    0 讨论(0)
  • 2020-12-07 18:07

    Maybe this solution could help anyone rather than:

    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
    

    use :

    ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
    

    so we add android. to Manifest.permission.WRITE_EXTERNAL_STORAGE

    0 讨论(0)
  • 2020-12-07 18:09

    For me the issue was requesting a group mistakenly instead of the actual permissions.

    0 讨论(0)
  • 2020-12-07 18:11

    For me the issue was I had an invalid request code. I choose 0xDEADBEEF as a request code and it was silently failing (maybe it's cast to something smaller than 32-bit somewhere internally?) If I choose 255 everything worked fine as NightSkyDev described above.

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