如何跳转到权限设置页面

2024-11-07 15:10:25
118次阅读
0个评论

如何跳转到权限设置页面

今天开发者在群里问,如何跳转到权限设置页面,那么我们就来以麦克分权限为例,一起来看一下

当应用通过requestPermissionsFromUser()拉起弹框请求用户授权时,用户拒绝授权。应用将无法再次通过requestPermissionsFromUser拉起弹框,需要用户在系统应用“设置”的界面中,手动授予权限。

如下是第一次请求权限

第一次请求权限

首先配置moudle.json5

"requestPermissions": [
  {
    "name": "ohos.permission.MICROPHONE",
    "reason": "$string:PERMISSION_MICROPHONE",
    "usedScene": {
      "abilities": [
        "EntryAbility"
      ],
      "when": "inuse"
    },
  }
],

接下来就是代码中申请。

import isLeapYear from "@nutpi/leap-year"
import { abilityAccessCtrl, PermissionRequestResult, common } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';


@Entry
@Component
struct Index {
  @State flag: boolean = true;

  build() {
    Row() {
      Column() {
        Button("拒绝申请权限").onClick(() => {


          let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager();
          let context: Context = getContext(this) as common.UIAbilityContext;
          atManager.requestPermissionsFromUser(context, ['ohos.permission.MICROPHONE'],
            (err: BusinessError, data: PermissionRequestResult) => {
              if (err) {
                console.error(`requestPermissionsFromUser fail, err->${JSON.stringify(err)}`);
              } else {
                console.info('data:' + JSON.stringify(data));
                console.info('data permissions:' + data.permissions);
                console.info('data authResults:' + data.authResults);
                console.info('data dialogShownResults:' + data.dialogShownResults);
              }
            });
        })


      }
      .width('100%')
    }
    .height('100%')
  }
}

第二次申请

当点击拒绝以后就可以使用如下代码,在“设置”应用中的路径:

  • 路径一:设置 > 隐私与安全 > 权限类型(如麦克风) > 具体应用
  • 路径二:设置 > 应用和元服务 > 某个应用

应用也可以通过调用requestPermissionOnSetting(),直接拉起权限设置弹框,引导用户授予权限。

import isLeapYear from "@nutpi/leap-year"
import { abilityAccessCtrl, PermissionRequestResult, common } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';


@Entry
@Component
struct Index {
  @State flag: boolean = true;

  build() {
    Row() {
      Column() {
 

        Button("被拒绝后申请麦克风权限").onClick(() => {


          let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager();
          let context: Context = getContext(this) as common.UIAbilityContext;
          atManager.requestPermissionOnSetting(context, ['ohos.permission.MICROPHONE'])
            .then((data: Array<abilityAccessCtrl.GrantStatus>) => {
              console.info('data:' + JSON.stringify(data));
            })
            .catch((err: BusinessError) => {
              console.error('data:' + JSON.stringify(err));
            });
        })

        Text(this.flag ? "坚果" : "坚果派")
          .fontSize(50)
          .fontWeight(FontWeight.Bold).onClick(() => {

          this.flag = isLeapYear(2014);


        })
      }
      .width('100%')
    }
    .height('100%')
  }
}

完整代码

import isLeapYear from "@nutpi/leap-year"
import { abilityAccessCtrl, PermissionRequestResult, common } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';


@Entry
@Component
struct Index {
  @State flag: boolean = true;

  build() {
    Row() {
      Column() {
        Button("拒绝申请权限").onClick(() => {


          let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager();
          let context: Context = getContext(this) as common.UIAbilityContext;
          atManager.requestPermissionsFromUser(context, ['ohos.permission.MICROPHONE'],
            (err: BusinessError, data: PermissionRequestResult) => {
              if (err) {
                console.error(`requestPermissionsFromUser fail, err->${JSON.stringify(err)}`);
              } else {
                console.info('data:' + JSON.stringify(data));
                console.info('data permissions:' + data.permissions);
                console.info('data authResults:' + data.authResults);
                console.info('data dialogShownResults:' + data.dialogShownResults);
              }
            });
        })

        Button("被拒绝后申请麦克风权限").onClick(() => {


          let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager();
          let context: Context = getContext(this) as common.UIAbilityContext;
          atManager.requestPermissionOnSetting(context, ['ohos.permission.MICROPHONE'])
            .then((data: Array<abilityAccessCtrl.GrantStatus>) => {
              console.info('data:' + JSON.stringify(data));
            })
            .catch((err: BusinessError) => {
              console.error('data:' + JSON.stringify(err));
            });
        })

        Text(this.flag ? "坚果" : "坚果派")
          .fontSize(50)
          .fontWeight(FontWeight.Bold).onClick(() => {

          this.flag = isLeapYear(2014);


        })
      }
      .width('100%')
    }
    .height('100%')
  }
}

参考

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-abilityaccessctrl-V5#requestpermissionsfromuser9

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/request-user-authorization-second-V5

作者:坚果 链接:https://www.nutpi.net/ 來源:坚果派 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处

收藏00

登录 后评论。没有帐号? 注册 一个。