harmony 鸿蒙@ohos.account.distributedAccount (Distributed Account Management)

2022-08-09 浏览 (867)

@ohos.account.distributedAccount (Distributed Account Management)

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.

Modules to Import

import account_distributedAccount from '@ohos.account.distributedAccount';

account_distributedAccount.getDistributedAccountAbility

getDistributedAccountAbility(): DistributedAccountAbility

Obtains a DistributedAccountAbility instance.

System capability: SystemCapability.Account.OsAccount

Return value

TypeDescription
DistributedAccountAbilityDistributedAccountAbility instance obtained. This instance provides APIs for querying and updating the login state of a distributed account.

Example

const accountAbility = account_distributedAccount.getDistributedAccountAbility();

DistributedAccountAbility

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

getOsAccountDistributedInfo9+

getOsAccountDistributedInfo(callback: AsyncCallback<DistributedInfo>): void

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

System capability: SystemCapability.Account.OsAccount

Required permissions: ohos.permission.MANAGE_DISTRIBUTED_ACCOUNTS, ohos.permission.GET_DISTRIBUTED_ACCOUNTS, or ohos.permission.DISTRIBUTED_DATASYNC

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<DistributedInfo>YesCallback invoked 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

IDError Message
12300001System service exception.

Example

import { BusinessError } from '@ohos.base';

const accountAbility = account_distributedAccount.getDistributedAccountAbility();
try {
  accountAbility.getOsAccountDistributedInfo(
    (err: BusinessError, data: account_distributedAccount.DistributedInfo) => {
      if (err) {
        console.log('getOsAccountDistributedInfo exception: ' + JSON.stringify(err));
      } else {
        console.log('distributed information: ' + JSON.stringify(data));
      }
    });
} catch (err) {
  console.log('getOsAccountDistributedInfo exception: ' + JSON.stringify(err));
}

getOsAccountDistributedInfo9+

getOsAccountDistributedInfo(): Promise<DistributedInfo>

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

System capability: SystemCapability.Account.OsAccount

Required permissions: ohos.permission.MANAGE_DISTRIBUTED_ACCOUNTS, ohos.permission.GET_DISTRIBUTED_ACCOUNTS, or ohos.permission.DISTRIBUTED_DATASYNC

Return value

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

Error codes

IDError Message
12300001System service exception.

Example

import { BusinessError } from '@ohos.base';

const accountAbility = account_distributedAccount.getDistributedAccountAbility();
try {
  accountAbility.getOsAccountDistributedInfo().then((data: account_distributedAccount.DistributedInfo) => {
      console.log('distributed information: ' + JSON.stringify(data));
  }).catch((err: BusinessError) => {
      console.log('getOsAccountDistributedInfo exception: '  + JSON.stringify(err));
  });
} catch (err) {
  console.log('getOsAccountDistributedInfo exception: ' + JSON.stringify(err));
}

getOsAccountDistributedInfoByLocalId10+

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

Obtains distributed information about an OS 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

Parameters

NameTypeMandatoryDescription
localIdnumberYesID of the target OS account.
callbackAsyncCallback<DistributedInfo>YesCallback invoked 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

IDError Message
12300001System service exception.
12300003Account not found.

Example

import { BusinessError } from '@ohos.base';

const accountAbility = account_distributedAccount.getDistributedAccountAbility();
try {
  accountAbility.getOsAccountDistributedInfoByLocalId(100,
    (err: BusinessError, data: account_distributedAccount.DistributedInfo) => {
      if (err) {
        console.log('getOsAccountDistributedInfoByLocalId exception: ' + JSON.stringify(err));
      } else {
        console.log('distributed information: ' + JSON.stringify(data));
      }
    });
} catch (err) {
  console.log('getOsAccountDistributedInfoByLocalId exception: ' + JSON.stringify(err));
}

getOsAccountDistributedInfoByLocalId10+

