DevEcoStudio: Permission application for Hongmeng system


foreword

My first article about Hongmeng system, this article mainly talks about the application of Hongmeng system permissions


Tip: If there are any problems in the article, please correct me, Huawei's official permission application document: Development-Permission Guidance

1. Application for permission

The Android permission application process is: first statically declare in the manifest file, and then dynamically apply for permission in the code according to whether version adaptation is required.
The ohos system is also similar to this process, but its permission application is a little more cumbersome, and dynamic code application is a must.

2. Application steps

1. Static declaration

Openconfig.jsonfile, and then declare the permissions in the file.
What we are applying for here is the camera permission, and other permissions can be found in the official documents.

config.json file

The config.json code is as follows (example):
If you want to apply for multiple permissions, just separate them with {}.

"module": {
    
    
    "reqPermissions": [
      {
    
    
        "name": "ohos.permission.CAMERA",
        "reason": "需要拍摄照片",
        "usedScene":{
    
    
          "ability": ["com.example.helloworld.MainAbility"],
          "when": "always"
        }
      }
    ],{
    
    
            ...
            }
        ]
    }
}

Official detailed description of the field
①name: Find the corresponding name according to the permissions you need.
②reason: If you are applying for user_grant permission, you must fill it in, allowing users to see the reason you need permission.
③In usedScene: ability: input the path name of the ability that can be used; when: use time

2. Code dynamic application permission

The code is as follows (example):

if (verifySelfPermission("ohos.permission.CAMERA") != IBundleManager.PERMISSION_GRANTED) {
    
    
            //应用权限未被授予
            if (canRequestPermission("ohos.permission.CAMERA")) {
    
    
                //验证是否可以申请弹窗授权(首次申请或者用户未选择禁止且不再提示)
                requestPermissionsFromUser(new String[]{
    
    "ohos.permission.CAMERA"}, MY_PERMISSION_REQUEST_CAMERA);
            } else {
    
    
                //显示应用权限需要权限的理由,提示用户进入设置授权
                 new ToastDialog(this).setText("请前往设置授予相机限").setDuration(2000).show();
            }
        } else {
    
    
            new ToastDialog(this).setText("相机权限已被授予").setDuration(2000).show();
        }

insert image description hereThe last thing to pay attention to is the ToastDialog that comes with DevEcoStudio. When using it, you must remember to set the extension time for him (setDuration()), otherwise problems such as not displaying will occur.

 new ToastDialog(this).setText("请前往设置授予相机限").setDuration(2000).show();

Summarize

Tip: Here is a summary of the article:
the above is the entire content of this blog. It is the first time to learn Hongmeng system. If you have any questions, please give us advice in the comment area.

Guess you like

Origin blog.csdn.net/XL1583135614/article/details/114310627