拒绝Android Marshmallow中的运行时权限崩溃应用程序

时间:2016-08-02 08:06:45

标签: android android-6.0-marshmallow android-permissions

我在Android Marshmallow中处理运行时权限时遇到了麻烦,我真的需要一只手。问题是当我拒绝运行时权限时(当对话框出现时),应用程序崩溃了。更详细地,我正在运行的活动重新启动(我只是将其称为应用程序崩溃?)。

以下是代码:

public void showContacts() {

    if (CommonData.showLogs) {
        Log.d(debugTag, "Show contacts button pressed. Checking permissions.");
    }

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)
            != PackageManager.PERMISSION_GRANTED
            || ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_CONTACTS)
            != PackageManager.PERMISSION_GRANTED) {

        requestContactsPermissions();
    } else {

        if (CommonData.showLogs) {
            Log.d(debugTag, "Contact permissions have already been granted. Displaying contact details.");
        }

        // Do my Stuff
    }
}

private void requestContactsPermissions() {

    if (ActivityCompat.shouldShowRequestPermissionRationale(this,
            Manifest.permission.READ_CONTACTS)
            || ActivityCompat.shouldShowRequestPermissionRationale(this,
            Manifest.permission.WRITE_CONTACTS)) {

        Snackbar.make(rLParent, R.string.permission_contacts_rationale,
                Snackbar.LENGTH_INDEFINITE)
                .setAction(R.string.ok, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        ActivityCompat
                                .requestPermissions(ImportContactsActivity.this, PERMISSIONS_CONTACT,
                                        REQUEST_CONTACTS);
                    }
                })
                .show();
    } else {

        ActivityCompat.requestPermissions(ImportContactsActivity.this, PERMISSIONS_CONTACT, REQUEST_CONTACTS);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {


    if (requestCode == REQUEST_CONTACTS) {

        if (PermissionUtil.verifyPermissions(grantResults)) {

            // Do my Stuff
        } else {



            Snackbar.make(rLParent, R.string.permissions_not_granted,
                    Snackbar.LENGTH_SHORT)
                    .show();

        }

    } else {
        Log.d(debugTag, "Contacts permissions were NOT granted.");
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

}

我在这里做错了什么? 任何帮助将不胜感激。非常感谢你!

1 个答案:

答案 0 :(得分:1)

尝试在Try-catch块中使用Permission执行操作,并在出现SecurityException时显示消息或其他内容。

例如......在询问权限后,我使用了以下位置权限...

Errortext是一个显示在屏幕中心的文本视图,用于显示用户需要做什么......

            try {

                LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);


            }catch (SecurityException se){
                Log.d("FragmentCreate","You don't have permissions");

                errortext.setVisibility(View.VISIBLE);
                errortext.setText("Please provide Location permission to continue, Settings->Apps->RecommendedApp->Permissions");
                Toast.makeText(this,"Please provide location permissions to continue",Toast.LENGTH_SHORT).show();
            }

可替换地:

你还可以在catch块中显示一个dialogfragment,其中包含一个按钮,可以将用户带到app的设置屏幕。你可以执行startActivityForResult()并等待返回的值并检查权限是否存在