Managing System Accounts (for System Applications Only)
The system provides APIs for managing system accounts. After applying for required permissions for your system application, you can use the APIs to create, activate, modify, and delete system accounts. For third-party applications, you can use the APIs to query basic information about system accounts to develop service logic related to system accounts.
Basic Concepts
Account Type
Currently, only the following types of system accounts can be created:
| Name | Value | Description |
|---|---|---|
| ADMIN | 0 | Administrator account. |
| NORMAL | 1 | Normal account. |
| GUEST | 2 | Guest account. |
| PRIVATE12+ | 1024 | Private account. |
Account Information
For details about complete system account information, see OsAccountInfo.
Getting Started
-
Request the ohos.permission.MANAGE_LOCAL_ACCOUNTS permission. For details, see Requesting Permissions for system_basic Applications.
-
Import the osAccount module.
import { osAccount, BusinessError } from '@kit.BasicServicesKit';
-
Obtain an AccountManager instance.
let accountManager = osAccount.getAccountManager();
Creating a System Account
The default system account is created during the system initialization. The user cal also create multiple system accounts as required.
Procedure
Use createOsAccount to create a system account with the specified name and type.
let name: string = 'Bob';
let type: osAccount.OsAccountType = osAccount.OsAccountType.NORMAL;
accountManager.createOsAccount(name, type, (err: BusinessError, osAccountInfo: osAccount.OsAccountInfo)=>{
if (err) {
console.error(`createOsAccount code is ${err.code}, message is ${err.message}`);
} else {
this.createLocalId = osAccountInfo.localId;
console.info('createOsAccount osAccountInfo:' + JSON.stringify(osAccountInfo));
}
});
Obtaining All System Accounts
The account management page may need to display information about all the system accounts.
Procedure
Use queryAllCreatedOsAccounts to obtain informatory about all system accounts.
accountManager.queryAllCreatedOsAccounts((err: BusinessError, accountArr: osAccount.OsAccountInfo[])=>{
if (err) {
console.info(`queryAllCreatedOsAccounts code is ${err.code}, message is ${err.message}`);
} else {
console.info('queryAllCreatedOsAccounts accountArr:' + JSON.stringify(accountArr));
}
});
Obtaining Information of a System Account
Detailed information about a system account can be obtained based on the account ID.
Procedure
Use queryOsAccountById to obtain detailed information about a system account.
let localId: number = 100;
accountManager.queryOsAccountById(localId, (err: BusinessError, accountInfo: osAccount.OsAccountInfo)=>{
if (err) {
console.error(`queryOsAccountById code is ${err.code}, message is ${err.message}`);
} else {
console.info('queryOsAccountById accountInfo:' + JSON.stringify(accountInfo));
}
});
Changing the ProfilePhoto and Nickname of a System Account
Change the profile photo and nickname of a system account as required.
Procedure
-
Use setOsAccountProfilePhoto to change the profile picture of a system account.
let localId: number = 100;
let newPhoto: string = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA0AAAAPCAYAAAA/I0V3AAAAAXNSR0IArs4c6QAAAARnQU1BAA'+
'Cxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAACwSURBVDhPvZLBDYMwDEV/ugsXRjAT0EHCOuFIBwkbdIRewi6unbiAyoGgSn1SFH85+Y'+
'q/4ljARW62X+LHS8uIzjm4dXUYF+utzBikB52Jo5e5iEPKqpACk7R9NM2RvWm5tIkD2czLCUFNKLD6IjdMHFHDzws285MgGrT0xCtp3WOKHo'+
'+7q0mP0DZW9pNmoEFUzrQjp5cCnaen2kSJXLFD8ghbXyZCMQf/8e8Ns1XVAG/XAgqKzVnJFAAAAABJRU5ErkJggg=='
accountManager.setOsAccountProfilePhoto(localId, newPhoto, (err: BusinessError)=>{
if(err) {
console.error(`setOsAccountProfilePhoto code is ${err.code}, message is ${err.message}`);
} else {
console.info('Successfully updated system account avatar');
}
});
-
Use setOsAccountName to change the system account name.
let localId: number = 100;
// ···
let newName: string = 'Tom';
accountManager.setOsAccountName(localId, newName, (err: BusinessError) => {
if (err) {
console.error(`setOsAccountName failed, code is ${err.code}, message is ${err.message}`);
// ···
} else {
console.info('setOsAccountName successfully');
// ···
}
});
Activating a System Account
System accounts are not activated by default. A system account can be used only after being activated. You can use activateOsAccount to activate a system account.
Procedure
Use activateOsAccount to activate a system account.
let localId: number = 101;
// ···
accountManager.activateOsAccount(localId, (err: BusinessError)=>{
if (err) {
console.error(`activateOsAccount failed, code is ${err.code}, message is ${err.message}`);
// ···
} else {
console.info('activateOsAccount successfully');
// ···
}
});
Removing a System Account
Remove a system account when it is no longer used.
Procedure
Use removeOsAccount to remove a system account.
let localId: number = 101;
// ···
accountManager.removeOsAccount(localId, (err: BusinessError)=>{
if (err) {
console.error(`removeOsAccount failed, code is ${err.code}, message is ${err.message}`);
// ···
} else {
console.info('removeOsAccount successfully');
// ···
}
});