#鸿蒙学习大百科#如何获取设备的OAID?

如何获取设备的OAID?

HarmonyOS
2024-10-23 10:17:08
1180浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
莫名瞄一眼

前提:module.json5中配置ohos.permission.APP_TRACKING_CONSENT权限。

import abilityAccessCtrl from '@ohos.abilityAccessCtrl';
import { BusinessError } from '@ohos.base';
import identifier from '@ohos.identifier.oaid';
import hilog from '@ohos.hilog';
@Entry
@Component
struct Index {
  build() {
    Column() {
      Button("获取OAID").onClick(() => {
        const atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager();
        try {
          atManager.requestPermissionsFromUser(getContext(), ["ohos.permission.APP_TRACKING_CONSENT"]).then((data) => {
            if (data.authResults[0] == 0) {
              hilog.info(0x0000, 'testTag', '%{public}s', 'request permission success');
              identifier.getOAID((err: BusinessError, data: string) => {
                if (err.code) {
                  hilog.error(0x0000, 'testTag', '%{public}s', `get oaid failed, error: ${err.code} ${err.message}`);
                } else {
                  const oaid: string = data;
                  hilog.info(0x0000, 'testTag', '%{public}s', `get oaid by callback success, oaid: ${oaid}`);
                }
              });
            } else {
              hilog.error(0x0000, 'testTag', '%{public}s', 'user rejected');
            }
          }).catch((err: BusinessError) => {
            hilog.error(0x0000, 'testTag', '%{public}s', `request permission failed, error: ${err.code} ${err.message}`);
          })
        } catch (err) {
          hilog.error(0x0000, 'testTag', '%{public}s', `catch err->${err.code}, ${err.message}`);
        }
      })
    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center)
  }

}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.

日志打印:

分享
微博
QQ
微信
回复
2024-10-23 15:48:02


相关问题