-
效果:
-
完整工具类:
import { common, Want } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { call } from '@kit.TelephonyKit';
import { promptAction } from '@kit.ArkUI';
import { contact } from '@kit.ContactsKit';
import { camera, cameraPicker } from '@kit.CameraKit';export class SystemAppUtil {static tag: string = 'SystemAppUtil '/*** 唤起系统浏览器应用* */static openBrowsable(url: string) {if (!url) {promptAction.showToast({ message: "唤起系统浏览器失败,url为空!" })return}if (getContext()) {let context = getContext() as common.UIAbilityContext;let want: Want = {action: 'ohos.want.action.viewData',entities: ['entity.system.browsable'],uri: url};context.startAbility(want).then(() => {console.info(SystemAppUtil.tag + ' 拉起浏览器成功');}).catch((err: BusinessError) => {console.error(SystemAppUtil.tag + ` 拉起浏览器失败: Code: ${err.code}, message: ${err.message}`);});} else {promptAction.showToast({ message: "唤起系统浏览器失败,context为空!" })}}/*** 唤起系统拨号界面* */static startCall(phoneNumber: string): void {if (!phoneNumber) {promptAction.showToast({ message: "唤起系统拨号界面失败,手机号码为空!" })return}// 检测是否支持语音通话let isSupport = call.hasVoiceCapability();if (!isSupport) {console.error(SystemAppUtil.tag + '不支持语音通话');return;}call.makeCall(phoneNumber, (err: BusinessError) => {if (err) {console.error(SystemAppUtil.tag + `唤起拨号界面失败. Code is ${err.code}, Message is ${err.message}`);return;}console.info(SystemAppUtil.tag + '唤起拨号界面成功');})}/*** 通讯录:打开通讯录并选择联系人,通过 promise回调* @param isMultiSelect 选择联系人,是否允许多选 true:支持多选* */static async selectContacts(isMultiSelect: boolean = false): Promise<Array<contact.Contact>> {let promise = await contact.selectContacts({ isMultiSelect: isMultiSelect });return promise}/*** 打开指定设置界面:对应界面关系* * wifi_entry : HOME-WLAN* bluetooth_entry : HOME-蓝牙* mobile_network_entry: HOME-移动网络* hotspot_data_settings: HOME-移动网络-个人热点界面* password_entry: HOME-移动网络-个人热点-密码界面* connected_device_entry: HOME-移动网络-个人热点-已连接设备界面* more_share_entry: HOME-移动网络-个人热点-更多共享设置界面* more_connections_settings: HOME-更多连接* nfc_settings: HOME-更多连接-NFC三级页面* display_settings: HOME-显示和亮度* screen_zoom: HOME-显示和亮度-显示大小三级页面* screen_refresh_rate_entry: HOME-显示和亮度-屏幕刷新率三级页面 (需看具体设备是否有刷新率选项)* volume_settings: HOME-声音和振动* systemui_notification_settings: HOME-通知和状态栏* accessibility_feature: accessibility_feature* accessibility_operation_entry: HOME-辅助功能-辅助功能快捷键三级页面* accessibility_more_settings_entry: HOME-辅助功能-已安装的服务-服务详情-更多设置五级页面* application_and_service_settings: HOME-应用与元服务* application_settings: HOME-应用与服务-应用管理三级页面* application_info_entry: HOME-应用和元服务HOME-某个具体应用的应用信息,需传递want.parameters.pushParams为具体应用的包名* storage_settings: HOME-存储界面* battery: HOME-电池* biometrics_and_password_settings: HOME-生物识别和密码* lock_screen_password_title: HOME-生物识别和密码-设置数字锁屏密码* change_six_to_number_entry: HOME-生物识别和密码-锁屏密码(其他密码类型)-自定义数字密码(设置锁屏数字密码)四级页面* hange_six_to_mixed_entry: HOME-生物识别和密码-锁屏密码(其他密码类型)-混合密码(设置锁屏密码)四级页面* fingerprint_settings_entry: HOME-生物识别与密码-指纹3级页面 (需看具体设备是否支持指纹解锁能力)* privacy_settings: HOME-隐私与安全* location_help_entry: HOME-隐私与安全-定位服务-帮助四级页面* users_accounts: HOME-用户和账户* current_user: HOME-用户和账户-当前登录(用户)三级页面* system_and_updates: HOME-系统和更新* time_zone_settings: HOME-系统和更新-日期时间-时区-时区选择列表* date_and_time: HOME-系统和更新-日期时间三级页面* set_language: HOME-系统和更新-语言和输入法-语言和输入法四级页面* set_language_region: HOME-系统和更新-语言和输入法-语言和地区-语言和地区5级页面* reset_settings: HOME-系统和更新-重置三级页面* developer_options_settings: HOME-系统和更新-开发人员选项三级页面* edit_language_entry: HOME-系统和更新-语言和输入法-语言和地区-编辑(编译语言)五级页面* add_language_entry: HOME-系统和更新-语言和输入法-语言和地区-添加语言五级页面* select_region_entry: HOME-系统和更新-语言和输入法-语言和地区-当前地区(选择地区)五级页面* reset_factory_settings: HOME-系统和更新-重置-恢复出厂设置四级页面* reset_net_settings: HOME-系统和更新-重置-还原网络设置四级页面* reset_confirm_settings: HOME-系统和更新-重置-恢复出厂设置-重置手机五级页面* reset_net_confirm_settings: HOME-系统和更新-重置-还原网络设置-还原网络设置确认五级页面* about_device: HOME-关于本机界面* device_name: HOME-HOME-关于本机-设备名称* * */static openDesignateSetting(uri: string) {if (getContext()) {let ctx = getContext() as common.UIAbilityContext;let applicationInfo = ctx.getApplicationContext().applicationInfoctx.startAbility({bundleName: 'com.huawei.hmos.settings',abilityName: 'com.huawei.hmos.settings.MainAbility',uri: uri,parameters: {pushParams: applicationInfo.name // 包名}});} else {promptAction.showToast({ message: "唤起打开指定设置界面失败,context为空!" })}}/*** 打开在华为市场的应用详情* */static openAppGalleryDetailAbility(bundleName: string): void {if (getContext()) {let ctx = getContext() as common.UIAbilityContext;let want: Want = {action: 'ohos.want.action.appdetail',uri: 'store://appgallery.huawei.com/app/detail?id=' + bundleName, // bundleName为需要打开应用详情的应用的包名};ctx.startAbility(want).then(() => {console.info(SystemAppUtil.tag + ' 打开应用市场成功');}).catch((err: BusinessError) => {console.error(SystemAppUtil.tag + `打开应用市场失败. Code: ${err.code}, message: ${err.message}`);});} else {promptAction.showToast({ message: "打开在华为市场的应用详情,context为空!" })}}/*** 打开相机* */static async startCameraPicker(): Promise<cameraPicker.PickerResult> {if (getContext()) {let ctx = getContext() as common.UIAbilityContext;try {let pickerProfile: cameraPicker.PickerProfile = {cameraPosition: camera.CameraPosition.CAMERA_POSITION_BACK};// PickerMediaType 配置打开类型let pickerResult: cameraPicker.PickerResult = await cameraPicker.pick(ctx,[cameraPicker.PickerMediaType.PHOTO, cameraPicker.PickerMediaType.VIDEO],pickerProfile);return pickerResult} catch (error) {let err = error as BusinessError;console.error(SystemAppUtil.tag + `相机打开失败. error code: ${err.code}`);return new cameraPicker.PickerResult()}} else {promptAction.showToast({ message: "打开相机,context为空!" })return new cameraPicker.PickerResult()}}
}
- 使用
import { SystemAppUtil } from '../../util/SystemAppUtil'
import { common } from '@kit.AbilityKit'
import { BusinessError } from '@kit.BasicServicesKit'@Entry
@Component
export struct SystemAppUtilTestWidget {aboutToAppear() {}@State contacts: string = '选择联系人数据'@State pick: string = ' 拍照/录像结果'build() {Column() {Button('唤起系统浏览器').onClick(() => {let url = 'https://developer.huawei.com/consumer/cn/activity/'SystemAppUtil.openBrowsable(url)})Button('唤起拨号界面').onClick(() => {SystemAppUtil.startCall('13711111111')}).margin({ top: 15 })Button('打开通讯录选择联系人').onClick(() => {SystemAppUtil.selectContacts().then((data) => {this.contacts = JSON.stringify(data)console.log(`selectContacts success: data->${JSON.stringify(data)}`);}).catch((err: BusinessError) => {this.contacts = JSON.stringify(err)console.error(`selectContacts fail: err->${JSON.stringify(err)}`);});}).margin({ top: 15 })Text(this.contacts)Button('权限指定设置界面').onClick(() => {SystemAppUtil.openDesignateSetting('bluetooth_entry')}).margin({ top: 15 })Button('打开应用市场').onClick(() => {// 替换上架app的包名SystemAppUtil.openAppGalleryDetailAbility('app的包名')}).margin({ top: 15 })Button('打开相机').onClick(() => {SystemAppUtil.startCameraPicker().then((data) => {console.log(`startCameraPicker success: data->${JSON.stringify(data)}`);this.pick = JSON.stringify(data)})}).margin({ top: 15 })Text(`拍照结果:${this.pick}`)}.height('100%').padding({ top: 70 })}
}