HarmonyOS Web组件中怎么拨打电话、跳转应用商店?

如题:HarmonyOS Web组件中怎么拨打电话、跳转应用商店?

阅读 561
1 个回答

拨打电话demo:

import web_webview from ‘@ohos.web.webview’;
import call from ‘@ohos.telephony.call’;
import { BusinessError } from ‘@ohos.base’;

@Entry
@Component
struct WebComponent {
webviewController: web_webview.WebviewController = new web_webview.WebviewController();

build() {
Column() {
Web({ src: $rawfile(‘call.html’), controller: this.webviewController})
.onLoadIntercept((event) => {
if (event) {
let url: string = event.data.getRequestUrl();
// 判断链接是否为拨号链接
if (url.indexOf(‘tel://’) === 0) {
call.makeCall(url.substring(6),(err: BusinessError) => {
if (!err) {
console.log(“make call success.”);
} else {
console.log(“make call fail, err is:” + JSON.stringify(err));
}
});
return true;
}
}
return false;
})
}
}
}
call.html:
<!DOCTYPE html> <html> <body> <div> <a href="tel://10086">拨打电话</a> </div> </body> </html>

跳转应用商店demo:

import Want from '@ohos.app.ability.Want';
import common from '@ohos.app.ability.common';

@Entry
@Component
struct Index {
  @State appId: string = 要跳转的AppID;
  controller: TextInputController = new TextInputController();
  build() {
    Row() {
      Column() {
        TextInput({ text: this.appId, placeholder: '请输入应用的appId', controller: this.controller })
          .width('90%')
          .onChange((value: string) => {
            this.appId = value
          })
        Button('点击跳转到HarmonyOS版应用市场详情页面')
          .margin({top: 50})
          .onClick(()=>{
            const want: Want = {
              uri: `store://appgallery.huawei.com/app/ id=${this.appId}`
            };
            const context = getContext(this) as common.UIAbilityContext;
            context.startAbility(want).then(()=>{ 
              //拉起成功
            }).catch(()=>{
              // 拉起失败
            });
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进