【HarmonyOS】广告标识ID设置装备唯一标识
媒介
1.广告标识ID-OAID是什么?:
开放匿名装备标识符(Open Anonymous Device Identifier, OAID,以下简称OAID):是一种非永世性装备标识符,基于开放匿名装备标识符,可在保护用户个人数据隐私安全的前提下,向用户提供个性化广告
2.OAID并非长期的唯一标识,不能当做DevicID使用:
固然OAID是装备级标识符,同一台装备上不同的App获取到的OAID值一样。
但是当用户规复手机出厂设置。用户操纵重置OAID。都会导致OAID厘革。并且最重要的是默认手机装备是没有开启【跨应用关联访问权限】,通过体系API获取到的是全0的值,没有效。只有当用户自动开启了,你去申请才气拿到值。
3.开启权限弹框的前置步骤
**【跨应用关联访问权限】**这个名字挺抽象的,你可以明白为安卓和苹果手机常说的跟踪访问大概广告ID权限。并且在鸿蒙中,同一台装备上当有应用去申请“跨应用关联访问权限”开关时,会初次生成OAID。
只有当用户开启时,调用API才会有自动提示用户授权的弹框,如下图:
代码实现:
代码运行日志:
调用函数:
- import { identifier } from '@kit.AdsKit';
- import { abilityAccessCtrl, common } from '@kit.AbilityKit';
- import { hilog } from '@kit.PerformanceAnalysisKit';
- import { BusinessError } from '@kit.BasicServicesKit';
- @Entry
- @Component
- struct Index {
- onClickGetOAID = ()=>{
- this.requestOAIDTrackingConsentPermissions(getContext());
- }
- private requestOAIDTrackingConsentPermissions(context: common.Context): void {
- // 进入页面时,向用户请求授权广告跨应用关联访问权限
- const atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager();
- try {
- atManager.requestPermissionsFromUser(context, ["ohos.permission.APP_TRACKING_CONSENT"]).then((data) => {
- if (data.authResults[0] === 0) {
- hilog.info(0x0000, 'testTag', '%{public}s', 'succeeded in requesting permission');
- 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', `succeeded in getting oaid by callback , 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}`);
- }
- }
- build() {
- RelativeContainer() {
- Text("点击请求用户OAID")
- .id('IndexHelloWorld')
- .fontSize(50)
- .fontWeight(FontWeight.Bold)
- .alignRules({
- center: { anchor: '__container__', align: VerticalAlign.Center },
- middle: { anchor: '__container__', align: HorizontalAlign.Center }
- })
- .onClick(this.onClickGetOAID)
- }
- .height('100%')
- .width('100%')
- }
- }
复制代码 申请权限配置文件:
ohos.permission.APP_TRACKING_CONSENT
- {
- "module": {
- "name": "entry",
- "type": "entry",
- "description": "$string:module_desc",
- "mainElement": "EntryAbility",
- "deviceTypes": [
- "phone",
- "tablet",
- "2in1"
- ],
- "deliveryWithInstall": true,
- "installationFree": false,
- "pages": "$profile:main_pages",
- "abilities": [
- {
- "name": "EntryAbility",
- "srcEntry": "./ets/entryability/EntryAbility.ets",
- "description": "$string:EntryAbility_desc",
- "icon": "$media:layered_image",
- "label": "$string:EntryAbility_label",
- "startWindowIcon": "$media:startIcon",
- "startWindowBackground": "$color:start_window_background",
- "exported": true,
- "skills": [
- {
- "entities": [
- "entity.system.home"
- ],
- "actions": [
- "action.system.home"
- ]
- }
- ]
- }
- ],
- "extensionAbilities": [
- {
- "name": "EntryBackupAbility",
- "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets",
- "type": "backup",
- "exported": false,
- "metadata": [
- {
- "name": "ohos.extension.backup",
- "resource": "$profile:backup_config"
- }
- ],
- }
- ],
- "requestPermissions": [
- {
- "name": "ohos.permission.APP_TRACKING_CONSENT",
- "reason": "$string:reason",
- "usedScene": {
- "abilities": [
- "EntryAbility"
- ],
- "when": "inuse"
- }
- }
- ]
- }
- }
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |