diff --git a/entry/src/main/ets/entryability/EntryAbility.ets b/entry/src/main/ets/entryability/EntryAbility.ets index a0e87b0b93e0bcde7b10c9f11dc61afbfef8b3a1..eda1bc00e4bdbc271ed0b226773fd7773cfadc23 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 7ac6d1bad676bc503ae5d9292d61514c2c50c076..5de8adff6c71cf8afc4182a425e83f1e6ee6ff4e 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 6ab61c60a33d5ad6b8b9da516a52514b13f27982..bcfe2e9b50489002f189dfbd84343c1fee09f436 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 a43403dac74e509fec8037d991f40f67ba0e16ea..55023b5c651d40d2b3c7964c469ee6ee08a097a3 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 {