getOsAccountDistributedInfoByLocalId(localId: number): Promise<DistributedInfo>

Obtains distributed information about an OS 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

Return value

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

Error codes

IDError Message
12300001System service exception.
12300003Account not found.

Example

import { BusinessError } from '@ohos.base';

const accountAbility = account_distributedAccount.getDistributedAccountAbility();
try {
  accountAbility.getOsAccountDistributedInfoByLocalId(100).then((
    data: account_distributedAccount.DistributedInfo) => {
    console.log('distributed information: ' + JSON.stringify(data));
  }).catch((err: BusinessError) => {
    console.log('getOsAccountDistributedInfoByLocalId exception: '  + JSON.stringify(err));
  });
} catch (err) {
  console.log('getOsAccountDistributedInfoByLocalId exception: ' + JSON.stringify(err));
}

queryOsAccountDistributedInfo(deprecated)

queryOsAccountDistributedInfo(callback: AsyncCallback<DistributedInfo>): void

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

NOTE

This API is supported since API version 7 and deprecated since API version 9. You are advised to use getOsAccountDistributedInfo.

System capability: SystemCapability.Account.OsAccount

Required permissions: ohos.permission.MANAGE_LOCAL_ACCOUNTS or ohos.permission.DISTRIBUTED_DATASYNC

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<DistributedInfo>YesCallback invoked 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.

Example

import { BusinessError } from '@ohos.base';

const accountAbility = account_distributedAccount.getDistributedAccountAbility();
accountAbility.queryOsAccountDistributedInfo(
  (err: BusinessError, data: account_distributedAccount.DistributedInfo) => {
    if (err) {
      console.log('queryOsAccountDistributedInfo exception: ' + JSON.stringify(err));
    } else {
      console.log('distributed information: ' + JSON.stringify(data));
    }
  });

queryOsAccountDistributedInfo(deprecated)

queryOsAccountDistributedInfo(): Promise<DistributedInfo>

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

NOTE

This API is supported since API version 7 and deprecated since API version 9. You are advised to use getOsAccountDistributedInfo.

System capability: SystemCapability.Account.OsAccount

Required permissions: ohos.permission.MANAGE_LOCAL_ACCOUNTS or ohos.permission.DISTRIBUTED_DATASYNC

Return value

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

Example

import { BusinessError } from '@ohos.base';

const accountAbility = account_distributedAccount.getDistributedAccountAbility();
accountAbility.queryOsAccountDistributedInfo().then((data: account_distributedAccount.DistributedInfo) => {
    console.log('distributed information: ' + JSON.stringify(data));
}).catch((err: BusinessError) => {
    console.log('queryOsAccountDistributedInfo exception: '  + JSON.stringify(err));
});

setOsAccountDistributedInfo9+

setOsAccountDistributedInfo(accountInfo: DistributedInfo, callback: AsyncCallback<void>): void

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

System capability: SystemCapability.Account.OsAccount

Required permissions: ohos.permission.MANAGE_DISTRIBUTED_ACCOUNTS

Parameters

NameTypeMandatoryDescription
accountInfoDistributedInfoYesDistributed account information to set.
callbackAsyncCallback<void>YesCallback invoked to return the result. If the distributed account information is set successfully, err is undefined. Otherwise, err is an error object.

Error codes

IDError Message
12300001System service exception.
12300002Invalid accountInfo.
12300003Account not found.

Example

import { BusinessError } from '@ohos.base';

const accountAbility = account_distributedAccount.getDistributedAccountAbility();
let accountInfo: account_distributedAccount.DistributedInfo =
  {id: '12345', name: 'ZhangSan', event: 'Ohos.account.event.LOGIN'};
try {
  accountAbility.setOsAccountDistributedInfo(accountInfo, (err: BusinessError) => {
    if (err) {
      console.log('setOsAccountDistributedInfo exception: ' + JSON.stringify(err));
    } else {
      console.log('setOsAccountDistributedInfo successfully');
    }
  });
} catch (err) {
    console.log('setOsAccountDistributedInfo exception: ' + JSON.stringify(err));
}

