openharmony 鸿蒙 manage-os-account-sys

2026-08-25 浏览 (1)

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:

NameValueDescription
ADMIN0Administrator account.
NORMAL1Normal account.
GUEST2Guest account.
PRIVATE12+1024Private account.

Account Information

For details about complete system account information, see OsAccountInfo.

Getting Started

  1. Request the ohos.permission.MANAGE_LOCAL_ACCOUNTS permission. For details, see Requesting Permissions for system_basic Applications.

  2. Import the osAccount module.

import { osAccount, BusinessError } from '@kit.BasicServicesKit';
  1. 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

  1. 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');
      }
    });
  1. 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');
		// ···
      }
    });

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 manage-distributed-account-sys

openharmony 鸿蒙 account-overview-sys

openharmony 鸿蒙 manage-domain-account-sys

openharmony 鸿蒙 auth-domain-account-sys

openharmony 鸿蒙 Readme-EN

openharmony 鸿蒙 manage-os-account-credential-sys

openharmony 鸿蒙 manage-application-account

openharmony 鸿蒙 manage-domain-plugin-sys

openharmony 鸿蒙 control-os-account-by-constraints-sys

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