StartPage.ets
6.9 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import { router, Prompt } from '@kit.ArkUI';
import { advertising, identifier } from '@kit.AdsKit';
import { common } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
export struct Index {
private context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext;
private oaid: string = '';
private isTimeOut: boolean = false;
// 超时时间(单位毫秒),开发者可根据实际情况修改
private timeOutDuration: number = 1 * 1000;
// 超时index
private timeOutIndex: number = -1;
// 广告展示参数
private adDisplayOptions: advertising.AdDisplayOptions = {
// 是否静音,默认不静音
mute: false
}
// 广告配置
private adOptions: advertising.AdOptions = {
// 是否允许流量下载0:不允许,1:允许,不设置以广告主设置为准
allowMobileTraffic: 0,
// 是否希望根据 COPPA 的规定将您的内容视为面向儿童的内容: -1默认值,不确定 0不希望 1希望
tagForChildProtection: -1,
// 是否希望按适合未达到法定承诺年龄的欧洲经济区 (EEA) 用户的方式处理该广告请求: -1默认值,不确定 0不希望 1希望
tagForUnderAgeOfPromise: -1,
// 设置广告内容分级上限: W: 3+,所有受众 PI: 7+,家长指导 J:12+,青少年 A: 16+/18+,成人受众
adContentClassification: 'A'
}
// 开屏视频广告请求参数
private splashVideoAdReqParams: advertising.AdRequestParams = {
// 'testd7c5cewoj6'为测试专用的广告位ID,App正式发布时需要改为正式的广告位ID
adId: 'testd7c5cewoj6',
adType: AdType.SPLASH_AD,
adCount: 1,
oaid: this.oaid
}
// 开屏图片广告请求参数
private splashImageAdReqParams: advertising.AdRequestParams = {
// 'testq6zq98hecj'为测试专用的广告位ID,App正式发布时需要改为正式的广告位ID
adId: 'testq6zq98hecj',
adType: AdType.SPLASH_AD,
adCount: 1,
oaid: this.oaid
}
aboutToAppear() {
hilog.info(0x0000, 'testTag', '%{public}s', 'Start to aboutToAppear');
try {
// 使用Promise回调方式获取OAID
identifier.getOAID().then((data: string) => {
this.oaid = data;
hilog.info(0x0000, 'testTag', '%{public}s', 'Succeeded in getting adsIdentifierInfo by promise');
}).catch((error: BusinessError) => {
hilog.error(0x0000, 'testTag', '%{public}s',
`Failed to get adsIdentifierInfo, code: ${error.code}, message: ${error.message}`);
})
} catch (error) {
hilog.error(0x0000, 'testTag', '%{public}s', `Catch err, code: ${error.code}, message: ${error.message}`);
}
}
build() {
Column() {
Column() {
// 跳转到开屏全屏视频广告展示页面
CustomButton({
mText: 'splash full screen request', mOnClick: () => {
this.requestAd(this.splashVideoAdReqParams, this.adOptions);
}
});
// 跳转到开屏半屏图片广告展示页面
CustomButton({
mText: 'splash half screen request', mOnClick: () => {
this.requestAd(this.splashImageAdReqParams, this.adOptions);
}
});
}.width('100%').height('80%').justifyContent(FlexAlign.Center)
}
.width('100%')
.height('100%')
}
private requestAd(adReqParams: advertising.AdRequestParams, adOptions: advertising.AdOptions): void {
// 广告请求回调监听
const adLoaderListener: advertising.AdLoadListener = {
// 广告请求失败回调
onAdLoadFailure: (errorCode: number, errorMsg: string) => {
clearTimeout(this.timeOutIndex);
if (this.isTimeOut) {
return;
}
hilog.error(0x0000, 'testTag', '%{public}s',
`Failed to request ad. errorCode is: ${errorCode}, errorMsg is: ${errorMsg}`);
Prompt.showToast({
message: `Failed to request ad, code is: ${errorCode} , errorMsg is: ${errorMsg}`,
duration: 1000
});
},
// 广告请求成功回调
onAdLoadSuccess: (ads: Array<advertising.Advertisement>) => {
clearTimeout(this.timeOutIndex);
if (this.isTimeOut) {
return;
}
hilog.info(0x0000, 'testTag', '%{public}s', 'Succeeded in requesting ad!');
// 保存请求到的广告内容用于展示
hilog.info(0x0000, 'testTag', '%{public}s', `ads[0].adType is : ${ads[0].adType}`);
if (canIUse("SystemCapability.Advertising.Ads")) {
if (ads[0].adType === AdType.SPLASH_AD) {
// 调用开屏广告展示页面
if (ads[0]?.isFullScreen === true) {
routePage('pages/SplashFullScreenAdPage', ads, this.adDisplayOptions);
} else {
routePage('pages/SplashHalfScreenAdPage', ads, this.adDisplayOptions);
}
} else {
hilog.error(0x0000, 'testTag', '%{public}s', 'Error adType');
}
}
}
};
// 创建AdLoader广告对象
const load: advertising.AdLoader = new advertising.AdLoader(this.context);
// 调用广告请求接口
hilog.info(0x0000, 'testTag', '%{public}s', 'Request ad!');
adReqParams.oaid = this.oaid;
this.timeOutHandler();
load.loadAd(adReqParams, adOptions, adLoaderListener);
}
private timeOutHandler(): void {
this.isTimeOut = false;
// 超时处理
this.timeOutIndex = setTimeout(() => {
this.isTimeOut = true;
const options: router.RouterOptions = {
// 开发者可根据项目实际情况修改超时之后要跳转的目标页面
url: 'pages/AdsServicePage',
};
router.pushUrl(options);
hilog.error(0x0000, 'testTag', '%{public}s', 'load ad time out');
}, this.timeOutDuration);
}
}
async function routePage(pageUri: string, ads: Array<advertising.Advertisement | null>,
displayOptions: advertising.AdDisplayOptions) {
let options: router.RouterOptions = {
url: pageUri,
params: {
ads: ads,
displayOptions: displayOptions
}
}
try {
hilog.info(0x0000, 'testTag', '%{public}s', `RoutePage: ${pageUri}`);
router.pushUrl(options);
} catch (error) {
hilog.error(0x0000, 'testTag', '%{public}s',
`Failed to routePage callback, code: ${error.code}, msg: ${error.message}`);
}
}
export enum AdType {
// 开屏广告的类型
SPLASH_AD = 1
}
@Component
export struct CustomButton {
private mText: string | Resource = '';
private mHeight: number = 40;
private mOnClick: (event?: ClickEvent) => void = (): void => {
};
build() {
Column() {
Button(this.mText)
.backgroundColor('#d3d4d6')
.fontSize(20)
.fontColor('#000')
.fontWeight(FontWeight.Normal)
.align(Alignment.Center)
.type(ButtonType.Capsule)
.width('90%')
.height(this.mHeight)
.margin({ top: 10, bottom: 5 })
.onClick(this.mOnClick);
}
}
}