鸿蒙HarmonyOS开发:系统服务

拨打电话

call.makeCall
跳转到拨号界面,并显示待拨出的号码。使用callback异步回调。

makeCall(phoneNumber: string, callback: AsyncCallback<void>): void
import { call } from '@kit.TelephonyKit';

import { BusinessError } from '@kit.BasicServicesKit';

call.makeCall("138xxxxxxxx", (err: BusinessError) => {
    if (err) {
        console.error(`makeCall fail, err->${JSON.stringify(err)}`);
    } else {
        console.log(`makeCall success`);
    }
});
import { BusinessError } from '@kit.BasicServicesKit';

call.makeCall("138xxxxxxxx").then(() => {
    console.log(`makeCall success`);
}).catch((err: BusinessError) => {
    console.error(`makeCall fail, promise: err->${JSON.stringify(err)}`);
});

网络搜索

需要权限:ohos.permission.GET_NETWORK_INFO

import { radio } from '@kit.TelephonyKit';
radio.getSignalInformationSync

获取指定SIM卡槽对应的注册网络信号强度信息列表。
卡槽ID。

  • 0:卡槽1
  • 1:卡槽2
let slotId: number = 0;
let signalInfo: Array<radio.SignalInformation> = radio.getSignalInformationSync(slotId);
console.log(`signal information size is:` + signalInfo.length);

联系人

提供联系人管理能力,包括添加联系人、删除联系人、更新联系人等。

import { contact } from '@kit.ContactsKit';

需要权限:ohos.permission.WRITE_CONTACTS

添加联系人,使用Promise方式作为异步方法。

addContact(context: Context, contact: Contact): Promise<number>
  import { BusinessError } from '@kit.BasicServicesKit';
  // 获取context
  let context = getContext(this) as Context;
  let promise = contact.addContact(
    context,
    {name: {fullName: 'xxx'},
      phoneNumbers: [{phoneNumber: '138xxxxxxxx'}]
  });
  promise.then((data) => {
    console.log(`addContact success: data->${JSON.stringify(data)}`);
  }).catch((err: BusinessError) => {
    console.error(`addContact fail: err->${JSON.stringify(err)}`);
  });

删除联系人,使用Promise方式作为异步方法。

deleteContact(context: Context, key: string): Promise<void>
import { BusinessError } from '@kit.BasicServicesKit';
// 获取context
let context = getContext(this) as Context;
let promise = contact.deleteContact(context, 'xxx');
promise.then(() => {
    console.log(`deleteContact success`);
}).catch((err: BusinessError) => {
    console.error(`deleteContact fail: err->${JSON.stringify(err)}`);
});

位置服务

import { geoLocationManager } from '@kit.LocationKit';

获取当前位置,使用Promise异步回调。

getCurrentLocation(request?: CurrentLocationRequest | SingleLocationRequest): Promise<Location>

调用逆地理编码服务,将坐标转换为地理描述,使用Promise异步回调。

getAddressesFromLocation(request: ReverseGeoCodeRequest): Promise<Array<GeoAddress>>;
import { geoLocationManager } from '@kit.LocationKit';
import { BusinessError } from '@kit.BasicServicesKit'
let reverseGeocodeRequest:geoLocationManager.ReverseGeoCodeRequest = {"latitude": 31.12, "longitude": 121.11, "maxItems": 1};
try {
    geoLocationManager.getAddressesFromLocation(reverseGeocodeRequest).then((data) => {
        console.log('getAddressesFromLocation: ' + JSON.stringify(data));
    })
    .catch((error:BusinessError) => {
        console.error('promise, getAddressesFromLocation: error=' + JSON.stringify(error));
    });
} catch (err) {
    console.error("errCode:" + JSON.stringify(err));
}

获取上一次位置。

import { geoLocationManager } from '@kit.LocationKit';
try {
    let location = geoLocationManager.getLastLocation();
} catch (err) {
    console.error("errCode:" + JSON.stringify(err));
}

监听位置

async aboutToAppear(){
 let status = await this.reqPermissionsFromUser(["ohos.permission.LOCATION","ohos.permission.APPROXIMATELY_LOCATION"])
if(status){
geoLocationManager.on("locationChange",{
priority:LocationRequestScenario.NAVIGATION,
timeInterval:1
},value=>{
console.log(JSON.stringify(value))
})
}
}

取消监听

aboutToDisappear(){
geoLocationManager.off("locationChange")
}

拉起弹框请求用户授权

requestPermissionsFromUser(context: Context, permissionList: Array<Permissions>): Promise<PermissionRequestResult>

用于UIAbility拉起弹框请求用户授权。使用promise异步回调。

如果用户拒绝授权,将无法再次拉起弹框,需要用户在系统应用“设置”的界面中,手动授予权限。

import abilityAccessCtrl, { Context, PermissionRequestResult } from '@ohos.abilityAccessCtrl';
import { BusinessError } from '@ohos.base';
import common from '@ohos.app.ability.common';

let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager();
let context: Context = getContext(this) as common.UIAbilityContext;
atManager.requestPermissionsFromUser(context, ['ohos.permission.CAMERA']).then((data: PermissionRequestResult) => {
  console.info('data:' + JSON.stringify(data));
  console.info('data permissions:' + data.permissions);
  console.info('data authResults:' + data.authResults);
  console.info('data dialogShownResults:' + data.dialogShownResults);
}).catch((err: BusinessError) => {
  console.error('data:' + JSON.stringify(err));
});

你可能感兴趣的:(HarmonyOS,harmonyos,拨打电话,网络搜索,蜂窝数据,联系人,位置服务,请求用户授权)