ref |> 将友盟初始化放至主模块的AbilityStage上面调用,解决友盟统计数据下降的问题
https://developer.umeng.com/docs/119267/detail/2712046 Signed-off-by: xugenyuan <xugenyuan@wondertek.com.cn>
Showing
4 changed files
with
53 additions
and
5 deletions
| @@ -4,13 +4,14 @@ import { common } from '@kit.AbilityKit'; | @@ -4,13 +4,14 @@ import { common } from '@kit.AbilityKit'; | ||
| 4 | import { AccountManagerUtils } from '../utils/AccountManagerUtils'; | 4 | import { AccountManagerUtils } from '../utils/AccountManagerUtils'; |
| 5 | import { UserDataLocal } from '../utils/UserDataLocal'; | 5 | import { UserDataLocal } from '../utils/UserDataLocal'; |
| 6 | import { EmitterUtils } from '../utils/EmitterUtils'; | 6 | import { EmitterUtils } from '../utils/EmitterUtils'; |
| 7 | +import { EmitterEventId } from '../utils/EmitterEventId'; | ||
| 7 | 8 | ||
| 8 | export class UmengStats { | 9 | export class UmengStats { |
| 9 | 10 | ||
| 10 | private static _inited = false | 11 | private static _inited = false |
| 11 | 12 | ||
| 12 | // 启动时调用 | 13 | // 启动时调用 |
| 13 | - static preInit(context: common.UIAbilityContext) { | 14 | + static preInit(context: common.AbilityStageContext) { |
| 14 | 15 | ||
| 15 | // 需在 AppScope/resources/rawfile/umconfig.json 配置key和channel | 16 | // 需在 AppScope/resources/rawfile/umconfig.json 配置key和channel |
| 16 | preInit({ | 17 | preInit({ |
| @@ -23,14 +24,23 @@ export class UmengStats { | @@ -23,14 +24,23 @@ export class UmengStats { | ||
| 23 | 24 | ||
| 24 | // 在用户同意隐私政策后再调用此方法 | 25 | // 在用户同意隐私政策后再调用此方法 |
| 25 | static initAfterAgreeProtocol() { | 26 | static initAfterAgreeProtocol() { |
| 27 | + if (UmengStats._inited) { | ||
| 28 | + return | ||
| 29 | + } | ||
| 26 | init(); | 30 | init(); |
| 27 | UmengStats._inited = true | 31 | UmengStats._inited = true |
| 28 | 32 | ||
| 29 | UmengStats.onLogin() | 33 | UmengStats.onLogin() |
| 30 | - UmengStats.event("testHarmony", {"key1": "value1"}) | 34 | + // UmengStats.event("testHarmony", {"key1": "value1"}) |
| 35 | + | ||
| 36 | + EmitterUtils.receiveEvent(EmitterEventId.LOGIN_SUCCESS, () => { | ||
| 37 | + UmengStats.onLogin() | ||
| 38 | + }) | ||
| 39 | + EmitterUtils.receiveEvent(EmitterEventId.FORCE_USER_LOGIN_OUT, () => { | ||
| 40 | + UmengStats.onLogout() | ||
| 41 | + }) | ||
| 31 | } | 42 | } |
| 32 | 43 | ||
| 33 | - // TODO: 登录成功调用 | ||
| 34 | static onLogin() { | 44 | static onLogin() { |
| 35 | AccountManagerUtils.isLogin().then((login) => { | 45 | AccountManagerUtils.isLogin().then((login) => { |
| 36 | if (!login) { return } | 46 | if (!login) { return } |
| @@ -41,7 +51,7 @@ export class UmengStats { | @@ -41,7 +51,7 @@ export class UmengStats { | ||
| 41 | } | 51 | } |
| 42 | }) | 52 | }) |
| 43 | } | 53 | } |
| 44 | - // TODO: 退出登录调用 | 54 | + |
| 45 | static onLogout() { | 55 | static onLogout() { |
| 46 | onProfileSignOff() | 56 | onProfileSignOff() |
| 47 | } | 57 | } |
| 1 | +import AbilityStage from '@ohos.app.ability.AbilityStage'; | ||
| 2 | +import AbilityConstant from '@ohos.app.ability.AbilityConstant'; | ||
| 3 | +import Want from '@ohos.app.ability.Want'; | ||
| 4 | +import { Configuration } from '@ohos.app.ability.Configuration'; | ||
| 5 | +import { SPHelper, UmengStats } from 'wdKit'; | ||
| 6 | + | ||
| 7 | +export default class MyAbilityStage extends AbilityStage { | ||
| 8 | + | ||
| 9 | + onCreate() { | ||
| 10 | + | ||
| 11 | + UmengStats.preInit(this.context) | ||
| 12 | + | ||
| 13 | + // KV存储 | ||
| 14 | + SPHelper.init(this.context); | ||
| 15 | + | ||
| 16 | + if (SPHelper.default.getSync('isPrivacy', true)) { | ||
| 17 | + // 同意隐私协议,这里直接初始化。TODO 耗时梳理 | ||
| 18 | + UmengStats.initAfterAgreeProtocol() | ||
| 19 | + } | ||
| 20 | + } | ||
| 21 | + | ||
| 22 | + onAcceptWant(want: Want): string { | ||
| 23 | + return "" | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | + onConfigurationUpdate(newConfig: Configuration): void { | ||
| 27 | + | ||
| 28 | + } | ||
| 29 | + onMemoryLevel(level: AbilityConstant.MemoryLevel): void { | ||
| 30 | + | ||
| 31 | + } | ||
| 32 | + | ||
| 33 | + onDestroy(): void { | ||
| 34 | + | ||
| 35 | + } | ||
| 36 | + | ||
| 37 | +} |
| @@ -193,7 +193,7 @@ export class StartupManager { | @@ -193,7 +193,7 @@ export class StartupManager { | ||
| 193 | } | 193 | } |
| 194 | 194 | ||
| 195 | private preInitUmentStat() { | 195 | private preInitUmentStat() { |
| 196 | - UmengStats.preInit(this.context!) | 196 | + // UmengStats.preInit(this.context!) |
| 197 | } | 197 | } |
| 198 | private initUmengStat() { | 198 | private initUmengStat() { |
| 199 | Logger.debug(TAG, "App 友盟统计 初始化") | 199 | Logger.debug(TAG, "App 友盟统计 初始化") |
| @@ -4,6 +4,7 @@ | @@ -4,6 +4,7 @@ | ||
| 4 | "type": "entry", | 4 | "type": "entry", |
| 5 | "description": "$string:module_desc", | 5 | "description": "$string:module_desc", |
| 6 | "mainElement": "EntryAbility", | 6 | "mainElement": "EntryAbility", |
| 7 | + "srcEntry": "./ets/entryability/EntryAbilityStage.ets", | ||
| 7 | "deviceTypes": [ | 8 | "deviceTypes": [ |
| 8 | "phone", | 9 | "phone", |
| 9 | // "tablet", | 10 | // "tablet", |
-
Please register or login to post a comment