From cb5a2545da62a8445c35c1b1d23582788e7a2786 Mon Sep 17 00:00:00 2001 From: WX1379483 Date: Tue, 20 May 2025 16:17:37 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E5=BA=9F=E5=BC=83API=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0=E4=BB=A3=E7=A0=81=E6=A0=87=E8=AF=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/ets/entryability/EntryAbility.ets | 8 +++-- .../InsightIntentExecutorImpl.ets | 11 +++--- entry/src/main/ets/pages/Index.ets | 16 ++++----- .../main/ets/utils/InsightIntentSharer.ets | 36 +++++++++++-------- 4 files changed, 42 insertions(+), 29 deletions(-) diff --git a/entry/src/main/ets/entryability/EntryAbility.ets b/entry/src/main/ets/entryability/EntryAbility.ets index a0e87b0..eda1bc0 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ets +++ b/entry/src/main/ets/entryability/EntryAbility.ets @@ -1,5 +1,7 @@ import { Logger } from '../utils/Logger'; - +/** + * 最佳实践:意图框架习惯推荐场景开发实践 + */ import { UIAbility } from '@kit.AbilityKit'; import type { AbilityConstant, Want } from '@kit.AbilityKit'; import type { window } from '@kit.ArkUI'; @@ -9,14 +11,14 @@ const TAG: string = 'EntryAbility'; export default class EntryAbility extends UIAbility { private result: string = ''; - + // [Start on_create] onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { Logger.info(TAG, 'onCreate'); if (want.parameters?.['result']) { this.result = want.parameters?.['result'] as string; } } - + // [End on_create] onDestroy(): void { Logger.info(TAG, 'onDestroy'); } diff --git a/entry/src/main/ets/entryability/InsightIntentExecutorImpl.ets b/entry/src/main/ets/entryability/InsightIntentExecutorImpl.ets index 7ac6d1b..5de8adf 100644 --- a/entry/src/main/ets/entryability/InsightIntentExecutorImpl.ets +++ b/entry/src/main/ets/entryability/InsightIntentExecutorImpl.ets @@ -3,7 +3,9 @@ import { Logger } from '../utils/Logger'; import { insightIntent, InsightIntentExecutor } from '@kit.AbilityKit'; import { window } from '@kit.ArkUI'; import type { BusinessError } from '@kit.BasicServicesKit'; - +/** + * 最佳实践:意图框架习惯推荐场景开发实践 + */ const TAG: string = 'InsightIntentExecutorImpl'; /** @@ -12,7 +14,7 @@ const TAG: string = 'InsightIntentExecutorImpl'; export default class InsightIntentExecutorImpl extends InsightIntentExecutor { private static readonly PLAY_MUSIC = 'PlayMusic'; private static readonly PLAY_MUSIC_LIST = 'PlayMusicList'; - + // [Start fore_mode] /** * Override the method to execute the intent in the foreground UIAbility. * @@ -41,7 +43,8 @@ export default class InsightIntentExecutorImpl extends InsightIntentExecutor { } } as insightIntent.ExecuteResult) } - + // [End fore_mode] + // [Start play_music] /** * Implement the music playback function. * @@ -78,7 +81,7 @@ export default class InsightIntentExecutorImpl extends InsightIntentExecutor { }); }) } - + // [End play_music] /** * Implement the playlist playback function. * diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index 6ab61c6..bcfe2e9 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -56,7 +56,7 @@ struct Index { .backgroundColor('#0ddffb') .onClick(async () => { Logger.info(TAG, 'refreshShareIntentMessage onclick'); - let str: string = await InsightIntentSharer.refreshShareIntentMessage(getContext()); + let str: string = await InsightIntentSharer.refreshShareIntentMessage(this.getUIContext().getHostContext()!); this.input = str; }) @@ -76,7 +76,7 @@ struct Index { .backgroundColor('#0ddffb') .onClick(async () => { Logger.info(TAG, 'shareIntent onclick'); - this.result = await InsightIntentSharer.shareIntent(getContext(), this.input); + this.result = await InsightIntentSharer.shareIntent(this.getUIContext().getHostContext()!, this.input); }) } .width('100%') @@ -103,7 +103,7 @@ struct Index { .backgroundColor('#0ddffb') .onClick(async () => { Logger.info(TAG, 'refreshDeleteIntentMessage onclick'); - let str: string = await InsightIntentSharer.refreshDeleteIntentMessage(getContext()); + let str: string = await InsightIntentSharer.refreshDeleteIntentMessage(this.getUIContext().getHostContext()!); this.input = str; }) @@ -123,7 +123,7 @@ struct Index { .backgroundColor('#0ddffb') .onClick(async () => { Logger.info(TAG, 'deleteIntent onclick'); - this.result = await InsightIntentSharer.deleteIntent(getContext(), this.input); + this.result = await InsightIntentSharer.deleteIntent(this.getUIContext().getHostContext()!, this.input); }) } .width('100%') @@ -150,7 +150,7 @@ struct Index { .backgroundColor('#0ddffb') .onClick(async () => { Logger.info(TAG, 'refreshDeleteEntityMessage onclick'); - let str: string = await InsightIntentSharer.refreshDeleteEntityMessage(getContext()); + let str: string = await InsightIntentSharer.refreshDeleteEntityMessage(this.getUIContext().getHostContext()!); this.input = str; }) @@ -170,7 +170,7 @@ struct Index { .backgroundColor('#0ddffb') .onClick(async () => { Logger.info(TAG, 'deleteEntity onclick'); - this.result = await InsightIntentSharer.deleteEntity(getContext(), this.input); + this.result = await InsightIntentSharer.deleteEntity(this.getUIContext().getHostContext()!, this.input); }) } .width('100%') @@ -197,7 +197,7 @@ struct Index { .backgroundColor('#0ddffb') .onClick(async () => { Logger.info(TAG, 'getSid onclick'); - this.result = await InsightIntentSharer.getSid(getContext()); + this.result = await InsightIntentSharer.getSid(this.getUIContext().getHostContext()!); }) Button() { Text('getSidRenew') @@ -215,7 +215,7 @@ struct Index { .backgroundColor('#0ddffb') .onClick(async () => { Logger.info(TAG, 'getSidRenew onclick'); - this.result = await InsightIntentSharer.getSidRenew(getContext()); + this.result = await InsightIntentSharer.getSidRenew(this.getUIContext().getHostContext()!); }) } .width('100%') diff --git a/entry/src/main/ets/utils/InsightIntentSharer.ets b/entry/src/main/ets/utils/InsightIntentSharer.ets index a43403d..55023b5 100644 --- a/entry/src/main/ets/utils/InsightIntentSharer.ets +++ b/entry/src/main/ets/utils/InsightIntentSharer.ets @@ -1,6 +1,8 @@ import { Logger } from './Logger'; import { FileReader } from './FileReader'; - +/** + * 最佳实践:意图框架习惯推荐场景开发实践 + */ import type { BusinessError } from '@kit.BasicServicesKit'; import type { Context } from '@kit.AbilityKit'; import { insightIntent } from '@kit.IntentsKit'; @@ -27,23 +29,29 @@ export class InsightIntentSharer { return 'invalid share intent message'; } } - + // [Start share_Intent] static async shareIntent(context: Context, input: string): Promise { Logger.debug(TAG, 'shareIntent'); - let insightIntents: insightIntent.InsightIntent[] = JSON.parse(input); - if (!insightIntents || insightIntents.length === 0){ - Logger.error(TAG, 'shareIntent: json invalid.'); - return 'shareIntent: json invalid.'; + let insightIntents: insightIntent.InsightIntent[]; + try { + let insightIntents: insightIntent.InsightIntent[] = JSON.parse(input); + if (!insightIntents || insightIntents.length === 0) { + Logger.error(TAG, 'shareIntent: json invalid.'); + return 'shareIntent: json invalid.'; + } + return await insightIntent.shareIntent(context, insightIntents).then(() => { + Logger.info(TAG, 'shareIntent success'); + return 'share intent success'; + }, (err: BusinessError) => { + Logger.error(TAG, `shareIntent error message: ${JSON.stringify(err)}`); + return `shareIntent error message: ${JSON.stringify(err)}`; + }); + }catch (err) { + Logger.error(TAG, 'shareIntent fail', err); } - return await insightIntent.shareIntent(context, insightIntents).then(() => { - Logger.info(TAG, 'shareIntent success'); - return 'share intent success'; - }, (err: BusinessError) => { - Logger.error(TAG, `shareIntent error message: ${JSON.stringify(err)}`); - return `shareIntent error message: ${JSON.stringify(err)}`; - }); + return Promise.reject('shareIntent fail'); } - + // [End share_Intent] static async refreshDeleteIntentMessage(context: Context): Promise { Logger.debug(TAG, 'refreshDeleteIntentMessage'); try { -- Gitee