HarmonyOS鸿蒙Next中如何拉起短信界面并指定联系人

HarmonyOS鸿蒙Next中如何拉起短信界面并指定联系人

2 回复

拉起短信界面并指定联系人

说明:

暂时不支持自动填充短信内容。

import common from '@ohos.app.ability.common';
import { Want } from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';

function startMMSAbilityExplicit(context: common.UIAbilityContext): void {
  class ModelContactInfo {
    contactsName: string;
    telephone: string;
  }

  let contactInfo: Array<ModelContactInfo> = [{
    contactsName: 'ZhangSan',
    telephone: '16888888888'
  }];
  let want: Want = {
    bundleName: 'com.ohos.mms',
    abilityName: 'com.ohos.mms.MainAbility',
    parameters: {
      contactObjects: JSON.stringify(contactInfo),
      pageFlag: 'conversation'
    }
  };
  context.startAbility(want)
    .then(() => {
      // ...
    })
    .catch((err: BusinessError) => {
      console.error(`Failed to startAbility. Code: ${err.code}, message: ${err.message}`);
    });
}

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(() => {
            const context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext;
            startMMSAbilityExplicit(context);
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}

更多关于HarmonyOS鸿蒙Next中如何拉起短信界面并指定联系人的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,可以通过Intent拉起短信界面并指定联系人。首先,创建一个Intent对象,设置ActionIntent.ACTION_SENDTO,然后使用Uri.parse("smsto:联系人号码")指定联系人号码,最后调用startAbility(intent)启动短信界面。示例代码如下:

Intent intent = new Intent();
intent.setAction(Intent.ACTION_SENDTO);
intent.setUri(Uri.parse("smsto:1234567890"));
startAbility(intent);

确保在config.json中声明了ohos.permission.SEND_MESSAGES权限。

回到顶部