public class AppAccountAbility extends Object
For example, you can use these methods to add or delete an application account, update account information, authorize access to an application account, and subscribe to or unsubscribe from account changes.
SystemCapability.Account#APP_ACCOUNT| 构造器 | 说明 |
|---|---|
AppAccountAbility(Context context) |
A constructor used to create an
AppAccountAbility instance based on the specified context. |
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
boolean |
addAccount(String name,
String extraInfo) |
Adds the account name and extra information of this application to the account management service.
|
boolean |
deleteAccount(String name) |
Deletes an application account from the account management service.
|
boolean |
disableAppAccess(String name,
String bundleName) |
Disables a third-party application with the specified bundle name from
accessing the given application account.
|
boolean |
enableAppAccess(String name,
String bundleName) |
Enables a third-party application with the specified bundle name to access the given application
account for data query and listening.
|
String |
getAccountCredential(String name,
String credentialType) |
Obtains the credential of this application account.
|
String |
getAccountExtraInfo(String name) |
Obtains extra information of this application account.
|
List<AppAccount> |
getAllAccessibleAccounts() |
Obtains information about all accessible accounts.
|
List<AppAccount> |
getAllAccounts(String owner) |
Obtains information about all accounts of a specified account owner.
|
boolean |
getAppAccountSync(String name) |
Checks whether a specified application account allows application data synchronization.
|
String |
getAssociatedData(String name,
String key) |
Obtains data associated with this application account.
|
boolean |
setAccountCredential(String name,
String credentialType,
String credential) |
Sets the credential for this application account.
|
boolean |
setAccountExtraInfo(String name,
String extraInfo) |
Sets extra information for this application account.
|
boolean |
setAppAccountSync(String name,
boolean isEnable) |
Sets whether a specified application account allows application data synchronization.
|
boolean |
setAssociatedData(String name,
String key,
String value) |
Sets data associated with this application account.
|
boolean |
subscribeAccountEvent(IAppAccountSubscriber subscriber,
List<String> owners) |
Subscribes to the change events of accounts of the specified owners.
|
boolean |
unsubscribeAccountEvent(IAppAccountSubscriber subscriber) |
Unsubscribes from account events.
|
public AppAccountAbility(Context context)
AppAccountAbility instance based on the specified context.context - Indicates the application context.public List<AppAccount> getAllAccounts(String owner)
This method applies to the following accounts:
ohos.permission.GET_ACCOUNTS_PRIVILEGED permission.owner - Indicates the account owner of your application or third-party applications.public boolean subscribeAccountEvent(IAppAccountSubscriber subscriber, List<String> owners)
When the account owner updates the account, the subscriber will receive a notification about the account change event.
subscriber - Indicates the subscriber, which is specified by IAppAccountSubscriber.owners - Indicates the account owners, which are specified
by AppAccount#AppAccount(String name, String owner).true if the subscription is successful; returns false otherwise.public boolean unsubscribeAccountEvent(IAppAccountSubscriber subscriber)
subscriber - Indicates the subscriber, which is specified by IAppAccountSubscriber.true if the unsubscription is successful; returns false otherwise.public boolean addAccount(String name, String extraInfo)
Only the owner of the application account has the permission to call this method.
name - Indicates the name of the application account to add.extraInfo - Indicates the extra information of the application account to add.
The extra information cannot be sensitive information of the application account.true if the operation is successful; returns false otherwise.public boolean deleteAccount(String name)
Only the owner of the application account has the permission to call this method.
name - Indicates the name of the application account to delete.true if the operation is successful; returns false otherwise.public List<AppAccount> getAllAccessibleAccounts()
This method applies to the following accounts:
public boolean enableAppAccess(String name, String bundleName)
name - Indicates the name of the application account.bundleName - Indicates the bundle name of the third-party application.true if the third-party application is enabled to access
the given application account; returns false otherwise.public boolean disableAppAccess(String name, String bundleName)
name - Indicates the name of the application account to disable access from
the third-party application.bundleName - Indicates the bundle name of the third-party application.true if the third-party application is disabled from accessing
the given application account; returns false otherwise.public boolean setAccountExtraInfo(String name, String extraInfo)
You can call this method when you forget the extra information of your application account or need to modify the extra information.
name - Indicates the name of the application account.extraInfo - Indicates the extra information to set.true if the operation is successful; returns false otherwise.public String getAccountExtraInfo(String name)
name - Indicates the name of the application account.null in other scenarios,
for example, if the account does not exist.public boolean setAccountCredential(String name, String credentialType, String credential)
name - Indicates the name of the application account.credentialType - Indicates the type of the credential to set.credential - Indicates the credential to set.true if the operation is successful; returns false otherwise.public String getAccountCredential(String name, String credentialType)
name - Indicates the name of the application account.credentialType - Indicates the type of the credential to obtain.public boolean setAssociatedData(String name, String key, String value)
name - Indicates the name of the application account.key - Indicates the key of the data to set. The key can be customized.value - Indicates the value of the data to set.true if the operation is successful; returns false otherwise.public String getAssociatedData(String name, String key)
name - Indicates the name of the application account.key - Indicates the key of the data to obtain.public boolean setAppAccountSync(String name, boolean isEnable)
If the same OHOS account has logged in to multiple devices, these devices constitute a super device through the distributed networking. On the networked devices, you can call this method to set whether to allow cross-device data synchronization. If synchronization is allowed, application data can be synchronized among these devices in the event of any changes related to the application account. If synchronization is not allowed, the application data is stored only on the local device.
Application account-related changes: adding or deleting an application account, setting extra information (such as updating a token), and setting data associated with this application account
Application data that can be synchronized: application account name, token, and data associated with this application account
Permissions: SystemPermission.DISTRIBUTED_DATASYNC
name - Indicates the name of the application account.isEnable - Specifies whether to allow application data synchronization.true if the operation is successful; returns false otherwise.public boolean getAppAccountSync(String name)
If the same OHOS account has logged in to multiple devices, these devices constitute a super device through the distributed networking. On the networked devices, you can call this method to check whether application data can be synchronized.
Permissions: SystemPermission.DISTRIBUTED_DATASYNC
name - Indicates the name of the application account.true if application data synchronization is allowed; returns false otherwise.setAppAccountSync(java.lang.String, boolean)