本模块提供管理分布式账号的一些基础功能,主要包括查询和更新账号登录状态。适用于多设备协同场景,提升跨设备账号管理的一致性和用户体验。典型使用场景包括多设备协同、分布式数据同步、跨设备能力调用等。
本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
getDistributedAccountAbility(): DistributedAccountAbility
获取分布式账号的单实例对象。
系统能力: SystemCapability.Account.OsAccount
返回值:
| 类型 | 说明 |
|---|---|
| DistributedAccountAbility | 返回一个实例,实例提供查询和更新分布式账号登录状态方法。 |
示例:
// 获取分布式账号的单实例对象 const accountAbility: distributedAccount.DistributedAccountAbility = distributedAccount.getDistributedAccountAbility();
getOsAccountDistributedInfo(callback: AsyncCallback<DistributedInfo>): void
获取分布式账号信息。使用callback异步回调。
系统能力: SystemCapability.Account.OsAccount
需要权限: ohos.permission.MANAGE_DISTRIBUTED_ACCOUNTS(仅系统应用可申请)或ohos.permission.GET_DISTRIBUTED_ACCOUNTS(仅系统应用可申请)或ohos.permission.DISTRIBUTED_DATASYNC
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| callback | AsyncCallback<DistributedInfo> | 是 | 回调函数。当获取分布式账号信息成功时,err为undefined,data为获取到的分布式账号信息对象;否则err为错误对象。 |
错误码:
| 错误码ID | 错误信息 |
|---|---|
| 201 | Permission denied. |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
| 12300001 | System service exception. |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
// 获取分布式账号的单实例对象
const accountAbility: distributedAccount.DistributedAccountAbility = distributedAccount.getDistributedAccountAbility();
try {
accountAbility.getOsAccountDistributedInfo(
(err: BusinessError, data: distributedAccount.DistributedInfo) => {
if (err) {
console.error(`getOsAccountDistributedInfo 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(`getOsAccountDistributedInfo exception: code is ${err.code}, message is ${err.message}`);
} getOsAccountDistributedInfo(): Promise<DistributedInfo>
获取分布式账号信息。使用Promise异步回调。
系统能力: SystemCapability.Account.OsAccount
需要权限: ohos.permission.MANAGE_DISTRIBUTED_ACCOUNTS(仅系统应用可申请)或ohos.permission.GET_DISTRIBUTED_ACCOUNTS(仅系统应用可申请)或ohos.permission.DISTRIBUTED_DATASYNC
返回值:
| 类型 | 说明 |
|---|---|
| Promise<DistributedInfo> | Promise对象,返回分布式账号信息对象。 |
错误码:
| 错误码ID | 错误信息 |
|---|---|
| 201 | Permission denied. |
| 12300001 | System service exception. |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
// 获取分布式账号的单实例对象
const accountAbility: distributedAccount.DistributedAccountAbility = distributedAccount.getDistributedAccountAbility();
try {
accountAbility.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}`);
});
} catch (e) {
const err = e as BusinessError;
console.error(`getOsAccountDistributedInfo exception: code is ${err.code}, message is ${err.message}`);
} queryOsAccountDistributedInfo(callback: AsyncCallback<DistributedInfo>): void
获取分布式账号信息。使用callback异步回调。
系统能力: SystemCapability.Account.OsAccount
需要权限: ohos.permission.MANAGE_LOCAL_ACCOUNTS(仅系统应用可申请)或ohos.permission.DISTRIBUTED_DATASYNC
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| callback | AsyncCallback<DistributedInfo> | 是 | 回调函数。当获取分布式账号信息成功时,err为undefined,data为获取到的分布式账号信息对象;否则err为错误对象。 |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
// 获取分布式账号的单实例对象
const accountAbility: distributedAccount.DistributedAccountAbility = distributedAccount.getDistributedAccountAbility();
accountAbility.queryOsAccountDistributedInfo(
(err: BusinessError, data: distributedAccount.DistributedInfo) => {
if (err) {
console.error(`queryOsAccountDistributedInfo exception: code is ${err.code}, message is ${err.message}`);
} else {
console.info('distributed information: ' + JSON.stringify(data));
}
}); queryOsAccountDistributedInfo(): Promise<DistributedInfo>
获取分布式账号信息。使用Promise异步回调。
系统能力: SystemCapability.Account.OsAccount
需要权限: ohos.permission.MANAGE_LOCAL_ACCOUNTS(仅系统应用可申请)或ohos.permission.DISTRIBUTED_DATASYNC
返回值:
| 类型 | 说明 |
|---|---|
| Promise<DistributedInfo> | Promise对象,返回分布式账号信息对象。 |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
// 获取分布式账号的单实例对象
const accountAbility: distributedAccount.DistributedAccountAbility = distributedAccount.getDistributedAccountAbility();
accountAbility.queryOsAccountDistributedInfo().then((data: distributedAccount.DistributedInfo) => {
console.info('distributed information: ' + JSON.stringify(data));
}).catch((err: BusinessError) => {
console.error(`queryOsAccountDistributedInfo exception: code is ${err.code}, message is ${err.message}`);
}); setOsAccountDistributedInfo(accountInfo: DistributedInfo, callback: AsyncCallback<void>): void
更新分布式账号信息。使用callback异步回调。
系统能力: SystemCapability.Account.OsAccount
需要权限: ohos.permission.MANAGE_DISTRIBUTED_ACCOUNTS,该权限仅系统应用可申请。
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| accountInfo | DistributedInfo | 是 | 分布式账号信息。 |
| callback | AsyncCallback<void> | 是 | 回调函数。当更新分布式账号信息成功时,err为undefined,否则err为错误对象。 |
错误码:
| 错误码ID | 错误信息 |
|---|---|
| 201 | Permission denied. |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
| 12300001 | System service exception. |
| 12300002 | Invalid accountInfo. |
| 12300003 | Account not found. |
| 12300406 | The distributed account information has already been bound to a sub-profile of the same OS account. 适用版本:26.0.0+ |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
// 获取分布式账号的单实例对象
const accountAbility: distributedAccount.DistributedAccountAbility = distributedAccount.getDistributedAccountAbility();
// 示例值,实际使用时请通过getOsAccountDistributedInfo获取真实分布式账号信息
let accountInfo: distributedAccount.DistributedInfo =
{ id: '12345', name: 'ZhangSan', event: 'Ohos.account.event.LOGIN' };
try {
accountAbility.setOsAccountDistributedInfo(accountInfo, (err: BusinessError) => {
if (err) {
console.error(`setOsAccountDistributedInfo exception: code is ${err.code}, message is ${err.message}`);
} else {
console.info('setOsAccountDistributedInfo successfully');
}
});
} catch (e) {
const err = e as BusinessError;
console.error(`setOsAccountDistributedInfo exception: code is ${err.code}, message is ${err.message}`);
} setOsAccountDistributedInfo(accountInfo: DistributedInfo): Promise<void>
更新分布式账号信息。使用Promise异步回调。
系统能力: SystemCapability.Account.OsAccount
需要权限: ohos.permission.MANAGE_DISTRIBUTED_ACCOUNTS,该权限仅系统应用可申请。
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| accountInfo | DistributedInfo | 是 | 分布式账号信息。 |
返回值:
| 类型 | 说明 |
|---|---|
| Promise<void> | Promise对象,无返回结果。 |
错误码:
| 错误码ID | 错误信息 |
|---|---|
| 201 | Permission denied. |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
| 12300001 | System service exception. |
| 12300002 | Invalid accountInfo. |
| 12300003 | Account not found. |
| 12300406 | The distributed account information has already been bound to a sub-profile of the same OS account. 适用版本:26.0.0+ |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
// 获取分布式账号的单实例对象
const accountAbility: distributedAccount.DistributedAccountAbility = distributedAccount.getDistributedAccountAbility();
// 示例值,实际使用时请通过getOsAccountDistributedInfo获取真实分布式账号信息
let accountInfo: distributedAccount.DistributedInfo =
{ id: '12345', name: 'ZhangSan', event: 'Ohos.account.event.LOGIN' };
try {
accountAbility.setOsAccountDistributedInfo(accountInfo).then(() => {
console.info('setOsAccountDistributedInfo successfully');
}).catch((err: BusinessError) => {
console.error(`setOsAccountDistributedInfo exception: code is ${err.code}, message is ${err.message}`);
});
} catch (e) {
const err = e as BusinessError;
console.error(`setOsAccountDistributedInfo exception: code is ${err.code}, message is ${err.message}`);
} updateOsAccountDistributedInfo(accountInfo: DistributedInfo, callback: AsyncCallback<void>): void
更新分布式账号信息。使用callback异步回调。
系统能力: SystemCapability.Account.OsAccount
需要权限: ohos.permission.MANAGE_LOCAL_ACCOUNTS,该权限仅系统应用可申请。
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| accountInfo | DistributedInfo | 是 | 分布式账号信息。 |
| callback | AsyncCallback<void> | 是 | 回调函数。当更新分布式账号信息成功时,err为undefined,否则err为错误对象。 |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
// 获取分布式账号的单实例对象
const accountAbility: distributedAccount.DistributedAccountAbility = distributedAccount.getDistributedAccountAbility();
// 示例值,实际使用时请通过getOsAccountDistributedInfo获取真实分布式账号信息
let accountInfo: distributedAccount.DistributedInfo =
{ id: '12345', name: 'ZhangSan', event: 'Ohos.account.event.LOGIN' };
accountAbility.updateOsAccountDistributedInfo(accountInfo, (err: BusinessError) => {
if (err) {
console.error(`updateOsAccountDistributedInfo exception: code is ${err.code}, message is ${err.message}`);
} else {
console.info('updateOsAccountDistributedInfo successfully');
}
}); updateOsAccountDistributedInfo(accountInfo: DistributedInfo): Promise<void>
更新分布式账号信息。使用Promise异步回调。
系统能力: SystemCapability.Account.OsAccount
需要权限: ohos.permission.MANAGE_LOCAL_ACCOUNTS,该权限仅系统应用可申请。
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| accountInfo | DistributedInfo | 是 | 分布式账号信息。 |
返回值:
| 类型 | 说明 |
|---|---|
| Promise<void> | Promise对象,无返回结果。 |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
// 获取分布式账号的单实例对象
const accountAbility: distributedAccount.DistributedAccountAbility = distributedAccount.getDistributedAccountAbility();
// 示例值,实际使用时请通过getOsAccountDistributedInfo获取真实分布式账号信息
let accountInfo: distributedAccount.DistributedInfo =
{ id: '12345', name: 'ZhangSan', event: 'Ohos.account.event.LOGIN' };
accountAbility.updateOsAccountDistributedInfo(accountInfo).then(() => {
console.info('updateOsAccountDistributedInfo successfully');
}).catch((err: BusinessError) => {
console.error(`updateOsAccountDistributedInfo exception: code is ${err.code}, message is ${err.message}`);
}); 提供操作系统账号的分布式账号信息。
系统能力: SystemCapability.Account.OsAccount
| 名称 | 类型 | 只读 | 可选 | 说明 |
|---|---|---|---|---|
| name | string | 否 | 否 | 分布式账号名称,非空字符串。最大长度为256个字节。 |
| id | string | 否 | 否 | 分布式账号UID,非空字符串。最大长度为512个字节。 |
| event | string | 否 | 否 | 分布式账号登录状态,包括登录、登出、Token失效和注销,分别对应以下字符串: - Ohos.account.event.LOGIN - Ohos.account.event.LOGOUT - Ohos.account.event.TOKEN_INVALID - Ohos.account.event.LOGOFF |
| nickname9+ | string | 否 | 是 | 分布式账号的昵称,当需要显示用户昵称时设置。不设置时默认为空,不影响账号功能使用。最大长度为1024个字节。 |
| avatar9+ | string | 否 | 是 | 分布式账号的头像,当需要显示用户头像时设置。不设置时默认为空,不影响账号功能使用。 最大长度为10M(10485760个字节)。 |
| status10+ | DistributedAccountStatus | 是 | 是 | 分布式账号的状态,枚举类型。当需要查询或设置账号登录状态时使用。不设置时默认为NOT_LOGGED_IN(未登录状态)。 |
| scalableData8+ | object | 否 | 是 | 分布式账号扩展信息,当需要传递定制化业务信息时设置,以k-v形式传递。不设置时默认为空,不影响账号基本功能。 |