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

@ohos.account.distributedAccount (Distributed Account Management) (System API)

The distributedAccount module provides APIs for managing distributed accounts, including querying and updating account login states.

NOTE

  • The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.
  • This topic describes only the system APIs provided by the module. For details about its public APIs, see @ohos.account.distributedAccount (Distributed Account Management).

Modules to Import

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

DistributedAccountAbility

Provides APIs for querying and updating the login state of a distributed account. You must obtain a DistributedAccountAbility instance first.

getOsAccountDistributedInfoByLocalId10+

getOsAccountDistributedInfoByLocalId(localId: number, callback: AsyncCallback<DistributedInfo>): void

Obtains distributed information about a system account. This API uses an asynchronous callback to return the result.

System API: This is a system API.

System capability: SystemCapability.Account.OsAccount

Required permissions: ohos.permission.MANAGE_DISTRIBUTED_ACCOUNTS; or ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS and ohos.permission.GET_DISTRIBUTED_ACCOUNTS

Parameters

Name Type Mandatory Description
localId number Yes ID of the target system account.
callback AsyncCallback<DistributedInfo> Yes Callback used to return the result. If the operation is successful, err is undefined and data is the distributed account information obtained. Otherwise, err is an error object.

Error codes

ID Error Message
201 Permission denied.
202 Not system application.
12300001 System service exception.
12300003 Account not found.

Example

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

const accountAbility: distributedAccount.DistributedAccountAbility = distributedAccount.getDistributedAccountAbility();
try {
  accountAbility.getOsAccountDistributedInfoByLocalId(100,
    (err: BusinessError, data: distributedAccount.DistributedInfo) => {
      if (err) {
        console.error(`getOsAccountDistributedInfoByLocalId exception: code is ${err.code}, message is ${err.message}`);
      } else {
        console.info('distributed information: ' + JSON.stringify(data));
      }
    });
} catch (e) {
  const err = e as BusinessError;
  console.error(`getOsAccountDistributedInfoByLocalId exception: code is ${err.code}, message is ${err.message}`);
}

getOsAccountDistributedInfoByLocalId10+

getOsAccountDistributedInfoByLocalId(localId: number): Promise<DistributedInfo>

Obtains distributed information about a system account. This API uses a promise to return the result.

System API: This is a system API.

System capability: SystemCapability.Account.OsAccount

Required permissions: ohos.permission.MANAGE_DISTRIBUTED_ACCOUNTS; or ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS and ohos.permission.GET_DISTRIBUTED_ACCOUNTS

Parameters

Name Type Mandatory Description
localId number Yes ID of the target system account.

Return value

Type Description
Promise<DistributedInfo> Promise used to return the distributed account information obtained.

Error codes

ID Error Message
201 Permission denied.
202 Not system application.
12300001 System service exception.
12300003 Account not found.

Example

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

const accountAbility: distributedAccount.DistributedAccountAbility = distributedAccount.getDistributedAccountAbility();
try {
  accountAbility.getOsAccountDistributedInfoByLocalId(100).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}`);
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`getOsAccountDistributedInfoByLocalId exception: code is ${err.code}, message is ${err.message}`);
}

setOsAccountDistributedInfoByLocalId10+

setOsAccountDistributedInfoByLocalId(localId: number, distributedInfo: DistributedInfo, callback: AsyncCallback<void>): void

Sets the distributed information for a system account. This API uses an asynchronous callback to return the result.

System API: This is a system API.

System capability: SystemCapability.Account.OsAccount

Required permissions: ohos.permission.MANAGE_DISTRIBUTED_ACCOUNTS

Parameters

Name Type Mandatory Description
localId number Yes ID of the target system account.
distributedInfo DistributedInfo Yes Distributed account information to set.
callback AsyncCallback<void> Yes Callback used to return the result. If the distributed information is set successfully, err is undefined. Otherwise, err is an error object.

Error codes

ID Error Message
201 Permission denied.
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
12300001 System service exception.
12300002 Invalid distributedInfo.
12300003 Account identified by localId or by distributedInfo not found.
12300008 Restricted OS account.

Example

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

const accountAbility: distributedAccount.DistributedAccountAbility = distributedAccount.getDistributedAccountAbility();
let accountInfo: distributedAccount.DistributedInfo =
  { id: '12345', name: 'ZhangSan', event: 'Ohos.account.event.LOGIN' };
try {
  accountAbility.setOsAccountDistributedInfoByLocalId(100, accountInfo, (err: BusinessError) => {
    if (err) {
      console.error(`setOsAccountDistributedInfoByLocalId exception: code is ${err.code}, message is ${err.message}`);
    } else {
      console.info('setOsAccountDistributedInfoByLocalId successfully');
    }
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`setOsAccountDistributedInfoByLocalId exception: code is ${err.code}, message is ${err.message}`);
}

setOsAccountDistributedInfoByLocalId10+

setOsAccountDistributedInfoByLocalId(localId: number, distributedInfo: DistributedInfo): Promise<void>

Sets the distributed information for a system account. This API uses a promise to return the result.

System API: This is a system API.

System capability: SystemCapability.Account.OsAccount

Required permissions: ohos.permission.MANAGE_DISTRIBUTED_ACCOUNTS

Parameters

Name Type Mandatory Description
localId number Yes ID of the target system account.
distributedInfo DistributedInfo Yes Distributed account information to set.

Return value

Type Description
Promise<void> Promise that returns no value.

Error codes

ID Error Message
201 Permission denied.
202 Not system application.
401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
12300001 System service exception.
12300002 Invalid distributedInfo.
12300003 Account identified by localId or by distributedInfo not found.
12300008 Restricted OS account.

Example

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

const accountAbility: distributedAccount.DistributedAccountAbility = distributedAccount.getDistributedAccountAbility();
let accountInfo: distributedAccount.DistributedInfo =
  { id: '12345', name: 'ZhangSan', event: 'Ohos.account.event.LOGIN' };
try {
  accountAbility.setOsAccountDistributedInfoByLocalId(100, accountInfo).then(() => {
    console.info('setOsAccountDistributedInfoByLocalId successfully');
  }).catch((err: BusinessError) => {
    console.error(`setOsAccountDistributedInfoByLocalId exception: code is ${err.code}, message is ${err.message}`);
  });
} catch (e) {
  const err = e as BusinessError;
  console.error(`setOsAccountDistributedInfoByLocalId exception: code is ${err.code}, message is ${err.message}`);
}