应用程序在请求运行时权限时关闭

时间:2017-07-04 03:33:07

标签: android android-6.0-marshmallow

在询问用户对运行时的权限时,获取以下叠加对话框。 Extra permission request

以下是我的代码:

if(ContextCompat.checkSelfPermission(MainActivity.this,permissions) == PackageManager.PERMISSION_DENIED) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, permissions)) {
            ActivityCompat.requestPermissions(MainActivity.this,new String[] {permissions}, requestCode);
        } else
            Toast.makeText(MainActivity.this,"Need Permission",Toast.LENGTH_LONG);
    }

清单

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

我要求的权限是CALL_PHONE。

TIA

1 个答案:

答案 0 :(得分:-1)

也许你没有为AndroidManifest添加笔画? 我确实喜欢以下内容: 为AndroidManifest添加了笔画

然后检查程序运行时权限。

MainActivity: verifyStoragePermissions(本);

public static void verifyStoragePermissions(Activity activity) {
    // Check if we have write permission
    int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
    if (permission != PackageManager.PERMISSION_GRANTED) {
        // We don't have permission so prompt the user
        if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
        } else {
            ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
        }
    }
}

然后检查MainActivity中的结果:

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case Utils.REQUEST_EXTERNAL_STORAGE: {
            // 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.
            } else {
                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }
        // other 'case' lines to check for other
        // permissions this app might request
    }
}