一、启动应用内的UIAbility
场景:应用内存在两个UIAbility:EntryAbility、FuncAbility,在EntryAbility页面启动FuncAbility下页面。
知识点:创建UIAbility
、startAbility()
、terminateSelf()
、killAllProcesses()
1.新建FuncAbility
FuncAbility.ets
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; import window from '@ohos.window'; export default class FuncAbility extends UIAbility { onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { // 接收调用方UIAbility传过来的参数 let info = want.parameters?.info as string console.info(info) } onWindowStageCreate(windowStage: window.WindowStage): void { let url = 'pages/PageFuncA' windowStage.loadContent(url, (err, data) => { }) } } |
module.json5
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 |
{ "module": { "name": "entry", "type": "entry", "description": "$string:module_desc", "mainElement": "EntryAbility", "deviceTypes": [ "phone", "tablet", "2in1" ], "deliveryWithInstall": true, "installationFree": false, "pages": "$profile:main_pages", "abilities": [ { "name": "EntryAbility", "srcEntry": "./ets/entryability/EntryAbility.ets", "description": "$string:EntryAbility_desc", "icon": "$media:layered_image", "label": "$string:EntryAbility_label", "startWindowIcon": "$media:startIcon", "startWindowBackground": "$color:start_window_background", "exported": true, "skills": [ { "entities": [ "entity.system.home" ], "actions": [ "action.system.home" ] } ] }, { "name": "FuncAbility", "srcEntry": "./ets/funcability/FuncAbility.ets", "description": "$string:FuncAbility_desc", "icon": "$media:layered_image", "label": "$string:FuncAbility_label", "startWindowIcon": "$media:startIcon", "startWindowBackground": "$color:start_window_background", "exported": true, "skills": [ { "entities": [ "entity.system.home" ], "actions": [ "action.system.home" ] } ] } ], "extensionAbilities": [ { "name": "EntryBackupAbility", "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets", "type": "backup", "exported": false, "metadata": [ { "name": "ohos.extension.backup", "resource": "$profile:backup_config" } ], } ] } } |
string.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
{ "string": [ { "name": "module_desc", "value": "module description" }, { "name": "EntryAbility_desc", "value": "description" }, { "name": "EntryAbility_label", "value": "label" }, { "name": "FuncAbility_desc", "value": "func description" }, { "name": "FuncAbility_label", "value": "func label" } ] } |
2.创建页面PageFuncA
PageFuncA.ets
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 |
import { common } from '@kit.AbilityKit' @Entry @Component struct PageFuncA { private context = getContext(this) as common.UIAbilityContext build() { Column() { Text('FuncAbility PageFuncA') .margin({ top: 20 }) Button('停止当前UIAbility') .margin({ top: 20 }) .onClick(() => { this.context.terminateSelf((err) => { }) }) Button('关闭应用所有UIAbility') .margin({ top: 20 }) .onClick(() => { this.context.getApplicationContext().killAllProcesses() }) } .width('100%') .height('100%') .padding(20) } } |
3.启动FuncAbility
在EntryAbility下的Index.ets中启动FuncAbility下的PageFuncA.ets页面。
Index.ets
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 |
import { common, Want } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; @Entry @Component struct Index { private context = getContext(this) as common.UIAbilityContext build() { Column() { Text('EntryAbility Index') Button('打开FuncAbility PageFuncA页面') .align(Alignment.Center) .margin({ top: 20 }) .onClick(() => { let want: Want = { deviceId: '', bundleName: this.context.abilityInfo.bundleName, moduleName: this.context.abilityInfo.moduleName, abilityName: 'FuncAbility', parameters: { info: '来自EntryAbility Index页面' } } this.context.startAbility(want).then(() => { }).catch((error: BusinessError) => { }) }) } .height('100%') .width('100%') .padding(20) } } |
二、启动应用内的UIAbility并获取返回结果
同上,在上面代码基础上修改,使用startAbilityForResult()、terminateSelfWithResult()
CommonConstants.ets
1 2 3 4 5 6 |
export default class CommonConstants { static readonly RESULT_CODE: number = 1001 static readonly DOMAIN_NUMBER: number = 0xFF00 static readonly TAG_INDEX: string = '[Index]' static readonly TAG_PAGE_FUNC_A: string = '[PageFuncA]' } |
Index.ets
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 |
import { common, Want } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; import CommonConstants from '../common/constants/CommonConstants'; import { promptAction } from '@kit.ArkUI'; @Entry @Component struct Index { private context = getContext(this) as common.UIAbilityContext build() { Column() { Text('EntryAbility Index') Button('打开FuncAbility PageFuncA页面') .align(Alignment.Center) .margin({ top: 20 }) .onClick(() => { let want: Want = { deviceId: '', bundleName: this.context.abilityInfo.bundleName, moduleName: this.context.abilityInfo.moduleName, abilityName: 'FuncAbility', parameters: { info: '来自EntryAbility Index页面' } } this.context.startAbilityForResult(want).then((data) => { if (data.resultCode === CommonConstants.RESULT_CODE) { let info = data.want?.parameters?.info hilog.info(CommonConstants.DOMAIN_NUMBER, CommonConstants.TAG_INDEX, JSON.stringify(info) ?? '') if (info !== null) { promptAction.showToast({ message: JSON.stringify(info) }) } } hilog.info(CommonConstants.DOMAIN_NUMBER, CommonConstants.TAG_INDEX, JSON.stringify(data.resultCode) ?? '') }).catch((error: BusinessError) => { hilog.error(CommonConstants.DOMAIN_NUMBER, CommonConstants.TAG_INDEX, `Failed to start ability for result. Code is ${error.code}, message is ${error.message}`) }) }) } .height('100%') .width('100%') .padding(20) } } |
PageFuncA.ets
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 |
import { common } from '@kit.AbilityKit' import CommonConstants from '../common/constants/CommonConstants' import { hilog } from '@kit.PerformanceAnalysisKit' @Entry @Component struct PageFuncA { private context = getContext(this) as common.UIAbilityContext build() { Column() { Text('FuncAbility PageFuncA') .margin({ top: 20 }) Button('停止当前UIAbility') .margin({ top: 20 }) .onClick(() => { let abilityResult: common.AbilityResult = { resultCode: CommonConstants.RESULT_CODE, want: { bundleName: this.context.abilityInfo.bundleName, moduleName: this.context.abilityInfo.moduleName, abilityName: this.context.abilityInfo.name, parameters: { info: '来自FuncAbility PageFuncA页面' } } } this.context.terminateSelfWithResult(abilityResult, (err) => { if (err.code) { hilog.error(CommonConstants.DOMAIN_NUMBER, CommonConstants.TAG_PAGE_FUNC_A, `Failed to terminate self with result. Code is ${err.code}, message is ${err.message}`) return } }) }) Button('关闭应用所有UIAbility') .margin({ top: 20 }) .onClick(() => { this.context.getApplicationContext().killAllProcesses() }) } .width('100%') .height('100%') .padding(20) } } |
三、启动UIAbility的指定页面
概述
概述
UIAbility的启动分为两种情况:UIAbility冷启动和UIAbility热启动。
- UIAbility热启动:指的是UIAbility实例已经启动并在前台运行过,由于某些原因切换到后台,再次启动该UIAbility实例,这种情况下可以快速恢复UIAbility实例的状态。
- UIAbility冷启动:指的是UIAbility实例处于完全关闭状态下被启动,这需要完整地加载和初始化UIAbility实例的代码、资源等。
目标UIAbility冷启动
Index.ets
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 |
import { common, Want } from '@kit.AbilityKit'; import { BusinessError } from '@kit.BasicServicesKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; import CommonConstants from '../common/constants/CommonConstants'; import { promptAction } from '@kit.ArkUI'; @Entry @Component struct Index { private context = getContext(this) as common.UIAbilityContext build() { Column() { Text('EntryAbility Index') Button('打开FuncAbility PageFuncA页面') .align(Alignment.Center) .margin({ top: 20 }) .onClick(() => { let want: Want = { deviceId: '', bundleName: this.context.abilityInfo.bundleName, moduleName: this.context.abilityInfo.moduleName, abilityName: 'FuncAbility', parameters: { info: '来自EntryAbility Index页面' } } this.context.startAbilityForResult(want).then((data) => { if (data.resultCode === CommonConstants.RESULT_CODE) { let info = data.want?.parameters?.info hilog.info(CommonConstants.DOMAIN_NUMBER, CommonConstants.TAG_INDEX, JSON.stringify(info) ?? '') if (info !== null) { promptAction.showToast({ message: JSON.stringify(info) }) } } hilog.info(CommonConstants.DOMAIN_NUMBER, CommonConstants.TAG_INDEX, JSON.stringify(data.resultCode) ?? '') }).catch((error: BusinessError) => { hilog.error(CommonConstants.DOMAIN_NUMBER, CommonConstants.TAG_INDEX, `Failed to start ability for result. Code is ${error.code}, message is ${error.message}`) }) }) Button('目标UIAbility冷启动、热启动') .align(Alignment.Center) .margin({ top: 20 }) .onClick(() => { let want: Want = { deviceId: '', bundleName: this.context.abilityInfo.bundleName, moduleName: this.context.abilityInfo.moduleName, abilityName: 'FuncAbility', parameters: { info: '来自EntryAbility Index页面', router: 'funcA' } } this.context.startAbility(want).then(() => { hilog.info(CommonConstants.DOMAIN_NUMBER, CommonConstants.TAG_INDEX, 'Succeeded in starting ability.') }).catch((err: BusinessError) => { hilog.error(CommonConstants.DOMAIN_NUMBER, CommonConstants.TAG_INDEX, `Failed to start ability. Code is ${err.code}, message is ${err.message}`) }) }) } .height('100%') .width('100%') .padding(20) } } |
FuncAbility.ets
1 |
<strong>import </strong>{ AbilityConstant, UIAbility, Want } <strong>from </strong>'@kit.AbilityKit';<br><strong>import </strong>window <strong>from </strong>'@ohos.window';<br><strong>import </strong>{ UIContext } <strong>from </strong>'@kit.ArkUI';<br><br><strong>export default class </strong>FuncAbility <strong>extends </strong>UIAbility {<br> want: Want | undefined = <strong>undefined<br></strong><strong> </strong>uiContext: UIContext | undefined = <strong>undefined<br></strong><strong><br></strong><strong> </strong>onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {<br> // 接收调用方UIAbility传过来的参数<br> <strong>this</strong>.want = want<br> <strong>let </strong>info = want.parameters?.info <strong>as </strong>string<br> console.info(info)<br> }<br><br> onWindowStageCreate(windowStage: window.WindowStage): void {<br> <strong>let </strong>url = 'pages/PageFuncA'<br><br> <strong>if </strong>(<strong>this</strong>.want?.parameters?.router && <strong>this</strong>.want.parameters.router === 'funcA') {<br> url = 'pages/PageCold'<br> }<br> windowStage.loadContent(url, (err, data) => {<br> })<br> }<br>} |
目标UIAbility热启动
FuncAbility.ets
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 |
import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; import window from '@ohos.window'; import { Router, UIContext } from '@kit.ArkUI'; import { BusinessError } from '@kit.BasicServicesKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; import CommonConstants from '../common/constants/CommonConstants'; export default class FuncAbility extends UIAbility { want: Want | undefined = undefined uiContext: UIContext | undefined = undefined onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { // 接收调用方UIAbility传过来的参数 this.want = want let info = want.parameters?.info as string console.info(info) } onWindowStageCreate(windowStage: window.WindowStage): void { let url = 'pages/PageFuncA' if (this.want?.parameters?.router && this.want.parameters.router === 'funcA') { url = 'pages/PageCold' } windowStage.loadContent(url, (err, data) => { if (err.code) { return } windowStage.getMainWindow((err, data) => { if (err.code) { return } this.uiContext = data.getUIContext() }) }) } onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): void { if (want.parameters?.router && want.parameters.router === 'funcA') { let url = 'pages/PageHot' if (this.uiContext) { let router: Router = this.uiContext.getRouter() router.pushUrl({ url }).catch((err: BusinessError) => { }) } } } } |