调试证书动态申请定位相关权限不弹窗问题

不弹窗直接返回 03-27 17:41:42.216 42683-42683 A03D00/JSAPP com.heao....jiaohome I requestPermissionsFromUser => [2,2,2]。

HarmonyOS
2024-11-08 11:06:13
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

​由于安全隐私要求,应用不能通过弹窗的形式被授予后台位置权限,应用如果需要使用后台位置权限,需要引导用户到设置界面手动授予。

参考链接:​https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/permissions-for-all-V5

const permissions1: Array<Permissions> = ['ohos.permission.LOCATION_IN_BACKGROUND']; 
const permissions3: Array<Permissions> = ['ohos.permission.APPROXIMATELY_LOCATION', 'ohos.permission.LOCATION',]; 
const permissions4: Array<Permissions> = [ 
  'ohos.permission.READ_MEDIA', 
  'ohos.permission.WRITE_MEDIA', 
  'ohos.permission.MEDIA_LOCATION', 
  'ohos.permission.MICROPHONE', 
  'ohos.permission.CAMERA' 
]; 
 
@Entry 
@Component 
struct ReqPermission { 
  @State message: string = 'Hello World' 
 
  reqPermissionsFromUser(permissions: Array<Permissions>): void { 
    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) { 
          // 用户授权,可以继续访问目标操作 
          AlertDialog.show({ message: "授权成功" }) 
          console.log("(授权完成)") 
        } else { 
 
          // 用户拒绝授权,提示用户必须授权才能访问当前页面的功能,并引导用户到系统设置中打开相应的权限 
          console.log("(授权失败)") 
          return; 
        } 
      } 
      // 授权成功 
    }).catch((err: BusinessError) => { 
      console.error(`Failed to request permissions from user. Code is ${err.code}, message is ${err.message}`); 
    }); 
  } 
  async onRemoteMessageRequest(): Promise<boolean> { 
    let flag: boolean = true; 
    let callerTokenId: number = rpc.IPCSkeleton.getCallingTokenId(); 
    console.log("RpcServer: getCallingTokenId result: " + callerTokenId); 
    let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager(); 
    try { 
      let res: number = await atManager.checkAccessToken(callerTokenId, 'ohos.permission.LOCATION_IN_BACKGROUND') 
      if (res == -1) { 
        flag = false; 
      } else { 
        flag = true; 
      } 
    } catch (err) { 
      console.log(`catch err->${JSON.stringify(err)}`); 
    } 
    return flag; 
  } 
 
  build() { 
    Row() { 
      Column() { 
        Text('授权2222') 
          .textAlign(TextAlign.Center) 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
          .width('100%') 
          .onClick(() => { 
            this.reqPermissionsFromUser(permissions3) 
          }) 
        Text('授权333') 
          .textAlign(TextAlign.Center) 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
          .width('100%') 
          .onClick(() => { 
            this.reqPermissionsFromUser(permissions1) 
          }) 
        Button('跳转到应用详情') 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
          .width('100%') 
          .onClick(() => { 
            let context = getContext(this) as common.UIAbilityContext; 
            openPermissionsInSystemSettings(context) 
          }) 
        Text('检验') 
          .textAlign(TextAlign.Center) 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
          .width('100%') 
          .onClick(() => { 
            this.onRemoteMessageRequest().then((data) => { 
              AlertDialog.show({ message: "==>" + data }) 
            }); 
          }) 
 
      } 
    } 
    .height('100%') 
  } 
}` 
function openPermissionsInSystemSettings(context: common.UIAbilityContext): void { 
let wantInfo: Want = { 
bundleName: "com.huawei.hmos.settings", 
abilityName: "com.huawei.hmos.settings.MainAbility", 
uri: 'application_info_entry', 
// action: 'action.settings.app.info', 
parameters: { 
settingsParamBundleName: 'com.example.api11' // 打开指定应用的详情页面 
} 
} 
context.startAbility(wantInfo).then(() => { 
// ... 
}).catch((err: BusinessError) => { 
// ... 
}) 
}
分享
微博
QQ
微信
回复
2024-11-08 16:27:11
相关问题
HarmonyOS 动态申请权限申请不了
35浏览 • 1回复 待解决
动态申请权限能否添加描述
775浏览 • 1回复 待解决
HarmonyOS定位权限问题
438浏览 • 1回复 待解决
HarmonyOS 申请权限,禁止后不再弹窗
42浏览 • 1回复 待解决
HarmonyOS 证书相关问题
406浏览 • 1回复 待解决
动态申请权限的步骤是怎样的?
502浏览 • 1回复 待解决
HarmonyOS权限申请问题
449浏览 • 1回复 待解决
HarmonyOS 权限申请拒绝问题
75浏览 • 1回复 待解决
HarmonyOS 麦克风权限弹窗
58浏览 • 1回复 待解决
HarmonyOS dialog弹窗相关问题
476浏览 • 1回复 待解决
HarmonyOS 相册权限检查、申请问题
1262浏览 • 1回复 待解决
HarmonyOS申请用户位置权限问题
535浏览 • 1回复 待解决
HarmonyOS 弹窗相关问题
287浏览 • 1回复 待解决
HarmonyOS 关于弹窗相关问题
16浏览 • 1回复 待解决