允许多个运行时权限

Posted

技术标签:

【中文标题】允许多个运行时权限【英文标题】:Allow Multiple Run Time Permission 【发布时间】:2017-09-23 09:54:05 【问题描述】:

我正在编写一个代码来请求多个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);
                
            
            

    

【问题讨论】:

试试 yourActivity.this ,而不是 context。 【参考方案1】:

尝试将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))  
  //...

【讨论】:

【参考方案2】:

第一个参数是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))

【讨论】:

以上是关于允许多个运行时权限的主要内容,如果未能解决你的问题,请参考以下文章

在运行时通过 chmod 更改权限,出现“不允许操作”错误

当我们有多个活动时如何集中运行时权限

在按下允许按钮获取运行时权限时,它不是在调用操作方法吗?

Activity 在调用运行时权限时变黑

在运行时请求和允许 WRITE_EXTERNAL_STORAGE 权限对当前会话没有影响

Android运行时权限申请快速学习教程