setOsAccountDistributedInfo9+

setOsAccountDistributedInfo(accountInfo: DistributedInfo): Promise<void>

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

System capability: SystemCapability.Account.OsAccount

Required permissions: ohos.permission.MANAGE_DISTRIBUTED_ACCOUNTS

Parameters

NameTypeMandatoryDescription
accountInfoDistributedInfoYesDistributed account information to set.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

IDError Message
12300001System service exception.
12300002Invalid accountInfo.
12300003Account not found.

Example

import { BusinessError } from '@ohos.base';

const accountAbility = account_distributedAccount.getDistributedAccountAbility();
let accountInfo: account_distributedAccount.DistributedInfo =
  {id: '12345', name: 'ZhangSan', event: 'Ohos.account.event.LOGIN'};
try {
  accountAbility.setOsAccountDistributedInfo(accountInfo).then(() => {
      console.log('setOsAccountDistributedInfo successfully');
  }).catch((err: BusinessError) => {
      console.log('setOsAccountDistributedInfo exception: '  + JSON.stringify(err));
  });
} catch (err) {
    console.log('setOsAccountDistributedInfo exception: ' + JSON.stringify(err));
}

setOsAccountDistributedInfoByLocalId10+

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

Sets the distributed information for an OS 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

NameTypeMandatoryDescription
localIdnumberYesID of the target OS account.
accountInfoDistributedInfoYesDistributed account information to set.
callbackAsyncCallback<void>YesCallback invoked to return the result. If the distributed information is set successfully, err is undefined. Otherwise, err is an error object.

Error codes

IDError Message
12300001System service exception.
12300002Invalid distributedInfo.
12300003Account identified by localId or by distributedInfo not found.
12300008Restricted OS account.

Example

import { BusinessError } from '@ohos.base';

const accountAbility = account_distributedAccount.getDistributedAccountAbility();
let accountInfo: account_distributedAccount.DistributedInfo =
  {id: '12345', name: 'ZhangSan', event: 'Ohos.account.event.LOGIN'};
try {
  accountAbility.setOsAccountDistributedInfoByLocalId(100, accountInfo, (err: BusinessError) => {
    if (err) {
      console.log('setOsAccountDistributedInfoByLocalId exception: ' + JSON.stringify(err));
    } else {
      console.log('setOsAccountDistributedInfoByLocalId successfully');
    }
  });
} catch (err) {
    console.log('setOsAccountDistributedInfoByLocalId exception: ' + JSON.stringify(err));
}

setOsAccountDistributedInfoByLocalId10+

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

Sets the distributed information for an OS 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

NameTypeMandatoryDescription
localIdnumberYesID of the target OS account.
distributedInfoDistributedInfoYesDistributed account information to set.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

IDError Message
12300001System service exception.
12300002Invalid distributedInfo.
12300003Account identified by localId or by distributedInfo not found.
12300008Restricted OS account.

Example

import { BusinessError } from '@ohos.base';

const accountAbility = account_distributedAccount.getDistributedAccountAbility();
let accountInfo: account_distributedAccount.DistributedInfo =
  {id: '12345', name: 'ZhangSan', event: 'Ohos.account.event.LOGIN'};
try {
  accountAbility.setOsAccountDistributedInfoByLocalId(100, accountInfo).then(() => {
      console.log('setOsAccountDistributedInfoByLocalId successfully');
  }).catch((err: BusinessError) => {
      console.log('setOsAccountDistributedInfoByLocalId exception: '  + JSON.stringify(err));
  });
} catch (err) {
    console.log('setOsAccountDistributedInfoByLocalId exception: ' + JSON.stringify(err));
}

updateOsAccountDistributedInfo(deprecated)

