智能客服
你问我答,随时在线为你解决问题
在应用开发中,启动应用(通常指启动UIAbility组件)是开发者的常见任务。例如,从商城应用跳转到支付应用进行付款,从聊天应用启动视频应用播放视频,或者在应用间跳转时需要指定特定的窗口模式等场景。
通过StartOptions,开发者可以在启动UIAbility时灵活控制其启动行为,包括窗口模式、窗口位置、启动动效、启动页样式、窗口尺寸限制等多个方面,从而满足不同设备形态和用户场景的需求。
在某些场景下,开发者需要指定UIAbility以特定的窗口模式启动。例如:
开发步骤如下:
- import { common, Want, AbilityConstant, StartOptions } from '@kit.AbilityKit';
- import { hilog } from '@kit.PerformanceAnalysisKit';
- import { BusinessError } from '@kit.BasicServicesKit';
-
- const TAG: string = '[StartAbility]';
- const DOMAIN_NUMBER: number = 0xFF00;
-
- @Entry
- @Component
- struct StartWithSpecifiedWindowModeAbility {
-
- build() {
- Row() {
- Column() {
- Button("以分屏模式启动")
- .onClick(() => {
- // context为调用方UIAbility的UIAbilityContext
- let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
-
- let want: Want = {
- deviceId: '', // deviceId为空表示本设备
- bundleName: 'com.example.startoptions',
- abilityName: 'StartWithSpecifiedWindowModeAbility',
- parameters: {
- // 自定义信息
- info: '从StartWithSpecifiedWindowModeAbility启动'
- }
- };
-
- let options: StartOptions = {
- windowMode: AbilityConstant.WindowMode.WINDOW_MODE_SPLIT_SECONDARY // 以分屏模式拉起
- };
-
- context.startAbility(want, options).then(() => {
- hilog.info(DOMAIN_NUMBER, TAG, 'Succeeded in starting ability.');
- }).catch((err: BusinessError) => {
- hilog.error(DOMAIN_NUMBER, TAG, `Failed to start ability. Code is ${err.code}, message is ${err.message}`);
- });
- })
- }
- .width('100%')
- }
- .height('100%')
- }
- }
在某些场景下,开发者需要指定UIAbility窗口标题栏显示哪些窗口模式。例如:
开发步骤如下:
- import { common, Want, StartOptions, bundleManager } from '@kit.AbilityKit';
- import { hilog } from '@kit.PerformanceAnalysisKit';
- import { BusinessError } from '@kit.BasicServicesKit';
-
- const TAG: string = '[StartAbility]';
- const DOMAIN_NUMBER: number = 0xFF00;
-
- @Entry
- @Component
- struct SetWindowDisplayModeAbility {
-
- build() {
- Row() {
- Column() {
- Button("设置窗口显示模式")
- .onClick(() => {
- // context为调用方UIAbility的UIAbilityContext
- let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
-
- let want: Want = {
- deviceId: '', // deviceId为空表示本设备
- bundleName: 'com.example.startoptions',
- abilityName: 'EntryAbility',
- parameters: {
- // 自定义信息
- info: '从EntryAbility启动'
- }
- };
-
- let options: StartOptions = {
- supportWindowModes: [
- bundleManager.SupportWindowMode.FULL_SCREEN, // 支持全屏模式
- ]
- };
-
- context.startAbility(want, options).then(() => {
- hilog.info(DOMAIN_NUMBER, TAG, 'Succeeded in starting ability.');
- }).catch((err: BusinessError) => {
- hilog.error(DOMAIN_NUMBER, TAG, `Failed to start ability. Code is ${err.code}, message is ${err.message}`);
- });
- })
- }
- .width('100%')
- }
- .height('100%')
- }
- }
在某些场景下,开发者需要指定UIAbility在分屏模式下的窗口比例分配。例如,根据应用内容的重要程度,设置不同的分屏比例。
开发步骤如下:
- import { common, Want, StartOptions, AbilityConstant } from '@kit.AbilityKit';
- import { hilog } from '@kit.PerformanceAnalysisKit';
- import { BusinessError } from '@kit.BasicServicesKit';
-
- const TAG: string = '[StartAbility]';
- const DOMAIN_NUMBER: number = 0xFF00;
-
- @Entry
- @Component
- struct SetSplitRatioAbility {
-
- build() {
- Row() {
- Column() {
- Button("以比例分屏启动")
- .onClick(() => {
- // context为调用方UIAbility的UIAbilityContext
- let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
-
- let want: Want = {
- deviceId: '', // deviceId为空表示本设备
- bundleName: 'com.example.startoptions',
- abilityName: 'EntryAbility',
- parameters: {
- // 自定义信息
- info: '从EntryAbility启动'
- }
- };
-
- let options: StartOptions = {
- windowMode: AbilityConstant.WindowMode.WINDOW_MODE_SPLIT_SECONDARY, // 以分屏模式拉起
- };
-
- context.startAbility(want, options).then(() => {
- hilog.info(DOMAIN_NUMBER, TAG, 'Succeeded in starting ability.');
- }).catch((err: BusinessError) => {
- hilog.error(DOMAIN_NUMBER, TAG, `Failed to start ability. Code is ${err.code}, message is ${err.message}`);
- });
- })
- }
- .width('100%')
- }
- .height('100%')
- }
- }
在多屏设备(如平板连接外接显示器、2in1设备等)上,开发者可能需要指定UIAbility在特定的屏幕上启动。例如:
开发步骤如下:
- import { common, Want, StartOptions } from '@kit.AbilityKit';
- import { hilog } from '@kit.PerformanceAnalysisKit';
- import { BusinessError } from '@kit.BasicServicesKit';
-
- const TAG: string = '[StartAbility]';
- const DOMAIN_NUMBER: number = 0xFF00;
-
- @Entry
- @Component
- struct SpecifyDisplayScreen {
-
- build() {
- Row() {
- Column() {
- Button("在当前屏幕上启动")
- .onClick(() => {
- // context为调用方UIAbility的UIAbilityContext
- let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
-
- let want: Want = {
- deviceId: '', // deviceId为空表示本设备
- bundleName: 'com.example.startoptions',
- abilityName: 'EntryAbility',
- parameters: {
- // 自定义信息
- info: '从EntryAbility启动'
- }
- };
-
- let options: StartOptions = {
- displayId: -1 // 在当前屏幕上启动
- };
-
- context.startAbility(want, options).then(() => {
- hilog.info(DOMAIN_NUMBER, TAG, 'Succeeded in starting ability.');
- }).catch((err: BusinessError) => {
- hilog.error(DOMAIN_NUMBER, TAG, `Failed to start ability. Code is ${err.code}, message is ${err.message}`);
- });
- })
- }
- .width('100%')
- }
- .height('100%')
- }
- }
需要快速切换界面时,关闭动画以提升响应速度。
开发步骤如下:
- import { common, Want, StartOptions } from '@kit.AbilityKit';
- import { hilog } from '@kit.PerformanceAnalysisKit';
- import { BusinessError } from '@kit.BasicServicesKit';
-
- const TAG: string = '[StartAbility]';
- const DOMAIN_NUMBER: number = 0xFF00;
-
- @Entry
- @Component
- struct ControlStartupAnimation {
-
- build() {
- Row() {
- Column() {
- Button("关闭启动动画")
- .onClick(() => {
- // context为调用方UIAbility的UIAbilityContext
- let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
-
- let want: Want = {
- deviceId: '', // deviceId为空表示本设备
- bundleName: 'com.example.startoptions',
- abilityName: 'EntryAbility',
- parameters: {
- // 自定义信息
- info: '从EntryAbility启动'
- }
- };
-
- let options: StartOptions = {
- withAnimation: false // 关闭启动动画
- };
-
- context.startAbility(want, options).then(() => {
- hilog.info(DOMAIN_NUMBER, TAG, 'Succeeded in starting ability.');
- }).catch((err: BusinessError) => {
- hilog.error(DOMAIN_NUMBER, TAG, `Failed to start ability. Code is ${err.code}, message is ${err.message}`);
- });
- })
- }
- .width('100%')
- }
- .height('100%')
- }
- }
在自由窗口状态下,开发者可能需要指定UIAbility窗口的初始位置。例如:
开发步骤如下:
- import { common, Want, StartOptions } from '@kit.AbilityKit';
- import { hilog } from '@kit.PerformanceAnalysisKit';
- import { BusinessError } from '@kit.BasicServicesKit';
-
- const TAG: string = '[StartAbility]';
- const DOMAIN_NUMBER: number = 0xFF00;
-
- @Entry
- @Component
- struct SetWindowPosition {
-
- build() {
- Row() {
- Column() {
- Button("设置窗口位置")
- .onClick(() => {
- // context为调用方UIAbility的UIAbilityContext
- let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
-
- let want: Want = {
- deviceId: '', // deviceId为空表示本设备
- bundleName: 'com.example.startoptions',
- abilityName: 'EntryAbility',
- parameters: {
- // 自定义信息
- info: '从EntryAbility启动'
- }
- };
-
- let options: StartOptions = {
- displayId: 0,
- windowLeft: 100, // 距离屏幕左侧100px
- windowTop: 200, // 距离屏幕顶部200px
- windowWidth: 800, // 窗口宽度800px
- windowHeight: 600 // 窗口高度600px
- };
-
- context.startAbility(want, options).then(() => {
- hilog.info(DOMAIN_NUMBER, TAG, 'Succeeded in starting ability.');
- }).catch((err: BusinessError) => {
- hilog.error(DOMAIN_NUMBER, TAG, `Failed to start ability. Code is ${err.code}, message is ${err.message}`);
- });
- })
- }
- .width('100%')
- }
- .height('100%')
- }
- }
在自由窗口状态下,开发者可能需要限制UIAbility窗口的尺寸范围,防止用户将窗口调整得过大或过小。例如:
开发步骤如下:
- import { common, Want, StartOptions } from '@kit.AbilityKit';
- import { hilog } from '@kit.PerformanceAnalysisKit';
- import { BusinessError } from '@kit.BasicServicesKit';
-
- const TAG: string = '[StartAbility]';
- const DOMAIN_NUMBER: number = 0xFF00;
-
- @Entry
- @Component
- struct SetWindowSizeConstraintsAbility {
-
- build() {
- Row() {
- Column() {
- Button("设置ability的窗口尺寸")
- .onClick(() => {
- // context为调用方UIAbility的UIAbilityContext
- let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
-
- let want: Want = {
- deviceId: '', // deviceId为空表示本设备
- bundleName: 'com.example.startoptions',
- abilityName: 'EntryAbility',
- parameters: {
- // 自定义信息
- info: '从EntryAbility启动'
- }
- };
-
- let options: StartOptions = {
- startWindowBackgroundColor: '#E510FFFF', // ARGB格式
- minWindowWidth: 320, // 最小宽度320vp
- maxWindowWidth: 2560, // 最大宽度2560vp
- minWindowHeight: 240, // 最小高度240vp
- maxWindowHeight: 2560 // 最大高度2560vp
- };
-
- context.startAbility(want, options).then(() => {
- hilog.info(DOMAIN_NUMBER, TAG, 'Succeeded in starting ability.');
- }).catch((err: BusinessError) => {
- hilog.error(DOMAIN_NUMBER, TAG, `Failed to start ability. Code is ${err.code}, message is ${err.message}`);
- });
- })
- }
- .width('100%')
- }
- .height('100%')
- }
- }
在某些场景下,开发者需要启动UIAbility但不希望其立即显示在前台。例如:启动一个用于后台监控的UIAbility,等待条件触发后再显示。
开发步骤如下:
当设置目标UIAbility为不可见时:
- import { AbilityConstant, contextConstant, StartOptions, UIAbility, Want } from '@kit.AbilityKit';
- import { hilog } from '@kit.PerformanceAnalysisKit';
- import { window } from '@kit.ArkUI';
-
- const DOMAIN = 0x0000;
- const TAG: string = '[StartAbility]';
- const DOMAIN_NUMBER: number = 0xFF00;
-
-
- export default class HideStartedUIAbilityAbility extends UIAbility {
- // ...
-
- onForeground(): void {
- // Ability has brought to foreground
- hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground');
-
- let want: Want = {
- deviceId: '', // deviceId为空表示本设备
- bundleName: 'com.example.startoptions',
- abilityName: 'EntryAbility',
- parameters: {
- // 自定义信息
- info: '从EntryAbility启动'
- }
- };
-
- let options: StartOptions = {
- processMode: contextConstant.ProcessMode.NEW_PROCESS_ATTACH_TO_PARENT,
- startupVisibility: contextConstant.StartupVisibility.STARTUP_HIDE
- };
-
- try {
- this.context.startAbility(want, options, (err: BusinessError) => {
- if (err.code) {
- // 处理业务逻辑错误
- console.error(`startAbility failed, code is ${err.code}, message is ${err.message}`);
- return;
- }
- // 执行正常业务
- console.info('startAbility succeed');
- });
- } catch (err) {
- // 处理入参错误异常
- let code = (err as BusinessError).code;
- let message = (err as BusinessError).message;
- console.error(`startAbility failed, code is ${code}, message is ${message}`);
- }
-
- }
-
- // ...
- }
开发者希望自定义UIAbility启动时显示的启动页图标和背景颜色。例如:
开发步骤如下:
- import { AbilityConstant, StartOptions, UIAbility, Want } from '@kit.AbilityKit';
- import { hilog } from '@kit.PerformanceAnalysisKit';
- import { window } from '@kit.ArkUI';
- import { image } from '@kit.ImageKit';
-
- const DOMAIN = 0x0000;
- const TAG: string = '[StartAbility]';
- const DOMAIN_NUMBER: number = 0xFF00;
-
- export default class SetBackgroundColorAbility extends UIAbility {
- // ...
-
- async onForeground(): Promise<void> {
- // Ability has brought to foreground
- hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground');
-
- let want: Want = {
- deviceId: '', // deviceId为空表示本设备
- bundleName: 'com.example.startoptions',
- abilityName: 'EntryAbility',
- parameters: {
- // 自定义信息
- info: '从EntryAbility启动'
- }
- };
-
- // 创建PixelMap对象
- let color = new ArrayBuffer(512 * 512 * 4);
- let bufferArr = new Uint8Array(color);
- for (let i = 0; i < bufferArr.length; i += 4) {
- bufferArr[i] = 255;
- bufferArr[i+1] = 0;
- bufferArr[i+2] = 122;
- bufferArr[i+3] = 255;
- }
-
- let windowParam: window.WindowCreateParams = {};
-
- let options: StartOptions = {
- startWindowIcon: await image.createPixelMap(color, {
- editable: true,
- pixelFormat: image.PixelMapFormat.RGBA_8888,
- size: { height: 512, width: 512 }
-
- }),
- startWindowBackgroundColor: '#E510FFFF', // ARGB格式
- minWindowWidth: 320,
- minWindowHeight: 240,
- maxWindowWidth: 2560,
- maxWindowHeight: 2560,
- windowCreateParams: windowParam
- };
-
- this.context.startAbility(want, options).then(() => {
- hilog.info(DOMAIN_NUMBER, TAG, 'Succeeded in starting ability.');
- }).catch((err: BusinessError) => {
- hilog.error(DOMAIN_NUMBER, TAG, `Failed to start ability. Code is ${err.code}, message is ${err.message}`);
- });
-
- }
-
- // ...
- }
在某些场景下,开发者需要获取UIAbility启动的结果,以便进行后续处理。例如:使用其他应用账号登录,需要知道拉起其他应用是否成功。
开发步骤如下:
- import { AbilityConstant, CompletionHandler, StartOptions, UIAbility, Want, bundleManager } from '@kit.AbilityKit';
- import { hilog } from '@kit.PerformanceAnalysisKit';
- import { window } from '@kit.ArkUI';
-
- const DOMAIN = 0x0000;
- const TAG: string = '[StartAbility]';
- const DOMAIN_NUMBER: number = 0xFF00;
-
- export default class GetLaunchResultAbility extends UIAbility {
- // ...
-
- onForeground(): void {
- // Ability has brought to foreground
- hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground');
-
- let want: Want = {
- deviceId: '', // deviceId为空表示本设备
- bundleName: 'com.example.startoptions',
- abilityName: 'EntryAbility',
- parameters: {
- // 自定义信息
- info: '从EntryAbility启动'
- }
- };
-
- let completionHandler: CompletionHandler = {
- onRequestSuccess: (elementName: bundleManager.ElementName, message: string): void => {
- console.info(`${elementName.bundleName}-${elementName.moduleName}-${elementName.abilityName} start succeeded: ${message}`);
- },
- onRequestFailure: (elementName: bundleManager.ElementName, message: string): void => {
- console.error(`${elementName.bundleName}-${elementName.moduleName}-${elementName.abilityName} start failed: ${message}`);
- }
- };
-
- let options: StartOptions = {
- completionHandler: completionHandler
- };
-
- this.context.startAbility(want, options).then(() => {
- hilog.info(DOMAIN_NUMBER, TAG, 'Succeeded in starting ability.');
- }).catch((err: BusinessError) => {
- hilog.error(DOMAIN_NUMBER, TAG, `Failed to start ability. Code is ${err.code}, message is ${err.message}`);
- });
-
- }
-
- // ...
- }
在某些场景下,开发者希望启动UIAbility时不显示启动页,直接显示应用界面。例如:
开发步骤如下:
启动页介绍和规格详见StartWindow。
- import { AbilityConstant, StartOptions, UIAbility, Want } from '@kit.AbilityKit';
- import { hilog } from '@kit.PerformanceAnalysisKit';
- import { window } from '@kit.ArkUI';
-
- const DOMAIN = 0x0000;
- const TAG: string = '[StartAbility]';
- const DOMAIN_NUMBER: number = 0xFF00;
-
- export default class HideSplashScreenAbility extends UIAbility {
- // ...
-
- onForeground(): void {
- // Ability has brought to foreground
- hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground');
-
- let want: Want = {
- deviceId: '', // deviceId为空表示本设备
- bundleName: 'com.example.startoptions',
- abilityName: 'EntryAbility',
- parameters: {
- // 自定义信息
- info: '从EntryAbility启动'
- }
- };
-
- let options: StartOptions = {
- hideStartWindow: true // 隐藏启动页
- };
-
- this.context.startAbility(want, options).then(() => {
- hilog.info(DOMAIN_NUMBER, TAG, 'Succeeded in starting ability.');
- }).catch((err: BusinessError) => {
- hilog.error(DOMAIN_NUMBER, TAG, `Failed to start ability. Code is ${err.code}, message is ${err.message}`);
- });
- }
-
- // ...
- }
在某些高级场景下,开发者需要更细粒度地控制UIAbility启动时的窗口参数。例如:窗口动效。
开发步骤如下:
- import { AbilityConstant, StartOptions, UIAbility, Want } from '@kit.AbilityKit';
- import { hilog } from '@kit.PerformanceAnalysisKit';
- import { window } from '@kit.ArkUI';
- import { image } from '@kit.ImageKit';
-
- const DOMAIN = 0x0000;
- const TAG: string = '[StartAbility]';
- const DOMAIN_NUMBER: number = 0xFF00;
-
- export default class SetBackgroundColorAbility extends UIAbility {
- // ...
-
- async onForeground(): Promise<void> {
- // Ability has brought to foreground
- hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground');
-
- let want: Want = {
- deviceId: '', // deviceId为空表示本设备
- bundleName: 'com.example.startoptions',
- abilityName: 'EntryAbility',
- parameters: {
- // 自定义信息
- info: '从EntryAbility启动'
- }
- };
-
- // 创建PixelMap对象
- let color = new ArrayBuffer(512 * 512 * 4);
- let bufferArr = new Uint8Array(color);
- for (let i = 0; i < bufferArr.length; i += 4) {
- bufferArr[i] = 255;
- bufferArr[i+1] = 0;
- bufferArr[i+2] = 122;
- bufferArr[i+3] = 255;
- }
-
- let windowParam: window.WindowCreateParams = {};
-
- let options: StartOptions = {
- startWindowIcon: await image.createPixelMap(color, {
- editable: true,
- pixelFormat: image.PixelMapFormat.RGBA_8888,
- size: { height: 512, width: 512 }
-
- }),
- startWindowBackgroundColor: '#E510FFFF', // ARGB格式
- minWindowWidth: 320,
- minWindowHeight: 240,
- maxWindowWidth: 2560,
- maxWindowHeight: 2560,
- windowCreateParams: windowParam
- };
-
- this.context.startAbility(want, options).then(() => {
- hilog.info(DOMAIN_NUMBER, TAG, 'Succeeded in starting ability.');
- }).catch((err: BusinessError) => {
- hilog.error(DOMAIN_NUMBER, TAG, `Failed to start ability. Code is ${err.code}, message is ${err.message}`);
- });
-
- }
-
- // ...
- }