harmony 鸿蒙PermissionRequestResult

  • 2022-08-09
  • 浏览 (737)

PermissionRequestResult

The PermissionRequestResult module defines the result of a permission request. The result is returned when requestPermissionsFromUser is called to request permissions.

NOTE

  • The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
  • The APIs of this module can be used only in the stage model.

Attributes

System capability: SystemCapability.Security.AccessToken

Name Type Readable Writable Description
permissions Array<string> Yes No Permissions requested.
authResults Array<number> Yes No Result of the permission request.
- -1: The permission is not authorized and must be set in Settings without displaying a dialog box.
- 0: The permission is authorized.
- 2: The permission is not authorized due to an invalid request. The possible causes are as follows:
- The permission is not declared in the configuration file.
- The permission name is invalid.
- Special conditions for applying for the permission are not satisfied. See ohos.permission.LOCATION and ohos.permission.APPROXIMATELY_LOCATION.

Usage

The permission request result is obtained through an atManager instance.

Example

The ArkTS syntax does not support direct use of globalThis. A singleton map is required to enable the use of globalThis. You need to perform the following operations:

a. Import the created singleton object GlobalThis to EntryAbility.ets.

    import {GlobalThis} from '../utils/globalThis'; // Set it based on the path of globalThis.ets.

b. Add the following to onCreate:

    GlobalThis.getInstance().setContext('context', this.context);

NOTE

An alert will be generated when a .ets file is imported to a TS file. To prevent the alert, you need to change the file name extension of EntryAbility.ts to EntryAbility.ets and modify the file name extension in module.json5.

The sample code of globalThis.ets is as follows:

import common from '@ohos.app.ability.common';

// Construct a singleton object.
export class GlobalThis {
  private constructor() {}
  private static instance: GlobalThis;
  private _uiContexts = new Map<string, common.UIAbilityContext>();

  public static getInstance(): GlobalThis {
    if (!GlobalThis.instance) {
      GlobalThis.instance = new GlobalThis();
    }
    return GlobalThis.instance;
  }

  getContext(key: string): common.UIAbilityContext|undefined {
    return this._uiContexts.get(key);
  }

  setContext(key: string, value: common.UIAbilityContext): void {
    this._uiContexts.set(key, value);
  }

  // Set other content in the same way.
}
import { BusinessError } from '@ohos.base';
import abilityAccessCtrl from '@ohos.abilityAccessCtrl';
import common from '@ohos.app.ability.common';
import { GlobalThis } from '../utils/globalThis';

let atManager = abilityAccessCtrl.createAtManager();
try {
  let context: common.UIAbilityContext = GlobalThis.getInstance().getContext('context');
  atManager.requestPermissionsFromUser(context, ["ohos.permission.CAMERA"]).then((data) => {
      console.info("data:" + JSON.stringify(data));
      console.info("data permissions:" + data.permissions);
      console.info("data authResults:" + data.authResults);
  }).catch((err: BusinessError) => {
      console.info("data:" + JSON.stringify(err));
  })
} catch(err) {
  console.log(`catch err->${JSON.stringify(err)}`);
}

你可能感兴趣的鸿蒙文章

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)

0  赞