首页 > 数据库 > 数据库开发技术 > HarmonyOS NEXT实战:拨打电话
##HarmonyOS Next实战##HarmonyOS SDK应用服务##教育##
目标:输入电话号码,并拨打电话。
知识点: Telephony Kit(蜂窝通信服务)提供一系列API帮助开发者开发通讯类应用,包括:
使用makeCall拨打电话
实战:CallPhoneDemoPage
import { call, observer } from '@kit.TelephonyKit';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct CallPhoneDemoPage {
@State message: string = 'Hello World';
@State phone: string = ''
build() {
Column({ space: 10 }) {
Text('拨打电话Demo')
TextInput({ placeholder: '请输入要拨打的电话' })
.type(InputType.PhoneNumber)
.onChange((value) => {
this.phone = value
})
Button('拨号')
.onClick(() => {
// 调用查询能力接口
let isSupport = call.hasVoiceCapability();
if (isSupport) {
// 如果设备支持呼叫能力,则继续跳转到拨号界面,并显示拨号的号码
call.makeCall(this.phone, (err: BusinessError) => {
if (!err) {
console.log("make call success.");
} else {
console.log("make call fail, err is:" + JSON.stringify(err));
}
});
// 订阅通话业务状态变化(可选)
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("call state change, data is:" + JSON.stringify(data));
});
}
})
}
.height('100%')
.width('100%')
.padding({ left: 20, right: 20 })
}
}
原文作者:;如有侵权,请联系管理员删除。