完整流程如下:
isLocationEnabled(): void { // 判断当前位置开关状态是否开启 let locationEnabled = geoLocationManager.isLocationEnabled(); if (locationEnabled) { this.message = `“访问我的位置”开关已开启`; this.openDialog(); } else { this.message = `“访问我的位置”开关未开启`; this.openDialog(); } }
toLocationInfo() { let context = this.getUIContext().getHostContext() as common.UIAbilityContext; let want: Want = { bundleName: 'com.huawei.hmos.settings', abilityName: 'com.huawei.hmos.settings.MainAbility', uri: 'location_manager_settings', }; // 跳转访问我的位置信息 context.startAbility(want); }
isLocationToggle(): void { let atManager = abilityAccessCtrl.createAtManager(); // 判断当前位置开关状态是否开启 let isLocationEnabled = geoLocationManager.isLocationEnabled(); let context = this.getUIContext().getHostContext() as common.UIAbilityContext; if (!isLocationEnabled) { // 拉起全局开关设置弹框 atManager.requestGlobalSwitch(context, abilityAccessCtrl.SwitchType.LOCATION).then((data: boolean) => { if (data) { this.message = `“访问我的位置”开关已开启`; this.openDialog(); } else { this.message = `“访问我的位置”开关未开启`; this.openDialog(); } }).catch((err: BusinessError) => { console.error(`Failed to request global switch. Code is ${err.code}, message is ${err.message}`); }); } else { this.message = `“访问我的位置”开关已开启`; this.openDialog(); } }
import common from '@ohos.app.ability.common'; import Want from '@ohos.app.ability.Want'; import { geoLocationManager } from '@kit.LocationKit'; import { abilityAccessCtrl } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; @Entry @Component struct LocationPage { @State message: string = ''; private customDialogComponentId: number = 0; @Builder normalCustomDialog() { Column() { Text(this.message) .fontSize(14) .padding({ left: 16, right: 16, bottom: 8.5, top: 8.5 }) } .width('100%') .height('100%') .borderRadius(18) .justifyContent(FlexAlign.Center) .alignItems(HorizontalAlign.Center) .onClick(() => { // 关闭弹窗 this.getUIContext().getPromptAction().closeCustomDialog(this.customDialogComponentId); }) } // 打开弹窗 openDialog() { this.getUIContext().getPromptAction().openCustomDialog({ builder: () => { this.normalCustomDialog(); }, width: 238, height: 36, alignment:DialogAlignment.Bottom, offset:{ dx: 0 , dy: -100 } }) .then((dialogId: number) => { this.customDialogComponentId = dialogId; }); } isLocationEnabled(): void { // 判断当前位置开关状态是否开启 let locationEnabled = geoLocationManager.isLocationEnabled(); if (locationEnabled) { this.message = `“访问我的位置”开关已开启`; this.openDialog(); } else { this.message = `“访问我的位置”开关未开启`; this.openDialog(); } } isLocationToggle(): void { let atManager = abilityAccessCtrl.createAtManager(); // 判断当前位置开关状态是否开启 let isLocationEnabled = geoLocationManager.isLocationEnabled(); let context = this.getUIContext().getHostContext() as common.UIAbilityContext; if (!isLocationEnabled) { // 拉起全局开关设置弹框 atManager.requestGlobalSwitch(context, abilityAccessCtrl.SwitchType.LOCATION).then((data: boolean) => { if (data) { this.message = `“访问我的位置”开关已开启`; this.openDialog(); } else { this.message = `“访问我的位置”开关未开启`; this.openDialog(); } }).catch((err: BusinessError) => { console.error(`Failed to request global switch. Code is ${err.code}, message is ${err.message}`); }); } else { this.message = `“访问我的位置”开关已开启`; this.openDialog(); } } toLocationInfo() { let context = this.getUIContext().getHostContext() as common.UIAbilityContext; let want: Want = { bundleName: 'com.huawei.hmos.settings', abilityName: 'com.huawei.hmos.settings.MainAbility', uri: 'location_manager_settings', }; // 跳转访问我的位置信息 context.startAbility(want); } build() { Column() { Button('是否开启“访问我的位置”开关') .backgroundColor('#0a59f7') .onClick(() => { // 判断当前位置开关状态是否开启 this.isLocationEnabled(); }).margin(30) Button('跳转访问我的位置信息') .backgroundColor('#0a59f7') .onClick(() => { // 跳转访问我的位置信息 this.toLocationInfo(); }).margin(30) Button('拉起半模态对话框') .backgroundColor('#0a59f7') .onClick(() => { // 拉起半模态对话框 this.isLocationToggle(); }).margin(30) } .width('100%') .height('100%') } }