使用JavaScriptProxy代理实现 JSBridge 时如何调用原生功能(如拨打电话)?

使用JavaScriptProxy代理实现 JSBridge 时如何调用原生功能(如拨打电话)?

阅读 1.6k
1 个回答

ets:
import { call } from '@kit.TelephonyKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { webview } from '@kit.ArkWeb';
class TestObj {
constructor() {
}
startCallDialog(phoneNumber: string): void {
let isSupport = call.hasVoiceCapability();
if (!isSupport) {
console.error('Not support voice capability.');
return;
}
call.makeCall(phoneNumber, (err: BusinessError) => {
if (err) {
console.error(Failed to make call. Code is ${err.code}, Message is ${err.message});
return;
}
console.info('Succeeded in making call.');
})
}
}
@Entry
@Component
struct StartCallDialogView {
@State message: string = '拉起拨号界面;
controller: webview.WebviewController = new webview.WebviewController()
testObj = new TestObj();
build() {
Row() {
Column() {
Web({ src: $rawfile('index.html'), controller: this.controller })
.javaScriptAccess(true)
.javaScriptProxy({
object: this.testObj,
name: "objName",
methodList: ["startCallDialog"],
controller: this.controller,
})
}
.width('100%')
}
.height('100%')
}
}

index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<h1>Hello, ArkWeb</h1>
<div onclick="htmlTest()">拉起拨号界面</div>
<script type="text/javascript">
function htmlTest() {
str = objName.startCallDialog("15988888888")
console.log('objName.test result:'+ str)
}
</script>
</body>
</html>

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进