允许多次运行时权限

时间:2017-04-26 06:34:05

标签: android android-permissions runtime-permissions

我正在编写代码以询问群组中的多个Run time permission on android 6.0。一切都很好,我为此做了一些很好的例子,但仍然有问题。

ActivityCompat.shouldShowRequestPermissionRationale (context, READ_PHONE_STATE)中,它对上下文给出错误 错误的第一个参数上下文。请帮忙解决问题。

先谢谢

代码是:

 if (ContextCompat
                    .checkSelfPermission(SpalshActivity.this,
                            READ_PHONE_STATE)+ContextCompat.checkSelfPermission(context,
                    WRITE_EXTERNAL_STORAGE) +ContextCompat.checkSelfPermission(context,
                    CAMERA) + ContextCompat
                    .checkSelfPermission(context,
                           READ_CONTACTS)+ContextCompat
                    .checkSelfPermission(context,
                            CALL_PHONE)+ContextCompat
                    .checkSelfPermission(context,
                            ACCESS_FINE_LOCATION)+ContextCompat
                    .checkSelfPermission(context,
                            READ_SMS)== PackageManager.PERMISSION_GRANTED) {
                myMethod();

            }
               else {
               if (ActivityCompat.shouldShowRequestPermissionRationale
                        (context, READ_PHONE_STATE) ||ActivityCompat.shouldShowRequestPermissionRationale
                        (context, WRITE_EXTERNAL_STORAGE)||
                        ActivityCompat.shouldShowRequestPermissionRationale
                                (context, CAMERA) ||
                        ActivityCompat.shouldShowRequestPermissionRationale
                                (context, READ_CONTACTS) || ActivityCompat.shouldShowRequestPermissionRationale
                        (context, CALL_PHONE) || ActivityCompat.shouldShowRequestPermissionRationale
                        (context, ACCESS_FINE_LOCATION) || ActivityCompat.shouldShowRequestPermissionRationale
                        (context, READ_SMS)) {
                    Snackbar.make(findViewById(android.R.id.content),
                            "Please Grant Permissions",
                            Snackbar.LENGTH_INDEFINITE).setAction("ENABLE",
                            new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    ActivityCompat.requestPermissions(SpalshActivity.this,
                                            new String[]{READ_PHONE_STATE,WRITE_EXTERNAL_STORAGE,CAMERA, READ_CONTACTS, CALL_PHONE, ACCESS_FINE_LOCATION, READ_SMS},
                                            REQUEST_READ_PHONE_STATE);
                                }
                            }).show();
                } else {
                    ActivityCompat.requestPermissions(SpalshActivity.this,
                            new String[]{READ_PHONE_STATE,WRITE_EXTERNAL_STORAGE,CAMERA, READ_CONTACTS, CALL_PHONE, ACCESS_FINE_LOCATION, READ_SMS},
                            REQUEST_READ_PHONE_STATE);
                }
            }
            }

    }

2 个答案:

答案 0 :(得分:9)

第一个参数是android.app.Activity类型,您无法在此处传递context,因此请使用this代替context,如下所示代码: -

if (ActivityCompat.shouldShowRequestPermissionRationale
                        (this, READ_PHONE_STATE) ||ActivityCompat.shouldShowRequestPermissionRationale
                        (this, WRITE_EXTERNAL_STORAGE)||
                        ActivityCompat.shouldShowRequestPermissionRationale
                                (this, CAMERA) ||
                        ActivityCompat.shouldShowRequestPermissionRationale
                                (this, READ_CONTACTS) || ActivityCompat.shouldShowRequestPermissionRationale
                        (this, CALL_PHONE) || ActivityCompat.shouldShowRequestPermissionRationale
                        (this, ACCESS_FINE_LOCATION) || ActivityCompat.shouldShowRequestPermissionRationale
                        (this, READ_SMS))

答案 1 :(得分:0)

尝试将context替换为this

if (ActivityCompat.shouldShowRequestPermissionRationale(this, READ_PHONE_STATE) ||
    ActivityCompat.shouldShowRequestPermissionRationale(this, WRITE_EXTERNAL_STORAGE) || 
    ActivityCompat.shouldShowRequestPermissionRationale(this, CAMERA) ||  
    ActivityCompat.shouldShowRequestPermissionRationale(this, READ_CONTACTS) || 
    ActivityCompat.shouldShowRequestPermissionRationale(this, CALL_PHONE) ||  
    ActivityCompat.shouldShowRequestPermissionRationale(this, ACCESS_FINE_LOCATION) || 
    ActivityCompat.shouldShowRequestPermissionRationale(this, READ_SMS))  {
  //...
}