Distributed Account Management
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.
Modules to Import
import account_distributedAccount from '@ohos.account.distributedAccount';
System Capability
SystemCapability.Account.OsAccount
account_distributedAccount.getDistributedAccountAbility
getDistributedAccountAbility(): DistributedAccountAbility
Obtains a DistributedAccountAbility instance.
- Return value
Type | Description |
---|---|
DistributedAccountAbility | DistributedAccountAbility instance obtained. This instance provides methods for querying and updating the login state of a distributed account. |
- Example
const accountAbility = account_distributedAccount.getDistributedAccountAbility();
DistributedAccountAbility
Provides methods for querying and updating the login state of a distributed account. You must obtain a DistributedAccountAbility instance first.
queryOsAccountDistributedInfo
queryOsAccountDistributedInfo(callback: AsyncCallback<DistributedInfo>): void
Obtains distributed account information. This method uses an asynchronous callback to return the result.
Required permissions: ohos.permission.MANAGE_LOCAL_ACCOUNTS or ohos.permission.DISTRIBUTED_DATASYNC (available only to system applications)
- Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
callback | AsyncCallback<DistributedInfo> | Yes | Callback invoked to return the distributed account information obtained. |
- Example
const accountAbility = account_distributedAccount.getDistributedAccountAbility(); accountAbility.queryOsAccountDistributedInfo((err, data) => { console.log("queryOsAccountDistributedInfo err: " + JSON.stringify(err)); console.log('Query account info name: ' + data.name); console.log('Query account info id: ' + data.id); });
queryOsAccountDistributedInfo
queryOsAccountDistributedInfo(): Promise<DistributedInfo>
Obtains distributed account information. This method uses a promise to return the result asynchronously.
Required permissions: ohos.permission.MANAGE_LOCAL_ACCOUNTS or ohos.permission.DISTRIBUTED_DATASYNC (available only to system applications)
- Return value
Type | Description |
---|---|
Promise<DistributedInfo> | Promise used to return the distributed account information obtained. |
- Example
const accountAbility = account_distributedAccount.getDistributedAccountAbility(); accountAbility.queryOsAccountDistributedInfo().then((data) => { console.log('Query account info name: ' + data.name); console.log('Query account info id: ' + data.id); }).catch((err) => { console.log("queryOsAccountDistributedInfoerr: " + JSON.stringify(err)); });
updateOsAccountDistributedInfo
updateOsAccountDistributedInfo(accountInfo: DistributedInfo, callback: AsyncCallback<void>): void
Updates distributed account information. This method uses an asynchronous callback to return the result.
Required permissions: ohos.permission.MANAGE_LOCAL_ACCOUNTS (available only to system applications)
- Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
accountInfo | DistributedInfo | Yes | Distributed account information. |
callback | AsyncCallback<void> | Yes | Callback invoked when the distributed account information is updated. |
- Example
const accountAbility = account_distributedAccount.getDistributedAccountAbility(); let accountInfo = {id: '12345', name: 'ZhangSan', event: 'Ohos.account.event.LOGIN'}; accountAbility.updateOsAccountDistributedInfo(accountInfo, (err) => { console.log("queryOsAccountDistributedInfo err: " + JSON.stringify(err)); });
updateOsAccountDistributedInfo
updateOsAccountDistributedInfo(accountInfo: DistributedInfo): Promise<void>
Updates distributed account information. This method uses a promise to return the result asynchronously.
Required permissions: ohos.permission.MANAGE_LOCAL_ACCOUNTS (available only to system applications)
- Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
accountInfo | DistributedInfo | Yes | Distributed account information. |
- Return value
Type | Description |
---|---|
Promise<void> | Promise used to return the result. |
- Example
const accountAbility = account_distributedAccount.getDistributedAccountAbility(); let accountInfo = {id: '12345', name: 'ZhangSan', event: 'Ohos.account.event.LOGIN'}; accountAbility.updateOsAccountDistributedInfo(accountInfo).then(() => { console.log('updateOsAccountDistributedInfo Success'); }).catch((err) => { console.log("updateOsAccountDistributedInfo err: " + JSON.stringify(err)); });
DistributedInfo
Defines distributed OS account information.
Name | Type | Mandatory | Description |
---|---|---|---|
name | string | Yes | Name of a distributed account. It must be a non-null string. |
id | string | Yes | UID of a distributed account. It must be a non-null string. |
event | string | Yes | Login state of a distributed account. The state can be login, logout, token invalid, or logoff, which correspond to the following strings respectively: - Ohos.account.event.LOGIN - Ohos.account.event.LOGOUT - Ohos.account.event.TOKEN_INVALID - Ohos.account.event.LOGOFF |
scalableData | object | No | Extended information about a distributed account. Customized information is passed in key-value pairs. Note: This parameter is reserved and not used in query and update methods. |