1e41f4b71Sopenharmony_ci# @system.app (应用上下文) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci> **说明:** 4e41f4b71Sopenharmony_ci> 5e41f4b71Sopenharmony_ci> 本模块首批接口从API version 3开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci 8e41f4b71Sopenharmony_ci## 导入模块 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci```ts 12e41f4b71Sopenharmony_ciimport app, { AppResponse } from '@system.app' 13e41f4b71Sopenharmony_ci``` 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci## App 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci### getInfo 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_cistatic getInfo(): AppResponse 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci获取当前应用配置文件中声明的信息。 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci从API Version9开始,推荐使用[bundleManager.getBundleInfoForSelf](../apis-ability-kit/js-apis-bundleManager.md#bundlemanagergetbundleinfoforself)。 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Lite 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci**返回值:** 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci| 参数类型 | 说明 | 32e41f4b71Sopenharmony_ci| -------- | -------- | 33e41f4b71Sopenharmony_ci| [AppResponse](#appresponse) | 定义AppResponse信息。 | 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci**示例:** 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci```ts 38e41f4b71Sopenharmony_ciimport app, { AppResponse } from '@system.app' 39e41f4b71Sopenharmony_ciexport default class Info { 40e41f4b71Sopenharmony_ci getInfo() { 41e41f4b71Sopenharmony_ci let info:AppResponse = app.getInfo() 42e41f4b71Sopenharmony_ci console.log(JSON.stringify(info)) 43e41f4b71Sopenharmony_ci } 44e41f4b71Sopenharmony_ci} 45e41f4b71Sopenharmony_ci``` 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci### terminate 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_cistatic terminate(): void 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci退出当前Ability。 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ci从API Version 7开始,推荐使用[`@ohos.ability.featureAbility`](../apis-ability-kit/js-apis-ability-featureAbility.md)。 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Lite 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ci**示例:** 60e41f4b71Sopenharmony_ci 61e41f4b71Sopenharmony_ci```ts 62e41f4b71Sopenharmony_ciimport app, { AppResponse } from '@system.app' 63e41f4b71Sopenharmony_ciexport default class TerM { 64e41f4b71Sopenharmony_ci terminate() { 65e41f4b71Sopenharmony_ci app.terminate() 66e41f4b71Sopenharmony_ci } 67e41f4b71Sopenharmony_ci} 68e41f4b71Sopenharmony_ci``` 69e41f4b71Sopenharmony_ci### setImageCacheCount<sup>7+</sup> 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_cistatic setImageCacheCount(value: number): void 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ci设置内存中缓存解码后图片的数量上限,提升再次加载同源图片的加载速度。如果不设置则默认为0,不进行缓存。缓存采用内置的LRU策略,新图片加载后,如果超过缓存上限,会删除最久未再次加载的缓存。建议根据应用内存需求,设置合理缓存数量,数字过大可能导致内存使用过高。 74e41f4b71Sopenharmony_ci 75e41f4b71Sopenharmony_cisetImageCacheCount方法需要在@Entry标记的页面,[onPageShow](../apis-arkui/arkui-ts/ts-custom-component-lifecycle.md#onpageshow)或[aboutToAppear](../apis-arkui/arkui-ts/ts-custom-component-lifecycle.md#abouttoappear)里面设置才生效。 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_cisetImageCacheCount、setImageRawDataCacheSize、和setImageFileCacheSize并不灵活,后续不继续演进,对于复杂情况,更推荐使用[ImageKnife](https://gitee.com/openharmony-tpc/ImageKnife)。 78e41f4b71Sopenharmony_ci 79e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 80e41f4b71Sopenharmony_ci 81e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 82e41f4b71Sopenharmony_ci 83e41f4b71Sopenharmony_ci**参数:** 84e41f4b71Sopenharmony_ci 85e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 86e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 87e41f4b71Sopenharmony_ci| value | number | 是 | 内存中解码后图片的缓存数量。 | 88e41f4b71Sopenharmony_ci 89e41f4b71Sopenharmony_ci**示例:** 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ci```ts 92e41f4b71Sopenharmony_ci// xxx.ets 93e41f4b71Sopenharmony_ciimport app, { AppResponse } from '@system.app' 94e41f4b71Sopenharmony_ci 95e41f4b71Sopenharmony_ci@Entry 96e41f4b71Sopenharmony_ci@Component 97e41f4b71Sopenharmony_cistruct Index { 98e41f4b71Sopenharmony_ci onPageShow() { 99e41f4b71Sopenharmony_ci // 设置解码后图片内存缓存上限为100张 100e41f4b71Sopenharmony_ci app.setImageCacheCount(100) 101e41f4b71Sopenharmony_ci console.info('Application onPageShow') 102e41f4b71Sopenharmony_ci } 103e41f4b71Sopenharmony_ci onDestroy() { 104e41f4b71Sopenharmony_ci console.info('Application onDestroy') 105e41f4b71Sopenharmony_ci } 106e41f4b71Sopenharmony_ci 107e41f4b71Sopenharmony_ci build() { 108e41f4b71Sopenharmony_ci Row(){ 109e41f4b71Sopenharmony_ci // xxxxxxxxxxxxx为图片地址 110e41f4b71Sopenharmony_ci Image('xxxxxxxxxxxxx') 111e41f4b71Sopenharmony_ci .width(200) 112e41f4b71Sopenharmony_ci .height(50) 113e41f4b71Sopenharmony_ci }.width('100%') 114e41f4b71Sopenharmony_ci } 115e41f4b71Sopenharmony_ci} 116e41f4b71Sopenharmony_ci``` 117e41f4b71Sopenharmony_ci 118e41f4b71Sopenharmony_ci### setImageRawDataCacheSize<sup>7+</sup> 119e41f4b71Sopenharmony_ci 120e41f4b71Sopenharmony_cistatic setImageRawDataCacheSize(value: number): void 121e41f4b71Sopenharmony_ci 122e41f4b71Sopenharmony_ci设置内存中缓存解码前图片数据的大小上限,单位为字节,提升再次加载同源图片的加载速度。如果不设置则默认为0,不进行缓存。缓存采用内置的LRU策略,新图片加载后,如果解码前数据超过缓存上限,会删除最久未再次加载的图片数据缓存。建议根据应用内存需求,设置合理缓存上限,过大可能导致应用内存使用过高。 123e41f4b71Sopenharmony_ci 124e41f4b71Sopenharmony_cisetImageRawDataCacheSize方法需要在@Entry标记的页面,[onPageShow](../apis-arkui/arkui-ts/ts-custom-component-lifecycle.md#onpageshow)或[aboutToAppear](../apis-arkui/arkui-ts/ts-custom-component-lifecycle.md#abouttoappear)里面设置才生效。 125e41f4b71Sopenharmony_ci 126e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 127e41f4b71Sopenharmony_ci 128e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 129e41f4b71Sopenharmony_ci 130e41f4b71Sopenharmony_ci**参数:** 131e41f4b71Sopenharmony_ci 132e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 133e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 134e41f4b71Sopenharmony_ci| value | number | 是 | 内存中解码前图片数据的缓存大小,单位为字节。 | 135e41f4b71Sopenharmony_ci 136e41f4b71Sopenharmony_ci**示例:** 137e41f4b71Sopenharmony_ci 138e41f4b71Sopenharmony_ci```ts 139e41f4b71Sopenharmony_ci// xxx.ets 140e41f4b71Sopenharmony_ciimport app, { AppResponse } from '@system.app' 141e41f4b71Sopenharmony_ci 142e41f4b71Sopenharmony_ci@Entry 143e41f4b71Sopenharmony_ci@Component 144e41f4b71Sopenharmony_cistruct Index { 145e41f4b71Sopenharmony_ci onPageShow() { 146e41f4b71Sopenharmony_ci // 设置解码前图片数据内存缓存上限为100MB (100MB=100*1024*1024B=104857600B) 147e41f4b71Sopenharmony_ci app.setImageRawDataCacheSize(104857600) 148e41f4b71Sopenharmony_ci console.info('Application onPageShow') 149e41f4b71Sopenharmony_ci } 150e41f4b71Sopenharmony_ci onDestroy() { 151e41f4b71Sopenharmony_ci console.info('Application onDestroy') 152e41f4b71Sopenharmony_ci } 153e41f4b71Sopenharmony_ci 154e41f4b71Sopenharmony_ci build() { 155e41f4b71Sopenharmony_ci Row(){ 156e41f4b71Sopenharmony_ci // xxxxxxxxxxxxx为图片地址 157e41f4b71Sopenharmony_ci Image('xxxxxxxxxxxxx') 158e41f4b71Sopenharmony_ci .width(200) 159e41f4b71Sopenharmony_ci .height(50) 160e41f4b71Sopenharmony_ci }.width('100%') 161e41f4b71Sopenharmony_ci } 162e41f4b71Sopenharmony_ci} 163e41f4b71Sopenharmony_ci``` 164e41f4b71Sopenharmony_ci 165e41f4b71Sopenharmony_ci### setImageFileCacheSize<sup>7+</sup> 166e41f4b71Sopenharmony_ci 167e41f4b71Sopenharmony_cistatic setImageFileCacheSize(value: number): void 168e41f4b71Sopenharmony_ci 169e41f4b71Sopenharmony_ci设置图片文件缓存的大小上限,单位为字节,提升再次加载同源图片的加载速度,特别是对网络图源会有较明显提升。如果不设置则默认为100MB。缓存采用内置的LRU策略,新图片加载后,如果超过文件缓存上限,会按照时间由远到近删除缓存图片文件直到缓存图片大小满足缓存上限。建议根据应用实际需求,设置合理文件缓存上限,数字过大可能导致磁盘空间占用过高。 170e41f4b71Sopenharmony_ci 171e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 172e41f4b71Sopenharmony_ci 173e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 174e41f4b71Sopenharmony_ci 175e41f4b71Sopenharmony_ci**参数:** 176e41f4b71Sopenharmony_ci 177e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 178e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 179e41f4b71Sopenharmony_ci| value | number | 是 | 图片文件的缓存大小,单位为字节。 | 180e41f4b71Sopenharmony_ci 181e41f4b71Sopenharmony_ci**示例:** 182e41f4b71Sopenharmony_ci 183e41f4b71Sopenharmony_ci```ts 184e41f4b71Sopenharmony_ci// app.ets 185e41f4b71Sopenharmony_ciimport app, { AppResponse } from '@system.app' 186e41f4b71Sopenharmony_ci 187e41f4b71Sopenharmony_ciexport default class OnC { 188e41f4b71Sopenharmony_ci onCreate() { 189e41f4b71Sopenharmony_ci app.setImageFileCacheSize(209715200) 190e41f4b71Sopenharmony_ci // 设置图片文件数据缓存上限为200MB (200MB=200*1024*1024B=209715200B) 191e41f4b71Sopenharmony_ci console.info('Application onCreate') 192e41f4b71Sopenharmony_ci } 193e41f4b71Sopenharmony_ci onDestroy() { 194e41f4b71Sopenharmony_ci console.info('Application onDestroy') 195e41f4b71Sopenharmony_ci } 196e41f4b71Sopenharmony_ci} 197e41f4b71Sopenharmony_ci``` 198e41f4b71Sopenharmony_ci 199e41f4b71Sopenharmony_ci### ScreenOnVisible<sup>(deprecated)</sup> 200e41f4b71Sopenharmony_ci 201e41f4b71Sopenharmony_cistatic screenOnVisible(options?: ScreenOnVisibleOptions): void 202e41f4b71Sopenharmony_ci 203e41f4b71Sopenharmony_ci定义屏幕唤醒时是否保持应用可见。 204e41f4b71Sopenharmony_ci 205e41f4b71Sopenharmony_ci该接口从API Version 8 开始废弃。 206e41f4b71Sopenharmony_ci 207e41f4b71Sopenharmony_ci**系统能力:** 以下各项对应的系统能力均为SystemCapability.ArkUI.ArkUI.Full 208e41f4b71Sopenharmony_ci 209e41f4b71Sopenharmony_ci| 名称 | 类型 | 必填 | 说明 | 210e41f4b71Sopenharmony_ci| ------- | ------------------------------------------------- | ---- | ------------------------------------------------------------ | 211e41f4b71Sopenharmony_ci| options | [ScreenOnVisibleOptions](#screenonvisibleoptions) | 否 | 当启动保活时,锁屏时将阻止系统返回桌面显示,以保持屏幕唤醒时应用可见。 | 212e41f4b71Sopenharmony_ci 213e41f4b71Sopenharmony_ci### requestFullWindow<sup>(deprecated)</sup> 214e41f4b71Sopenharmony_ci 215e41f4b71Sopenharmony_cistatic requestFullWindow(options?: RequestFullWindowOptions): void 216e41f4b71Sopenharmony_ci 217e41f4b71Sopenharmony_ci请求应用以全窗口运行,FA在某些场景下(如半模态FA)会以非全窗口运行,调用该接口会从非全窗口切换为全窗口运行,如果已经以全窗口运行则该接口调用无效。 218e41f4b71Sopenharmony_ci 219e41f4b71Sopenharmony_ci从API Version 7开始,推荐使用[`@ohos.window`](js-apis-window.md)。 220e41f4b71Sopenharmony_ci 221e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 222e41f4b71Sopenharmony_ci 223e41f4b71Sopenharmony_ci**参数:** 224e41f4b71Sopenharmony_ci 225e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 226e41f4b71Sopenharmony_ci| ------- | ----------------------------------------------------- | ---- | ------------------------------------------------------------ | 227e41f4b71Sopenharmony_ci| options | [RequestFullWindowOptions](#requestfullwindowoptions) | 否 | 请求全屏时,设定非全屏到全屏的过渡时间,单位为毫秒,默认时间与非全屏到全屏的距离成正比。 | 228e41f4b71Sopenharmony_ci 229e41f4b71Sopenharmony_ci**示例:** 230e41f4b71Sopenharmony_ci 231e41f4b71Sopenharmony_ci```ts 232e41f4b71Sopenharmony_ciimport app, { AppResponse } from '@system.app' 233e41f4b71Sopenharmony_ciexport default class Req { 234e41f4b71Sopenharmony_ci requestFullWindow() { 235e41f4b71Sopenharmony_ci app.requestFullWindow({ 236e41f4b71Sopenharmony_ci duration: 200 237e41f4b71Sopenharmony_ci }) 238e41f4b71Sopenharmony_ci } 239e41f4b71Sopenharmony_ci} 240e41f4b71Sopenharmony_ci``` 241e41f4b71Sopenharmony_ci 242e41f4b71Sopenharmony_ci## AppResponse 243e41f4b71Sopenharmony_ci 244e41f4b71Sopenharmony_ci定义AppResponse信息。 245e41f4b71Sopenharmony_ci 246e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 247e41f4b71Sopenharmony_ci 248e41f4b71Sopenharmony_ci**系统能力:** 以下各项对应的系统能力有所不同,详见下表。 249e41f4b71Sopenharmony_ci 250e41f4b71Sopenharmony_ci| 名称 | 类型 | 必填 | 说明 | 251e41f4b71Sopenharmony_ci| -------- | -------- | -------- |-------- | 252e41f4b71Sopenharmony_ci| appID<sup>6+</sup> | string | 是 | 表示应用的包名,用于标识应用的唯一性。 <br> **系统能力:** SystemCapability.ArkUI.ArkUI.Full| 253e41f4b71Sopenharmony_ci| appName | string | 是 | 表示应用的名称。 <br> **系统能力:** SystemCapability.ArkUI.ArkUI.Lite| 254e41f4b71Sopenharmony_ci| versionName | string | 是 | 表示应用的版本名称。 <br> **系统能力:** SystemCapability.ArkUI.ArkUI.Lite| 255e41f4b71Sopenharmony_ci| versionCode | number | 是 | 表示应用的版本号。 <br> **系统能力:** SystemCapability.ArkUI.ArkUI.Lite| 256e41f4b71Sopenharmony_ci 257e41f4b71Sopenharmony_ci## ScreenOnVisibleOptions 258e41f4b71Sopenharmony_ci 259e41f4b71Sopenharmony_ci定义屏幕上可见接口的选项。 260e41f4b71Sopenharmony_ci 261e41f4b71Sopenharmony_ci**系统能力:** 以下各项对应的系统能力均为SystemCapability.ArkUI.ArkUI.Full 262e41f4b71Sopenharmony_ci 263e41f4b71Sopenharmony_ci| 名称 | 类型 | 必填 | 说明 | 264e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 265e41f4b71Sopenharmony_ci| visible | boolean | 否 | 是否启动保活,默认值false。 | 266e41f4b71Sopenharmony_ci| success | () => void | 否 | 接口调用成功的回调函数。 | 267e41f4b71Sopenharmony_ci| fail | (data: string, code: number) => void | 否 | 接口调用失败的回调函数。 | 268e41f4b71Sopenharmony_ci| complete | () => void | 否 | 接口调用结束的回调函数。 | 269e41f4b71Sopenharmony_ci 270e41f4b71Sopenharmony_ci## RequestFullWindowOptions 271e41f4b71Sopenharmony_ci 272e41f4b71Sopenharmony_ci定义RequestFullWindow接口的选项。 273e41f4b71Sopenharmony_ci 274e41f4b71Sopenharmony_ci**系统能力:** 以下各项对应的系统能力均为SystemCapability.ArkUI.ArkUI.Full 275e41f4b71Sopenharmony_ci 276e41f4b71Sopenharmony_ci| 名称 | 类型 | 必填 | 说明 | 277e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 278e41f4b71Sopenharmony_ci| duration | number | 是 | 定义动画选项的持续时间,单位为毫秒。 | 279e41f4b71Sopenharmony_ci 280