
回复
系统设置及系统应用跳转是各类应用的高频使用场景之一,如跳转至系统设置、一键拨打电话、查看与修改应用权限、打开浏览器浏览网页等。
本文总结了打开系统蓝牙设置、打开移动网络设置、打开WiFi设置、拉起拨打电话、打开指定应用的设置详情页、打开指定应用在华为市场的详情页、打开应用通知设置、跳转系统浏览器打开网页、使用OpenLink跳转到系统其他应用。
跳转工具类源码
import { common, OpenLinkOptions, Want } from "@kit.AbilityKit";
import { WindowUtils } from "./WindowUtils";
import { call, observer } from "@kit.TelephonyKit";
import { BusinessError } from "@kit.BasicServicesKit";
import { productViewManager } from "@kit.StoreKit";
import { notificationManager } from "@kit.NotificationKit";
let context = WindowUtils.getWindowStage()?.getMainWindowSync().getUIContext().getHostContext() as common.UIAbilityContext;
export class SystemUtil{
// 跳转到蓝牙页面
static JumpToBluetooth(): void {
context.startAbility({
bundleName: 'com.huawei.hmos.settings',
abilityName: 'com.huawei.hmos.settings.MainAbility',
uri: 'bluetooth_entry',
})
}
// 跳转到移动网络二级页面
static JumpToMobileNetwork(): void {
context.startAbility({
bundleName: 'com.huawei.hmos.settings',
abilityName: 'com.huawei.hmos.settings.MainAbility',
uri: 'mobile_network_entry',
})
}
// 跳转到wifi二级页
static JumpToWifiEntry(): void {
context.startAbility({
bundleName: 'com.huawei.hmos.settings',
abilityName: 'com.huawei.hmos.settings.MainAbility',
uri: 'wifi_entry',
})
}
// 跳转到应用管理四级页面
static JumpToApplicationInfo(bundleName:string): void {
context.startAbility({
bundleName: 'com.huawei.hmos.settings',
abilityName: 'com.huawei.hmos.settings.MainAbility',
uri: 'application_info_entry',
parameters:{
pushParams:bundleName
}
})
}
// 跳转拨号
static JumpToCall(phoneNum:string): void {
// 调用查询能力接口
let isSupport = call.hasVoiceCapability();
if (isSupport) {
call.makeCall(phoneNum, (err: BusinessError) => {
if (err) {
console.error(`makeCall fail, err->${JSON.stringify(err)}`);
} else {
console.log(`makeCall success`);
}
});
// 订阅通话业务状态变化
class SlotId {
slotId: number = 0
}
class CallStateCallback {
state: call.CallState = call.CallState.CALL_STATE_UNKNOWN;
number: string = '';
}
let slotId: SlotId = { slotId: 0 }
observer.on('callStateChange', slotId, (data: CallStateCallback) => {
console.log("on callStateChange, data:" + JSON.stringify(data));
});
}
}
// 跳转到应用市场中的应用详情页
static JumpToAppGalleryProduct( bundleName: string) {
try {
const request: Want = {
parameters: { bundleName: bundleName}
};
productViewManager.loadProduct(context, request, {
});
}catch (err) {
console.log(`loadProduct failed.code is ${err.code}, message is ${err.message}`);
}
}
// 拉起应用通知设置
static openNotificationSettings(){
notificationManager.openNotificationSettings(context).then(() => {
console.log(`[ANS] openNotificationSettings success`);
}).catch((err: BusinessError) => {
console.log(`[ANS] openNotificationSettings failed, code is ${err.code}, message is ${err.message}`);
});
}
//跳转系统浏览器
static JumpSystemBrowser(url:string){
let want: Want = {
action: "ohos.want.action.viewData",
bundleName: 'com.huawei.hmos.browser',
abilityName: 'MainAbility',
uri:url,
};
context.startAbility(want)
}
//跳转到其他应用
static JumpOtherAppLink(link:string,params:Record<string, Object>){
let openLinkOptions: OpenLinkOptions = {
//表示是否必须以AppLinking的方式启动UIAbility
appLinkingOnly: false,
// 同want中的parameter,用于传递的参数
parameters: params
};
try {
context.openLink(link, openLinkOptions, (err, data) => {
// AbilityResult回调函数,仅在被启动的UIAbility终止时触发
console.log('open link success. Callback result:' + JSON.stringify(data));
}).then(() => {
console.log('open link success.');
}).catch((err: BusinessError) => {
console.log(`open link failed. Code is ${err.code}, message is ${err.message}`);
})
} catch (paramError) {
console.log(`Failed to start link. Code is ${paramError.code}, message is ${paramError.message}`);
}
}
}
使用openLink跳转时,目标应用需要在module.json中的skills添加如下配置
"uris": [
{
"scheme": "scheme", //通常不为"https"、"http"、"file"等已被系统应用使用的值,否则会匹配到对应的系统应用
"host": "com.app.comment" // 自定义一个域名
}
]
Page中使用:
import { SystemUtil } from '../utils/SystemUtil'
@Entry
@ComponentV2
struct SystemJumpTest{
build() {
Column({space:10}){
Button('打开蓝牙设置').onClick(()=>{
SystemUtil.JumpToBluetooth()
})
Button('打开移动网络二级页').onClick(()=>{
SystemUtil.JumpToMobileNetwork()
})
Button('打开wifi二级页').onClick(()=>{
SystemUtil.JumpToWifiEntry()
})
Button('打电话:12345').onClick(()=>{
SystemUtil.JumpToCall('12345')
})
Button('跳转应用详情:应用市场').onClick(()=>{
SystemUtil.JumpToApplicationInfo('com.huawei.hmsapp.appgallery')
})
Button('打开华为阅读的应用详情页').onClick(()=>{
SystemUtil.JumpToAppGalleryProduct('com.huawei.hmsapp.books')
})
Button('打开应用通知设置').onClick(()=>{
SystemUtil.openNotificationSettings()
})
Button('打开系统浏览器').onClick(()=>{
SystemUtil.JumpSystemBrowser('https://developer.huawei.com/')
})
Button('使用openLink打开其他应用').onClick(()=>{
SystemUtil.JumpOtherAppLink('scheme://com.app.comment',{})
})
}
}
}