updateOsAccountDistributedInfo(accountInfo: DistributedInfo, callback: AsyncCallback<void>): void

Updates the distributed account information. This API uses an asynchronous callback to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 9. You are advised to use setOsAccountDistributedInfo.

System capability: SystemCapability.Account.OsAccount

Required permissions: ohos.permission.MANAGE_LOCAL_ACCOUNTS

Parameters

NameTypeMandatoryDescription
accountInfoDistributedInfoYesNew distributed account information.
callbackAsyncCallback<void>YesCallback invoked to return the result. If the distributed account information is set successfully, err is undefined. Otherwise, err is an error object.

Example

import { BusinessError } from '@ohos.base';

const accountAbility = account_distributedAccount.getDistributedAccountAbility();
let accountInfo: account_distributedAccount.DistributedInfo =
  {id: '12345', name: 'ZhangSan', event: 'Ohos.account.event.LOGIN'};
accountAbility.updateOsAccountDistributedInfo(accountInfo, (err: BusinessError) => {
  if (err) {
    console.log('queryOsAccountDistributedInfo exception: ' + JSON.stringify(err));
  } else {
    console.log('queryOsAccountDistributedInfo successfully');
  }
});

updateOsAccountDistributedInfo(deprecated)

updateOsAccountDistributedInfo(accountInfo: DistributedInfo): Promise<void>

Updates the distributed account information. This API uses a promise to return the result.

NOTE

This API is supported since API version 7 and deprecated since API version 9. You are advised to use setOsAccountDistributedInfo. System capability: SystemCapability.Account.OsAccount

Required permissions: ohos.permission.MANAGE_LOCAL_ACCOUNTS

Parameters

NameTypeMandatoryDescription
accountInfoDistributedInfoYesNew distributed account information.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Example

import { BusinessError } from '@ohos.base';

const accountAbility = account_distributedAccount.getDistributedAccountAbility();
let accountInfo: account_distributedAccount.DistributedInfo =
  {id: '12345', name: 'ZhangSan', event: 'Ohos.account.event.LOGIN'};
accountAbility.updateOsAccountDistributedInfo(accountInfo).then(() => {
    console.log('updateOsAccountDistributedInfo successfully');
 }).catch((err: BusinessError) => {
    console.log('updateOsAccountDistributedInfo exception: '  + JSON.stringify(err));
});

DistributedInfo

Defines the distributed information about an OS account.

System capability: SystemCapability.Account.OsAccount

NameTypeMandatoryDescription
namestringYesName of the distributed account. It must be a non-null string.
idstringYesUID of the distributed account. It must be a non-null string.
eventstringYesLogin state of the 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
nickname9+stringNoNickname of the distributed account. By default, no value is passed.
avatar9+stringNoAvatar of the distributed account. By default, no value is passed.
status10+DistributedAccountStatusNoStatus of the distributed account. The value is of the enumerated type. The default status is unlogged.
scalableData8+objectNoExtended information about the distributed account, passed in key-value (KV) pairs based on service requirements. By default, no value is passed.

DistributedAccountStatus10+

Enumerates the statuses of a distributed account.

System capability: SystemCapability.Account.OsAccount

NameValueDescription
NOT_LOGGED_IN0The account has not logged in.
LOGGED_IN1The account has logged in.

你可能感兴趣的鸿蒙文章

harmony 鸿蒙APIs

harmony 鸿蒙System Common Events (To Be Deprecated Soon)

harmony 鸿蒙System Common Events

harmony 鸿蒙API Reference Document Description

harmony 鸿蒙Enterprise Device Management Overview (for System Applications Only)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager)

harmony 鸿蒙@ohos.distributedBundle (Distributed Bundle Management)

harmony 鸿蒙@ohos.bundle (Bundle)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (EnterpriseAdminExtensionAbility)

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/ece9c9eb2a3747f4b86d07a08090a9f2