Android授权每次授权三次,应用程序在授予权限后关闭

时间:2017-09-14 10:43:54

标签: java android permissions

我正在创建一个简单的应用程序,其中需要READ_CONTACT和CALL_PHONE权限。我写了下面的代码。

安装后,应用程序请求权限3次 -

1 of 2 read contacts
2 of 2 call and manage phone 


1 of 2 read contacts
2 of 2 call and manage phone 


1 of 2 read contacts
2 of 2 call and manage phone

此外,在授予这些权限后,应用程序也无法打开。但是,当我再次打开应用程序时,它工作正常,并且不会再次询问权限。

我有以下代码

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode) {
        case MULTIPLE_REQUESTS: {
            if (grantResults.length > 0) {
                boolean contactPermission = grantResults[1] == PackageManager.PERMISSION_GRANTED;
                boolean phonePermission = grantResults[0] == PackageManager.PERMISSION_GRANTED;

                if (contactPermission && phonePermission) {
                    // write your logic here
                } else {
                    Toast.makeText(this, "Read Contact & Call phone permissions are required", Toast.LENGTH_SHORT).show();
                    closeNow();
                }
            }
            break;
        }
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Here, thisActivity is the current activity
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)
            + ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE)
            != PackageManager.PERMISSION_GRANTED) {

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

        } else {
            // No explanation needed, we can request the permission.
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.READ_CONTACTS, Manifest.permission.CALL_PHONE},
                    MULTIPLE_REQUESTS);
        }


    }

    setContentView(R.layout.activity_contact_app_bar);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    toolbar.setTitle(getTitle());

}

2 个答案:

答案 0 :(得分:0)

调用相应功能时应询问权限。

由于closeNow被调用,我怀疑该应用程序已关闭。

您需要调试以下代码。

if(grantResults.length> 0){                 boolean contactPermission = grantResults [1] == PackageManager.PERMISSION_GRANTED;                 boolean phonePermission = grantResults [0] == PackageManager.PERMISSION_GRANTED;

            if (contactPermission && phonePermission) {
                // write your logic here
            } else {
                Toast.makeText(this, "Read Contact & Call phone permissions are required", Toast.LENGTH_SHORT).show();
                closeNow();
            }
        }

答案 1 :(得分:-1)

尝试此代码

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
         ActivityCompat.requestPermissions(this,
            new String[]{ Manifest.permission.READ_CONTACTS, Manifest.permission.CALL_PHONE},
            MULTIPLE_REQUESTS);
    }

将上述代码添加到您需要权限的位置

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MULTIPLE_REQUESTS: {
            for (int i = 0; i < grantResults.length; i++) {
                if (grantResults[i] !=
                                PackageManager.PERMISSION_GRANTED) {
                     // Permission has been denied by user
                } else {
                    // Permission has been granted by user
                }
                return;
            }
        }
    }
}
相关问题