天天看點

#DAYU200# OpenHarmony跳轉撥号界面

日常交流中,給朋友打電話是一個常見的交流方式,那麼如何在OpenHarmony中進行電話服務相關的開發呢,今天我們可以一起來了解一下。

電話服務系統提供了一系列的API用于撥打電話、擷取無線蜂窩網絡和SIM卡相關資訊。

應用可以通過調用API來擷取目前注冊網絡名稱、網絡服務狀态、信号強度以及SIM卡的相關資訊,具體可參考擷取目前蜂窩網絡信号資訊開發指導。

直接撥打電話需要系統權限ohos.permission.PLACE_CALL,建議應用使用makeCall(),跳轉到撥号界面,并顯示撥号的号碼,當開發者調用makeCall接口時,裝置會自動跳轉到撥号界面。和正常撥打電話一樣,使用者可以選擇卡1或卡2撥出。

#DAYU200# OpenHarmony跳轉撥号界面

接口說明

call子產品為開發者提供呼叫管理功能。observer子產品為開發者提供通話業務狀态訂閱和取消訂閱功能。

  • call.hasVoiceCapability():能力擷取,表示是否具有語音功能。
  • call.makeCall()跳轉撥号界面,跳轉到撥号界面,并顯示撥号的号碼。
  • observer.on(‘callStateChange’):訂閱通話業務狀态變化,ohos.permission.READ_CALL_LOG (擷取通話号碼需要該權限)
  • observer.off(‘callStateChange’):取消訂閱通話業務狀态變化.

開發步驟

1.import需要的子產品。

// import需要的子產品
import call from '@ohos.telephony.call';
import observer from '@ohos.telephony.observer';      

2.調用hasVoiceCapability()接口擷取目前裝置呼叫能力,如果支援繼續下一步;如果不支援則無法發起呼叫。

// 調用查詢能力接口
let isSupport = call.hasVoiceCapability();
if (!isSupport) {
    console.log("not support voice capability, return.");
    return;
}      

3.跳轉到撥号界面,并顯示撥号的号碼。

// 如果裝置支援呼叫能力,則繼續跳轉到撥号界面,并顯示撥号的号碼
call.makeCall("13xxxx", (err)=> {
    if (!err) {
        console.log("make call success.");
    } else {
        console.log("make call fail, err is:" + JSON.stringify(err));
    }
});      

4.(可選)訂閱通話業務狀态變化。

// 訂閱通話業務狀态變化(可選)
observer.on("callStateChange", (data) => {
    console.log("call state change, data is:" + JSON.stringify(data));
});      

5.取消訂閱通話業務狀态變。

// 取消訂閱通話業務狀态變
observer.off("callStateChange", (data) => {
    console.log("call state change, data is:" + JSON.stringify(data));
});      

完畢

最後附上完整代碼:

/*
 * Copyright (c) 2022 JianGuo Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/**
 * @ProjectName : nutsStudy
 * @FileName :  call
 * @Author : 堅果
 * @Time : 2022/9/15 08:20
 * @Description : 檔案描述
 */
// import需要的子產品
import call from '@ohos.telephony.call';
import observer from '@ohos.telephony.observer';
@Entry
@Component
struct CAllTest{
  build(){
​
    Column(){
​
      Button("打電話給堅果").width(300).height(80) .fontSize(30).fontColor(Color.Orange).onClick(()=>{
        // 調用查詢能力接口
        let isSupport = call.hasVoiceCapability();
        if (!isSupport) {
          console.info(" support voice capability, return");
          return;
        }
        // 如果裝置支援呼叫能力,則繼續跳轉到撥号界面,并顯示撥号的号碼
        call.makeCall("17752170152", (err)=> {
          if (!err) {
            console.info(" make call success.");
​
          } else {
            console.info("make call fail, err is:" + JSON.stringify(err));
          }
        });
​
​
      })
​
    }.width("100%").height("100%").justifyContent(FlexAlign.Center)
​
​
  }
}      

好的,今天的一個小功能就帶大家了解到這兒,大家也可以将這個功能擴充到自己的應用裡。