HarmonyOS 设置应用权限列表中没有位置权限

首次使用位置权限的时候会去提示申请位置权限,如果没有权限,则显示弹框并跳转到应用详情页手动开启,但是权限列表中没有展示位置权限选项。如图所示:

HarmonyOS 设置应用权限列表中没有位置权限 -鸿蒙开发者社区

HarmonyOS
2024-12-25 11:19:52
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
put_get

当拉起授权弹窗后,只有用户进行了禁止、或者允许的操作之后。才能在设置应用权限列表中展示权限信息。点击获取当前位置。弹出弹窗后选择禁止,再次点击获取当前位置。确认跳转设置页面,勾选使用期间开启精确位置,示例参考如下:

import { abilityAccessCtrl, common, PermissionRequestResult, Permissions, Want } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { geoLocationManager } from '@kit.LocationKit';
import { AlertDialog } from '@ohos.arkui.advanced.Dialog';

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';
  @State location: geoLocationManager.Location | null = null;
  dialogControllerConfirm: CustomDialogController = new CustomDialogController({
    builder: AlertDialog({
      content: '已拒绝访问系统位置,是否前往开启?',
      primaryButton: {
        value: '取消',
        action: () => {
        },
      },
      secondaryButton: {
        value: '确认',
        fontColor: $r('sys.color.ohos_id_color_warning'),
        action: () => {
          this.openAppInfo();
        }
      },
    }),
    autoCancel: true,
    customStyle: true,
    alignment: DialogAlignment.Bottom
  });

  build() {
    RelativeContainer() {
      Button("获取当前位置").onClick(async () => {
        const permissions: Array<Permissions> = [
          'ohos.permission.APPROXIMATELY_LOCATION',
          'ohos.permission.LOCATION'];
        let context: Context = getContext(this) as common.UIAbilityContext;
        let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager();
        // requestPermissionsFromUser会判断权限的授权状态来决定是否唤起弹窗
        atManager.requestPermissionsFromUser(context, permissions).then((data: PermissionRequestResult) => {
          let grantStatus: Array<number> = data.authResults;
          let length: number = grantStatus.length;
          for (let i = 0; i < length; i++) {
            if (grantStatus[i] === 0) {
              // 已经授权,可以继续访问目标操作
              this.getLocation();
            } else {
              // 用户拒绝授权,提示用户必须授权才能访问当前页面的功能,并引导用户到系统设置中打开相应的权限
              this.location = null;
              this.dialogControllerConfirm.open();
              return;
            }
          }
        }).catch((err: BusinessError) => {
          console.error(`Failed to request permissions from user. Code is ${err.code}, message is ${err.message}`);
        })
      }).margin(10)
    }
    .height('100%')
    .width('100%')
  }

  JumpToSettings(pageName: string, context: common.UIAbilityContext) {


    let want: Want = {
      bundleName: 'com.huawei.hmos.settings',
      abilityName: 'com.huawei.hmos.settings.MainAbility',
      uri: pageName,
      parameters: {
        pushParams: {
          bundleName: context.abilityInfo.bundleName // 应用包名
        }
      }

    };
    if (pageName === 'application_info_entry' && want.parameters) {
      want.parameters['pushParams'] = context.abilityInfo.bundleName
    }
    context.startAbility(want);
  }

  openAppInfo() {
    let context = getContext(this) as common.UIAbilityContext;
    this.JumpToSettings('application_info_entry', context)
  };

  getLocation() {
    let requestInfo: geoLocationManager.CurrentLocationRequest = {
      'priority': geoLocationManager.LocationRequestPriority.FIRST_FIX,
      'scenario': geoLocationManager.LocationRequestScenario.UNSET,
      'maxAccuracy': 0
    };
    let locationChange = (err: BusinessError, location: geoLocationManager.Location): void => {
      if (err) {
        console.error('locationChanger: err=' + JSON.stringify(err));
      }
      if (location) {
        this.location = location
      }
    };

    try {
      geoLocationManager.getCurrentLocation(requestInfo, locationChange);
    } catch (err) {
      console.error("errCode:" + JSON.stringify(err));
    }
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
分享
微博
QQ
微信
回复
2024-12-25 13:31:34
相关问题
关于获取应用列表权限问题?
4463浏览 • 1回复 待解决
HarmonyOS 跳转应用权限设置
741浏览 • 1回复 待解决
HarmonyOS 位置权限申请
949浏览 • 1回复 待解决
HarmonyOS 如何打开应用权限设置页面
724浏览 • 1回复 待解决
app如何申请位置权限
1329浏览 • 1回复 待解决
权限设置没有落地页,怎么办?
1214浏览 • 1回复 待解决
HarmonyOS申请用户位置权限问题
1291浏览 • 1回复 待解决
Web组件如何申请位置权限
1327浏览 • 1回复 待解决
HarmonyOS 位置权限变更监听回调问题
933浏览 • 1回复 待解决
HarmonyOS 位置信息访问权限始终允许
1206浏览 • 1回复 待解决
Web中网页如何申请位置权限
1526浏览 • 1回复 待解决
关于权限列表条目缺少问题
2725浏览 • 1回复 待解决