android - 允许多个运行时权限

标签 android android-permissions runtime-permissions

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

    }

最佳答案

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

关于android - 允许多个运行时权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43626674/

相关文章:

java - 在 Android >6.0 中第二次访问图库中的图像时出现安全权限异常

android - 使用 ContentValues 数组批量插入

flutter - Flutter : PlatformException(no_available_camera,没有可用于拍照的相机。,null,null)

android - 请求应用程序更新许可

android - React Native - 无法使用 PermissionsAndroid 请求位置权限

Android 应用程序在开始使用 SQLite 数据库时启动后立即崩溃

android - 如何在 Android 上以 ListView 显示文件

java - Android Studio 显示错误加载 : id=gralloc ! = hmi->id=gralloc

android - cordova 中所有平台的通用 www 文件夹和所有文件