f91c1754创建于 2025年10月23日历史提交

Managing Distributed Accounts (for System Applications Only)

You can use the distributed account SDK to implement smooth switchover between a distributed account and a system account.

Getting Started

  1. Request the ohos.permission.MANAGE_DISTRIBUTED_ACCOUNTS permission. For details, see Requesting Permissions for system_basic Applications.

  2. Import the distributedAccount module.

import { distributedAccount, BusinessError } from '@kit.BasicServicesKit';
  1. Obtain a DistributedAccountAbility instance.

const distributedAccountAbility = distributedAccount.getDistributedAccountAbility();

Logging In to a Distributed Account from the Current System Account

Procedure

  1. Specify the distributed account to be logged in. Set event to Ohos.account.event.LOGIN.

    let distributedInfo: distributedAccount.DistributedInfo = {
      name: 'ZhangSan',
      id: '12345',
      event: 'Ohos.account.event.LOGIN',
    };
  1. Use setOsAccountDistributedInfo to log in to the distributed account.

    await distributedAccountAbility.setOsAccountDistributedInfo(distributedInfo).then(() => {
      console.info('setOsAccountDistributedInfo successfully');
    }).catch((err: BusinessError) => {
      console.error(`setOsAccountDistributedInfo exception: code is ${err.code}, message is ${err.message}`);
	// ···
    });
  1. After the login, use getOsAccountDistributedInfo to obtain information of the distributed account.

    distributedAccountAbility.getOsAccountDistributedInfo().then((data: distributedAccount.DistributedInfo) => {
      console.info('distributed information: ' + JSON.stringify(data));
	// ···
    }).catch((err: BusinessError) => {
      console.error(`getOsAccountDistributedInfo exception: code is ${err.code}, message is ${err.message}`);
	// ···
    });

Logging Out of a Distributed Account to the Current System Account

Procedure

  1. Specify the distributed account to be logged out. Set event to Ohos.account.event.LOGOUT.

    let distributedInfo: distributedAccount.DistributedInfo = {
      name: 'ZhangSan',
      id: '12345',
      event: 'Ohos.account.event.LOGOUT',
    };
  1. Use setOsAccountDistributedInfo to log out of the specified distributed account.

    distributedAccountAbility.setOsAccountDistributedInfo(distributedInfo).then(() => {
      console.info('setOsAccountDistributedInfo successfully');
	// ···
    }).catch((err: BusinessError) => {
      console.error(`setOsAccountDistributedInfo exception: code is ${err.code}, message is ${err.message}`);
	// ···
    });

Logging In to a Distributed Account from a System Account

Procedure

  1. Specify the system account and the distributed account to be logged in. Set event to Ohos.account.event.LOGIN.

    let localId: number = 100;
    let distributedInfo: distributedAccount.DistributedInfo = {
      name: 'ZhangSan',
      id: '12345',
      event: 'Ohos.account.event.LOGIN',
    };
  1. Call setOsAccountDistributedInfoByLocalId to bind the specified distributed account to the current system account.

    await distributedAccountAbility.setOsAccountDistributedInfoByLocalId(localId, distributedInfo).then(() => {
      console.info('setOsAccountDistributedInfoByLocalId successfully');
    }).catch((err: BusinessError) => {
      console.error(`setOsAccountDistributedInfoByLocalId exception: code is ${err.code}, message is ${err.message}`);
	// ···
    });
  1. After the login, use getOsAccountDistributedInfoByLocalId to obtain information of the distributed account.

    distributedAccountAbility.getOsAccountDistributedInfoByLocalId(localId)
      .then((data: distributedAccount.DistributedInfo) => {
      console.info('distributed information: ' + JSON.stringify(data));
	// ···
    }).catch((err: BusinessError) => {
      console.error(`getOsAccountDistributedInfoByLocalId exception: code is ${err.code}, message is ${err.message}`);
	// ···
    });

Logging Out of a Distributed Account to a System Account

Procedure

  1. Specify the system account and the distributed account to be logged out. Set event to Ohos.account.event.LOGOUT.

    let localId: number = 100;
    let distributedInfo: distributedAccount.DistributedInfo = {
      name: 'ZhangSan',
      id: '12345',
      event: 'Ohos.account.event.LOGOUT',
    };
  1. Use setOsAccountDistributedInfoByLocalId to log out of the specified distributed account to the target system account.

    distributedAccountAbility.setOsAccountDistributedInfoByLocalId(localId, distributedInfo).then(() => {
      console.info('setOsAccountDistributedInfoByLocalId successfully');
	// ···
    }).catch((err: BusinessError) => {
      console.error(`setOsAccountDistributedInfoByLocalId exception: code is ${err.code}, message is ${err.message}`);
	// ···
    });