【问题标题】:Requesting Runtime Permission from Custom Dialog in Android从 Android 中的自定义对话框请求运行时权限
【发布时间】:2019-08-13 02:33:01
【问题描述】:

我正在开发一个应用程序,它显示了一个自定义的笔记列表视图和一个用于添加新笔记的按钮。当用户单击按钮时,应用程序会显示一个全屏对话框,以添加带有一些额外细节的注释。用户还可以通过单击附件按钮上传带有注释的附件。但问题是在 Android M(API 23) 之后此任务需要运行时权限。

根据Google Developers,权限请求的结果将通过onRequestPermissionsResult()方法传递。我不知道如何在我用来显示全屏对话框的方法中得到这个结果。

这就是我的方法的样子:

private void showCreateNoteDialog() {

    //create dialog body...
    final Dialog createDialog = new Dialog(NotesActivity.this,R.style.MyFullscreenDialog);
    createDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    LayoutInflater inflater = this.getLayoutInflater();
    createDialog.setContentView(inflater.inflate(R.layout.customdialog_createNote, null));
    createDialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);}

编辑:

上传文件需要的权限是从SD卡或外部存储读取

【问题讨论】:

  • 您无需请求权限即可显示对话框。您的应用在尝试显示对话框时崩溃了吗?
  • 你想申请什么权限??
  • @SrikarReddy 抱歉我对问题的分类描述,但我需要从用户的 SD 卡读取权限才能上传附注附件。
  • 可以在上传文件onClickListener中添加权限检查
  • @Basi,是的,我可以在 onClickListener 中添加权限。但我无法在侦听器中获得权限请求的结果。

标签: android dialog android-permissions


【解决方案1】:

使用一些有用的库可以更轻松地集成运行时权限,我最喜欢的是PermissionUtils

你需要做的就是在应用级别编译依赖build.gradle

dependencies {
    compile 'rebus:permission-utils:1.0.3'
}

然后在onCreate你的活动中

PermissionManager.with(YourActivity.this)
    .permission(PermissionEnum.READ_PHONE_STATE, PermissionEnum.READ_EXTERNAL_STORAGE) // You can put all permissions here
    .askagain(true)
    .ask();

就是这样。您可以找到更多信息here

【讨论】:

    【解决方案2】:

    试试这样的。

    if (ContextCompat.checkSelfPermission(thisActivity,
                Manifest.permission.READ_EXTERNAL_STORAGE)
        != PackageManager.PERMISSION_GRANTED) {
    
        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
            Manifest.permission.READ_EXTERNAL_STORAGE)) {
    
            // Show an explanation to the user *asynchronously* -- don't block
            // this thread waiting for the user's response! After the user
            // sees the explanation, try again to request the permission.
    
        } else {
    
            // No explanation needed, we can request the permission.
    
            ActivityCompat.requestPermissions(thisActivity,
                new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
    
            // MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE is an
            // app-defined int constant. The callback method gets the
            // result of the request.
        }
    }
    

    这就是您实现运行时权限的方式。欲了解更多信息,请查看official developer site。另外,请查看Normal and Dangerous Permissions 以了解更多关于在运行时需要询问哪些权限。

    【讨论】:

    • 获得结果怎么样?如何在我们的方法中返回结果?
    • 用户接受请求后,您可以使用FileInputStream 从 SD 卡中读取数据。我希望这有助于stackoverflow.com/a/10550036/4402462
    【解决方案3】:

    您无法直接在 showCreateNoteDialog 方法中得到结果,但这是您可以做的(伪代码):

    showCreateNoteDialog() {
        /* some code goes here */
        if (checkPermission) {
            newMethodCalledFromNoteDialog();
        } else {
            requestPermission();
           // return?
        }
    }
    
    newMethodCalledFromNoteDialog() {
            // part of code which needs permission
            // some code depending on previous code
    }
    
    onRequestPermissionsResult() {
        if (permissionGranted) {
            newMethodCalledFromNoteDialog();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-03-28
      • 1970-01-01
      • 1970-01-01
      • 2016-01-20
      • 2016-01-21
      • 2015-11-12
      • 1970-01-01
      • 1970-01-01
      • 2020-01-28
      相关资源
      最近更新 更多