diff --git a/zh-cn/application-dev/application-models/ability-startup-with-explicit-want.md b/zh-cn/application-dev/application-models/ability-startup-with-explicit-want.md index 2500d05a5b26e611b1490996eadfb5ee8fdf6f4f..fd067cba00d102a9e2dff8adac223fa430f25957 100644 --- a/zh-cn/application-dev/application-models/ability-startup-with-explicit-want.md +++ b/zh-cn/application-dev/application-models/ability-startup-with-explicit-want.md @@ -1,73 +1,5 @@ # 使用显式Want启动Ability +在应用使用场景中,当用户在应用内点击某个按钮时,经常需要拉起指定UIAbility组件来完成某些特定任务。在启动UIAbility时,指定了abilityName和bundleName参数,可以使用显式Want方式启动UIAbility。显式Want的使用 -在应用使用场景中,当用户点击某个按钮时,应用经常需要拉起指定UIAbility组件来完成某些特定任务。下面介绍如何通过显式Want拉起应用内一个指定UIAbility组件。 - - -## 开发步骤 - -1. Stage模型工程内,创建一个Ability(此示例内命名为callerAbility)与相应Page(此示例中名为Index.ets),并在callerAbility.ts文件内的onWindowStageCreate函数内通过windowStage.loadContent()方法将两者绑定。 - - ```ts - // ... - // callerAbility.ts - onWindowStageCreate(windowStage) { - // Main window is created, set main page for this ability - console.info('[Demo] EntryAbility onWindowStageCreate') - // Bind callerAbility with a paged named Index - windowStage.loadContent('pages/Index') - } - // ... - ``` - -2. 同上方法再创建一个Ability,此示例内命名为“calleeAbility”。 - -3. 在callerAbility的“Index.ets”页面内新增一个按钮。 - - ```ts - // ... - build() { - Row() { - Column() { - Text('hello') - .fontSize(50) - .fontWeight(FontWeight.Bold) - // A new button with will call explicitStartAbility() when clicked. - Button("CLICKME") - .onClick(this.explicitStartAbility) // explicitStartAbility见下面示例代码 - // ... - } - .width('100%') - } - .height('100%') - } - // ... - ``` - -4. 补充相对应的onClick方法,并使用**显式Want**在方法内启动calleeAbility。bundleName字段可在工程AppScope>app.json5文件内获取;abilityName可在对应模块内的“yourModuleName > src > main > module.json5”文件查看。 - - ```ts - import common from '@ohos.app.ability.common'; - - // ... - async explicitStartAbility() { - try { - // Explicit want with abilityName specified. - let want = { - deviceId: "", - bundleName: "com.example.myapplication", - abilityName: "calleeAbility" - }; - let context = getContext(this) as common.UIAbilityContext; - await context.startAbility(want); - console.info(`explicit start ability succeed`); - } catch (error) { - console.info(`explicit start ability failed with ${error.code}`); - } - } - // ... - ``` - -5. 至此,当您点击CLICKME按钮时,应看到页面的跳转。 - - startAbilityWtExplicitWant +针对应用的特定任务,用户需要通过点击应用内的按钮来启动指定的UIAbility组件。在启动UIAbility时,需要提供abilityName和bundleName参数,并使用显式Want方式来启动。关于如何使用显式Want方式启动应用内的UIAbility,请参见[启动应用内的UIAbility](uiability-intra-device-interaction.md#启动应用内的uiability)。