• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.app.ability.ChildProcessOptions (子进程启动选项)
2<!--Kit: Ability Kit-->
3<!--Subsystem: Ability-->
4<!--Owner: @SKY2001-->
5<!--Designer: @jsjzju-->
6<!--Tester: @lixueqing513-->
7<!--Adviser: @huipeizi-->
8
9<!--Kit: Ability Kit-->
10<!--Subsystem: Ability-->
11<!--Owner: @SKY2001-->
12<!--Designer: @jsjzju-->
13<!--Tester: @lixueqing513-->
14
15子进程的启动配置选项。通过[childProcessManager](js-apis-app-ability-childProcessManager.md)启动子进程时,可以通过ChildProcessOptions配置子进程启动选项。
16
17> **说明:**
18>
19> 本模块首批接口从API version 12开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
20>
21> 本模块接口仅可在Stage模型下使用。
22
23## 导入模块
24
25```ts
26import { ChildProcessOptions } from '@kit.AbilityKit';
27```
28
29## ChildProcessOptions
30
31**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
32
33| 名称        | 类型      | 只读 | 可选 | 说明                                                               |
34| ----------- | --------- | ---- | ----- | ----------------------------------------------- |
35| isolationMode | boolean | 否 | 是 | 控制子进程的沙箱隔离级别及网络访问权限。true表示子进程运行在独立沙箱环境中,且无法访问网络;false表示子进程与主进程共享沙箱和网络环境。默认为false。|
36
37**示例:**
38
39子进程部分:
40
41```ts
42// 在entry模块的src/main/ets/process下创建DemoProcess.ets子进程类:
43// entry/src/main/ets/process/DemoProcess.ets
44import { ChildProcess, ChildProcessArgs } from '@kit.AbilityKit';
45
46export default class DemoProcess extends ChildProcess {
47  onStart(args?: ChildProcessArgs) {
48    let entryParams = args?.entryParams;
49    let fd = args?.fds?.key1;
50    //子进程代码逻辑
51  }
52}
53```
54
55主进程部分:
56
57```ts
58// 使用childProcessManager.startArkChildProcess方法启动子进程:
59// entry/src/main/ets/pages/Index.ets
60import { ChildProcessArgs, ChildProcessOptions, childProcessManager } from '@kit.AbilityKit';
61import { BusinessError } from '@kit.BasicServicesKit';
62import DemoProcess from '../process/DemoProcess';
63
64@Entry
65@Component
66struct Index {
67  build() {
68    Row() {
69      Column() {
70        Text('Click')
71          .fontSize(30)
72          .fontWeight(FontWeight.Bold)
73          .onClick(() => {
74            try {
75              DemoProcess.toString(); // 这里调用DemoProcess类的任意方法,防止没有引用到而被构建工具优化掉
76              let options: ChildProcessOptions = {
77                isolationMode: false
78              };
79              let args: ChildProcessArgs = {
80                entryParams: "testParam",
81              };
82              childProcessManager.startArkChildProcess("entry/ets/process/DemoProcess.ets", args, options)
83                .then((pid) => {
84                  console.info(`startChildProcess success, pid: ${pid}`);
85                })
86                .catch((err: BusinessError) => {
87                  console.error(`startChildProcess business error, errorCode: ${err.code}, errorMsg:${err.message}`);
88                });
89            } catch (err) {
90              console.error(`startChildProcess error, errorCode: ${err.code}, errorMsg:${err.message}`);
91            }
92          });
93      }
94      .width('100%')
95    }
96    .height('100%')
97  }
98}
99```