1# 元能力子系统 2 3## 简介 4 5**元能力子系统**实现对Ability的运行及生命周期进行统一的调度和管理,应用进程能够支撑多个Ability,Ability具有跨应用进程间和同一进程内调用的能力。Ability管理服务统一调度和管理应用中各Ability,并对Ability的生命周期变更进行管理。 6 7 8 9**元能力子系统架构图说明:** 10 11- **Ability Kit**为Ability的运行提供基础的运行环境支撑。Ability是系统调度应用的最小单元,是能够完成一个独立功能的组件,一个应用可以包含一个或多个Ability。Ability分为FA(Feature Ability)和PA(Particle Ability)两种类,其中FA支持Page Ability,PA支持Service Ability和Data Ability。 12 13- **Ability管理服务(AbilityManagerService)**:用于协调各Ability运行关系、及对生命周期进行调度的系统服务。 14 - Ability栈管理模块(AbilityStackManager)负责维护各个Ability之间跳转的先后关系。 15 - 连接管理模块(AbilityConnectManager)是Ability管理服务对Service类型Ability连接管理的模块。 16 - 数据管理模块(DataAbilityManager)是Ability管理服务对Data类型Ability管理的模块。 17 - App管理服务调度模块(AppScheduler)提供Ability管理服务对用户程序管理服务进行调度管理的能力。 18 - Ability调度模块(AbilityScheduler)提供对Ability进行调度管理的能力。 19 - 生命周期调度模块(LifecycleDeal)是Ability管理服务对Ability的生命周期事件进行管理调度的模块。 20 21**Ability生命周期介绍**(Ability Life Cycle)是Ability被调度到INACTIVE、ACTIVE、BACKGROUND等各个状态的统称(主要涉及PageAbility类型和ServiceAbility类型的Ability)。 22 23 - **PageAbility类型的Ability生命周期流转如下图所示** 24 25 26 27 28 29 - **ServiceAbility类型的Ability生命周期流转如下图所示** 30 31 32 33**Ability生命周期状态说明:** 34 35 - **UNINITIALIZED**:未初始状态,为临时状态,Ability被创建后会由UNINITIALIZED状态进入INITIAL状态。 36 37 - **INITIAL**:初始化状态,也表示停止状态,表示当前Ability未运行,Ability被启动后由INITIAL态进入INACTIVE状态。 38 39 - **INACTIVE**:未激活状态,表示当前窗口已显示但是无焦点状态,由于Window暂未支持焦点的概念,当前状态与ACTIVE一致。 40 41 - **ACTIVE**:前台激活状态,表示当前窗口已显示,并获取焦点,Ability在退到后台之前先由ACTIVE状态进入INACTIVE状态。 42 43 - **BACKGROUND**: 后台状态,表示当前Ability退到后台,Ability在被销毁后由BACKGROUND状态进入INITIAL状态,或者重新被激活后由BACKGROUND状态进入ACTIVE状态。 44 45**PageAbility类型Ability生命周期回调如下图所示:** 46 47 48 49 50 51**ServiceAbility类型Ability生命周期回调如下图所示:** 52 53 54 55 56 57## 目录 58 59``` 60foundation/ 61└──foundation/aafwk/standard 62 ├── frameworks 63 │ └── kits 64 │ └── ability # AbilityKit实现的核心代码 65 ├── interfaces 66 │ └── innerkits 67 │ └── want # Ability之间交互的信息载体的对外接口 68 └── services 69 ├── abilitymgr # Ability管理服务框架代码 70 ├── common # 日志组件目录 71 ├── test # 测试目录 72 └── tools # aa命令代码目录 73``` 74 75## 使用说明 76### 启动Abiltiy 77启动新的ability(callback形式) 78 79* startAbility参数描述 80 81| 名称 | 读写属性 | 类型 | 必填 | 描述 | 82| --------- | -------- | --------------------- | ---- | ------------------- | 83| parameter | 读写 | StartAbilityParameter | 是 | 表示被启动的Ability | 84| callback | 只读 | AsyncCallback | 是 | 被指定的回调方法 | 85 86- StartAbilityParameter类型说明 87 88| 名称 | 读写属性 | 类型 | 必填 | 描述 | 89| ------------------- | -------- | ------ | ---- | ---------------------------------- | 90| want | 读写 | want | 是 | 表示需要包含有关目标启动能力的信息 | 91| abilityStartSetting | 只读 | string | 否 | 指示启动能力中使用的特殊启动设置 | 92 93- want类型说明 94 95| 名称 | 读写属性 | 类型 | 必填 | 描述 | 96| ----------- | -------- | -------------------- | ---- | -------------------------- | 97| deviceId | 读写 | string | 否 | 设备id | 98| bundleName | 读写 | string | 否 | Bundle名 | 99| abilityName | 读写 | string | 否 | Ability 名 | 100| uri | 读写 | string | 否 | 请求中URI的描述 | 101| type | 读写 | string | 否 | Want中类型的说明 | 102| flags | 读写 | number | 否 | Want中标志的选项,必填 | 103| action | 读写 | string | 否 | Want中对操作的描述 | 104| parameters | 读写 | {[key: string]: any} | 否 | Want中WantParams对象的描述 | 105| entities | 读写 | string | 否 | 对象中实体的描述 | 106 107* 返回值 108 109 void 110 111* 示例 112 113```javascript 114import featureAbility from '@ohos.ability.featureAbility' 115function addSlotsCallBack(err) { 116 console.info("==========================>startAbility=======================>"); 117} 118featureAbility.startAbility( 119 { 120 want: 121 { 122 action: "", 123 entities: [""], 124 type: "", 125 options: { 126 // indicates the grant to perform read operations on the URI 127 authReadUriPermission: true, 128 // indicates the grant to perform write operations on the URI 129 authWriteUriPermission: true, 130 // support forward intent result to origin ability 131 abilityForwardResult: true, 132 // used for marking the ability start-up is triggered by continuation 133 abilityContinuation: true, 134 // specifies whether a component does not belong to ohos 135 notOhosComponent: true, 136 // specifies whether an ability is started 137 abilityFormEnabled: true, 138 // indicates the grant for possible persisting on the URI. 139 authPersistableUriPermission: true, 140 // indicates the grant for possible persisting on the URI. 141 authPrefixUriPermission: true, 142 // support distributed scheduling system start up multiple devices 143 abilitySliceMultiDevice: true, 144 // indicates that an ability using the service template is started regardless of whether the 145 // host application has been started. 146 startForegroundAbility: true, 147 // install the specified ability if it's not installed. 148 installOnDemand: true, 149 // return result to origin ability slice 150 abilitySliceForwardResult: true, 151 // install the specified ability with background mode if it's not installed. 152 installWithBackgroundMode: true 153 }, 154 deviceId: "", 155 bundleName: "com.example.startability", 156 abilityName: "com.example.startability.MainAbility", 157 uri: "" 158 }, 159 }, 160 ); 161) 162``` 163启动新的ability(Promise形式) 164* startAbility参数描述 165 166| 名称 | 读写属性 | 类型 | 必填 | 描述 | 167| --------- | -------- | --------------------- | ---- | ------------------- | 168| parameter | 读写 | StartAbilityParameter | 是 | 表示被启动的Ability | 169 170- StartAbilityParameter类型说明 171 172| 名称 | 读写属性 | 类型 | 必填 | 描述 | 173| ------------------- | -------- | ------ | ---- | ---------------------------------- | 174| want | 读写 | want | 是 | 表示需要包含有关目标启动能力的信息 | 175| abilityStartSetting | 只读 | string | 否 | 指示启动能力中使用的特殊启动设置 | 176 177- want类型说明 178 179| 名称 | 读写属性 | 类型 | 必填 | 描述 | 180| ----------- | -------- | -------------------- | ---- | -------------------------- | 181| deviceId | 读写 | string | 否 | 设备id | 182| bundleName | 读写 | string | 否 | Bundle名 | 183| abilityName | 读写 | string | 否 | Ability 名字 | 184| uri | 读写 | string | 否 | 请求中URI的描述 | 185| type | 读写 | string | 否 | Want中类型的说明 | 186| flags | 读写 | number | 否 | Want中标志的选项,必填 | 187| action | 读写 | string | 否 | Want中对操作的描述 | 188| parameters | 读写 | {[key: string]: any} | 否 | Want中WantParams对象的描述 | 189| entities | 读写 | string | 否 | 对象中实体的描述 | 190 191* 返回值 192 193 void 194 195* 示例 196 197```javascript 198import featureAbility from '@ohos.ability.featureAbility' 199featureAbility.startAbility( 200 { 201 want: 202 { 203 action: "action.system.home", 204 entities: ["entity.system.home"], 205 type: "MIMETYPE", 206 options: { 207 // indicates the grant to perform read operations on the URI 208 authReadUriPermission: true, 209 // indicates the grant to perform write operations on the URI 210 authWriteUriPermission: true, 211 // support forward intent result to origin ability 212 abilityForwardResult: true, 213 // used for marking the ability start-up is triggered by continuation 214 abilityContinuation: true, 215 // specifies whether a component does not belong to ohos 216 notOhosComponent: true, 217 // specifies whether an ability is started 218 abilityFormEnabled: true, 219 // indicates the grant for possible persisting on the URI. 220 authPersistableUriPermission: true, 221 // indicates the grant for possible persisting on the URI. 222 authPrefixUriPermission: true, 223 // support distributed scheduling system start up multiple devices 224 abilitySliceMultiDevice: true, 225 // indicates that an ability using the service template is started regardless of whether the 226 // host application has been started. 227 startForegroundAbility: true, 228 // install the specified ability if it's not installed. 229 installOnDemand: true, 230 // return result to origin ability slice 231 abilitySliceForwardResult: true, 232 // install the specified ability with background mode if it's not installed. 233 installWithBackgroundMode: true 234 }, 235 deviceId: "", 236 bundleName: "com.example.startability", 237 abilityName: "com.example.startability.MainAbility", 238 uri: "" 239 }, 240 } 241 ).then((void) => { 242 console.info("==========================>startAbility=======================>"); 243}); 244``` 245 246### 停止Ability 247 248设置page ability返回给被调用方的结果代码和数据,并销毁此page ability(callback形式) 249 250* 参数描述 251 252| 名称 | 读写属性 | 类型 | 必填 | 描述 | 253| -------- | -------- | -------------------- | ---- | ---------------- | 254| callback | 只读 | AsyncCallback\<void> | 是 | 被指定的回调方法 | 255 256* 返回值 257 258 void 259 260* 示例 261 262```javascript 263import featureAbility from '@ohos.ability.featureability'; 264function terminateSelfCallBack(err) { 265 console.info("==========================>terminateSelfCallBack=======================>"); 266} 267featureAbility.terminateSelf() 268``` 269 270设置page ability返回给被调用方的结果代码和数据,并销毁此page ability(Promise形式) 271 272* 参数描述 273 274 Null 275 276* 返回值 277 278* void 279 280* 示例 281 282```javascript 283import featureAbility from '@ohos.ability.featureability'; 284featureAbility.terminateSelf().then((void) => { console.info("==========================>terminateSelfCallBack=======================>"); 285}); 286``` 287 288 289## **aa命令** 290 291**aa help** 292 293| 命令 | 描述 | 294| ------- | ------------------ | 295| aa help | 显示aa命令帮助信息 | 296 297**aa start** 298 299| 命令 | 描述 | 300| --------------------------------------------------------- | ------------------------ | 301| aa start [-d <device>] -a <ability-name> -b <bundle-name> | 启动ability,设备ID 可空 | 302 303``` 304示例: 305aa start -d 12345 -a com.ohos.app.MainAbility -b com.ohos.app 306``` 307 308**aa dump** 309 310| 命令 | 描述 | 311| ---------- | --------------------- | 312| aa dump -a | 打印栈中的Ability信息 | 313 314## 相关仓 315元能力子系统 316 317appexecfwk_standard 318 319**aafwk_standard** 320 321