1# application_context.h 2 3## 概述 4 5提供应用级别上下文相关的接口。 6 7**引用文件:** <AbilityKit/ability_runtime/application_context.h> 8 9**库:** libability_runtime.so 10 11**系统能力:** SystemCapability.Ability.AbilityRuntime.Core 12 13**起始版本:** 13 14 15**相关模块:** [AbilityRuntime](capi-abilityruntime.md) 16 17## 汇总 18 19### 函数 20 21| 名称 | 描述 | 22| -- | -- | 23| [AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetCacheDir(char* buffer, int32_t bufferSize, int32_t* writeLength)](#oh_abilityruntime_applicationcontextgetcachedir) | 获取本应用的应用级的缓存目录。 | 24| [AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetAreaMode(AbilityRuntime_AreaMode* areaMode)](#oh_abilityruntime_applicationcontextgetareamode) | 获取本应用的应用级的文件数据加密等级。 | 25| [AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetBundleName(char* buffer, int32_t bufferSize, int32_t* writeLength)](#oh_abilityruntime_applicationcontextgetbundlename) | 获取应用包名。 | 26| [AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetTempDir(char* buffer, const int32_t bufferSize, int32_t* writeLength)](#oh_abilityruntime_applicationcontextgettempdir) | 获取本应用的应用级的临时文件目录。 | 27| [AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetFilesDir(char* buffer, const int32_t bufferSize, int32_t* writeLength)](#oh_abilityruntime_applicationcontextgetfilesdir) | 获取本应用的应用级的通用文件目录。 | 28| [AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetDatabaseDir(char* buffer, const int32_t bufferSize, int32_t* writeLength)](#oh_abilityruntime_applicationcontextgetdatabasedir) | 获取本应用的应用级的数据库文件目录。 | 29| [AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetPreferencesDir(char* buffer, const int32_t bufferSize, int32_t* writeLength)](#oh_abilityruntime_applicationcontextgetpreferencesdir) | 获取本应用的应用级的首选项文件目录。 | 30| [AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetBundleCodeDir(char* buffer, const int32_t bufferSize, int32_t* writeLength)](#oh_abilityruntime_applicationcontextgetbundlecodedir) | 获取本应用的应用级的安装文件目录。 | 31| [AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetDistributedFilesDir(char* buffer, const int32_t bufferSize, int32_t* writeLength)](#oh_abilityruntime_applicationcontextgetdistributedfilesdir) | 获取本应用的应用级的分布式文件目录。 | 32| [AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetCloudFileDir(char* buffer, const int32_t bufferSize, int32_t* writeLength)](#oh_abilityruntime_applicationcontextgetcloudfiledir) | 获取本应用的应用级的云文件目录。 | 33| [AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetResourceDir(const char* moduleName, char* buffer, const int32_t bufferSize, int32_t* writeLength)](#oh_abilityruntime_applicationcontextgetresourcedir) | 获取应用级别的资源目录。 | 34| [AbilityRuntime_ErrorCode OH_AbilityRuntime_StartSelfUIAbility(AbilityBase_Want *want)](#oh_abilityruntime_startselfuiability) | 启动当前应用的UIAbility。 | 35| [AbilityRuntime_ErrorCode OH_AbilityRuntime_StartSelfUIAbilityWithStartOptions(AbilityBase_Want *want,AbilityRuntime_StartOptions *options)](#oh_abilityruntime_startselfuiabilitywithstartoptions) | 通过StartOptions启动当前应用的UIAbility。 | 36 37## 函数说明 38 39### OH_AbilityRuntime_ApplicationContextGetCacheDir() 40 41``` 42AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetCacheDir(char* buffer, int32_t bufferSize, int32_t* writeLength) 43``` 44 45**描述** 46 47获取本应用的应用级的缓存目录。 48 49**起始版本:** 13 50 51**参数:** 52 53| 参数项 | 描述 | 54| -- | -- | 55| char* buffer | 指向缓冲区的指针,用于接收本应用的应用级的缓存目录。 | 56| int32_t bufferSize | 缓冲区大小,单位为字节。 | 57| int32_t* writeLength | 在返回[ABILITY_RUNTIME_ERROR_CODE_NO_ERROR](capi-ability-runtime-common-h.md#abilityruntime_errorcode)时,表示实际写入到缓冲区的字符串长度,单位为字节。 | 58 59**返回:** 60 61| 类型 | 说明 | 62| -- | -- | 63| [AbilityRuntime_ErrorCode](capi-ability-runtime-common-h.md#abilityruntime_errorcode) | 返回执行结果。<br>ABILITY_RUNTIME_ERROR_CODE_NO_ERROR - 查询成功。<br>ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID - 入参buffer或者writeLength为空,或者缓冲区大小小于需要写入的大小。<br>ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST - 应用上下文不存在,如在应用创建的[子进程](capi-childprocess.md)中应用级别上下文不存在。 | 64 65### OH_AbilityRuntime_ApplicationContextGetAreaMode() 66 67``` 68AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetAreaMode(AbilityRuntime_AreaMode* areaMode) 69``` 70 71**描述** 72 73获取本应用的应用级的文件数据加密等级。 74 75**起始版本:** 13 76 77**参数:** 78 79| 参数项 | 描述 | 80| -- | -- | 81| [AbilityRuntime_AreaMode](capi-context-constant-h.md#abilityruntime_areamode)* areaMode | 指向接收数据加密等级的指针。 | 82 83**返回:** 84 85| 类型 | 说明 | 86| -- | -- | 87| [AbilityRuntime_ErrorCode](capi-ability-runtime-common-h.md#abilityruntime_errorcode) | 返回执行结果。<br>ABILITY_RUNTIME_ERROR_CODE_NO_ERROR - 查询成功。<br>ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID - 入参buffer或者writeLength为空,或者缓冲区大小小于需要写入的大小。<br>ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST - 应用上下文不存在,如在应用创建的[子进程](capi-childprocess.md)中应用级别上下文不存在。 | 88 89### OH_AbilityRuntime_ApplicationContextGetBundleName() 90 91``` 92AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetBundleName(char* buffer, int32_t bufferSize, int32_t* writeLength) 93``` 94 95**描述** 96 97获取应用包名。 98 99**起始版本:** 13 100 101**参数:** 102 103| 参数项 | 描述 | 104| -- | -- | 105| char* buffer | 指向缓冲区的指针,用于接收应用包名。 | 106| int32_t bufferSize | 缓冲区大小,单位为字节。 | 107| int32_t* writeLength | 在返回[ABILITY_RUNTIME_ERROR_CODE_NO_ERROR](capi-ability-runtime-common-h.md#abilityruntime_errorcode)时,表示实际写入到缓冲区的字符串长度,单位为字节。 | 108 109**返回:** 110 111| 类型 | 说明 | 112| -- | -- | 113| [AbilityRuntime_ErrorCode](capi-ability-runtime-common-h.md#abilityruntime_errorcode) | 返回执行结果。<br>ABILITY_RUNTIME_ERROR_CODE_NO_ERROR - 查询成功。<br>ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID - 入参buffer或者writeLength为空,或者缓冲区大小小于需要写入的大小。<br>ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST - 应用上下文不存在,如在应用创建的[子进程](capi-childprocess.md)中应用级别上下文不存在。 | 114 115### OH_AbilityRuntime_ApplicationContextGetTempDir() 116 117``` 118AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetTempDir(char* buffer, const int32_t bufferSize, int32_t* writeLength) 119``` 120 121**描述** 122 123获取本应用的应用级的临时文件目录。 124 125**起始版本:** 16 126 127**参数:** 128 129| 参数项 | 描述 | 130| -- | -- | 131| char* buffer | 指向缓冲区的指针,用于接收应用包名。 | 132| const int32_t bufferSize | 缓冲区大小,单位为字节。 | 133| int32_t* writeLength | 在返回[ABILITY_RUNTIME_ERROR_CODE_NO_ERROR](capi-ability-runtime-common-h.md#abilityruntime_errorcode)时,表示实际写入到缓冲区的字符串长度,单位为字节。 | 134 135**返回:** 136 137| 类型 | 说明 | 138| -- | -- | 139| [AbilityRuntime_ErrorCode](capi-ability-runtime-common-h.md#abilityruntime_errorcode) | 返回执行结果。<br>ABILITY_RUNTIME_ERROR_CODE_NO_ERROR - 查询成功。<br>ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID - 入参buffer或者writeLength为空,或者缓冲区大小小于需要写入的大小。<br>ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST - 应用上下文不存在,如在应用创建的[子进程](capi-childprocess.md)中应用级别上下文不存在。 | 140 141### OH_AbilityRuntime_ApplicationContextGetFilesDir() 142 143``` 144AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetFilesDir(char* buffer, const int32_t bufferSize, int32_t* writeLength) 145``` 146 147**描述** 148 149获取本应用的应用级的通用文件目录。 150 151**起始版本:** 16 152 153**参数:** 154 155| 参数项 | 描述 | 156| -- | -- | 157| char* buffer | 指向缓冲区的指针,用于接收应用包名。 | 158| const int32_t bufferSize | 缓冲区大小,单位为字节。 | 159| int32_t* writeLength | 在返回[ABILITY_RUNTIME_ERROR_CODE_NO_ERROR](capi-ability-runtime-common-h.md#abilityruntime_errorcode)时,表示实际写入到缓冲区的字符串长度,单位为字节。 | 160 161**返回:** 162 163| 类型 | 说明 | 164| -- | -- | 165| [AbilityRuntime_ErrorCode](capi-ability-runtime-common-h.md#abilityruntime_errorcode) | 返回执行结果。<br>ABILITY_RUNTIME_ERROR_CODE_NO_ERROR - 查询成功。<br>ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID - 入参buffer或者writeLength为空,或者缓冲区大小小于需要写入的大小。<br>ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST - 应用上下文不存在,如在应用创建的[子进程](capi-childprocess.md)中应用级别上下文不存在。 | 166 167### OH_AbilityRuntime_ApplicationContextGetDatabaseDir() 168 169``` 170AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetDatabaseDir(char* buffer, const int32_t bufferSize, int32_t* writeLength) 171``` 172 173**描述** 174 175获取本应用的应用级的数据库文件目录。 176 177**起始版本:** 16 178 179**参数:** 180 181| 参数项 | 描述 | 182| -- | -- | 183| char* buffer | 指向缓冲区的指针,用于接收应用包名。 | 184| const int32_t bufferSize | 缓冲区大小,单位为字节。 | 185| int32_t* writeLength | 在返回[ABILITY_RUNTIME_ERROR_CODE_NO_ERROR](capi-ability-runtime-common-h.md#abilityruntime_errorcode)时,表示实际写入到缓冲区的字符串长度,单位为字节。 | 186 187**返回:** 188 189| 类型 | 说明 | 190| -- | -- | 191| [AbilityRuntime_ErrorCode](capi-ability-runtime-common-h.md#abilityruntime_errorcode) | 返回执行结果。<br>ABILITY_RUNTIME_ERROR_CODE_NO_ERROR - 查询成功。<br>ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID - 入参buffer或者writeLength为空,或者缓冲区大小小于需要写入的大小。<br>ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST - 应用上下文不存在,如在应用创建的[子进程](capi-childprocess.md)中应用级别上下文不存在。 | 192 193### OH_AbilityRuntime_ApplicationContextGetPreferencesDir() 194 195``` 196AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetPreferencesDir(char* buffer, const int32_t bufferSize, int32_t* writeLength) 197``` 198 199**描述** 200 201获取本应用的应用级的首选项文件目录。 202 203**起始版本:** 16 204 205**参数:** 206 207| 参数项 | 描述 | 208| -- | -- | 209| char* buffer | 指向缓冲区的指针,用于接收应用包名。 | 210| const int32_t bufferSize | 缓冲区大小,单位为字节。 | 211| int32_t* writeLength | 在返回[ABILITY_RUNTIME_ERROR_CODE_NO_ERROR](capi-ability-runtime-common-h.md#abilityruntime_errorcode)时,表示实际写入到缓冲区的字符串长度,单位为字节。 | 212 213**返回:** 214 215| 类型 | 说明 | 216| -- | -- | 217| [AbilityRuntime_ErrorCode](capi-ability-runtime-common-h.md#abilityruntime_errorcode) | 返回执行结果。<br>ABILITY_RUNTIME_ERROR_CODE_NO_ERROR - 查询成功。<br>ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID - 入参buffer或者writeLength为空,或者缓冲区大小小于需要写入的大小。<br>ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST - 应用上下文不存在,如在应用创建的[子进程](capi-childprocess.md)中应用级别上下文不存在。 | 218 219### OH_AbilityRuntime_ApplicationContextGetBundleCodeDir() 220 221``` 222AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetBundleCodeDir(char* buffer, const int32_t bufferSize, int32_t* writeLength) 223``` 224 225**描述** 226 227获取本应用的应用级的安装文件目录。 228 229**起始版本:** 16 230 231**参数:** 232 233| 参数项 | 描述 | 234| -- | -- | 235| char* buffer | 指向缓冲区的指针,用于接收应用包名。 | 236| const int32_t bufferSize | 缓冲区大小,单位为字节。 | 237| int32_t* writeLength | 在返回[ABILITY_RUNTIME_ERROR_CODE_NO_ERROR](capi-ability-runtime-common-h.md#abilityruntime_errorcode)时,表示实际写入到缓冲区的字符串长度,单位为字节。 | 238 239**返回:** 240 241| 类型 | 说明 | 242| -- | -- | 243| [AbilityRuntime_ErrorCode](capi-ability-runtime-common-h.md#abilityruntime_errorcode) | 返回执行结果。<br>ABILITY_RUNTIME_ERROR_CODE_NO_ERROR - 查询成功。<br>ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID - 入参buffer或者writeLength为空,或者缓冲区大小小于需要写入的大小。<br>ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST - 应用上下文不存在,如在应用创建的[子进程](capi-childprocess.md)中应用级别上下文不存在。 | 244 245### OH_AbilityRuntime_ApplicationContextGetDistributedFilesDir() 246 247``` 248AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetDistributedFilesDir(char* buffer, const int32_t bufferSize, int32_t* writeLength) 249``` 250 251**描述** 252 253获取本应用的应用级的分布式文件目录。 254 255**起始版本:** 16 256 257**参数:** 258 259| 参数项 | 描述 | 260| -- | -- | 261| char* buffer | 指向缓冲区的指针,用于接收应用包名。 | 262| const int32_t bufferSize | 缓冲区大小,单位为字节。 | 263| int32_t* writeLength | 在返回[ABILITY_RUNTIME_ERROR_CODE_NO_ERROR](capi-ability-runtime-common-h.md#abilityruntime_errorcode)时,表示实际写入到缓冲区的字符串长度,单位为字节。 | 264 265**返回:** 266 267| 类型 | 说明 | 268| -- | -- | 269| [AbilityRuntime_ErrorCode](capi-ability-runtime-common-h.md#abilityruntime_errorcode) | 返回执行结果。<br>ABILITY_RUNTIME_ERROR_CODE_NO_ERROR - 查询成功。<br>ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID - 入参buffer或者writeLength为空,或者缓冲区大小小于需要写入的大小。<br>ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST - 应用上下文不存在,如在应用创建的[子进程](capi-childprocess.md)中应用级别上下文不存在。 | 270 271### OH_AbilityRuntime_ApplicationContextGetCloudFileDir() 272 273``` 274AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetCloudFileDir(char* buffer, const int32_t bufferSize, int32_t* writeLength) 275``` 276 277**描述** 278 279获取本应用的应用级的云文件目录。 280 281**起始版本:** 16 282 283**参数:** 284 285| 参数项 | 描述 | 286| -- | -- | 287| char* buffer | 指向缓冲区的指针,用于接收应用包名。 | 288| const int32_t bufferSize | 缓冲区大小,单位为字节。 | 289| int32_t* writeLength | 在返回[ABILITY_RUNTIME_ERROR_CODE_NO_ERROR](capi-ability-runtime-common-h.md#abilityruntime_errorcode)时,表示实际写入到缓冲区的字符串长度,单位为字节。 | 290 291**返回:** 292 293| 类型 | 说明 | 294| -- | -- | 295| [AbilityRuntime_ErrorCode](capi-ability-runtime-common-h.md#abilityruntime_errorcode) | 返回执行结果。<br>ABILITY_RUNTIME_ERROR_CODE_NO_ERROR - 查询成功。<br>ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID - 入参buffer或者writeLength为空,或者缓冲区大小小于需要写入的大小。<br>ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST - 应用上下文不存在,如在应用创建的[子进程](capi-childprocess.md)中应用级别上下文不存在。 | 296 297### OH_AbilityRuntime_ApplicationContextGetResourceDir() 298 299``` 300AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetResourceDir(const char* moduleName, char* buffer, const int32_t bufferSize, int32_t* writeLength) 301``` 302 303**描述** 304 305获取本应用的应用级的资源目录。 306 307**起始版本:** 20 308 309**参数:** 310 311| 参数项 | 描述 | 312| -- | -- | 313| char* moduleName | 模块名。 | 314| char* buffer | 指向缓冲区的指针,用于接收应用包名。 | 315| int32_t bufferSize | 缓冲区大小,单位为字节。 | 316| int32_t* writeLength | 在返回[ABILITY_RUNTIME_ERROR_CODE_NO_ERROR](capi-ability-runtime-common-h.md#abilityruntime_errorcode)时,表示实际写入到缓冲区的字符串长度,单位为字节。 | 317 318**返回:** 319 320| 类型 | 说明 | 321| -- | -- | 322| [AbilityRuntime_ErrorCode](capi-ability-runtime-common-h.md#abilityruntime_errorcode) | 返回执行结果。<br>ABILITY_RUNTIME_ERROR_CODE_NO_ERROR - 查询成功。<br>ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID - 入参buffer或者writeLength为空,或者缓冲区大小小于需要写入的大小。<br>ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST - 应用上下文不存在,如在应用创建的[子进程](capi-childprocess.md)中应用级别上下文不存在。 | 323 324### OH_AbilityRuntime_StartSelfUIAbility() 325 326``` 327AbilityRuntime_ErrorCode OH_AbilityRuntime_StartSelfUIAbility(AbilityBase_Want *want) 328``` 329 330**描述** 331 332启动当前应用的UIAbility。 333 334 335**需要权限:** ohos.permission.NDK_START_SELF_UI_ABILITY 336 337**起始版本:** 15 338 339**设备行为差异**:该接口仅在2in1和Tablet设备中可正常调用,在其他设备中返回ABILITY_RUNTIME_ERROR_CODE_NOT_SUPPORTED错误码。 340 341**参数:** 342 343| 参数项 | 描述 | 344| -- | -- | 345| [AbilityBase_Want](capi-abilitybase-want.md) *want | 启动当前应用UIAbility时需要的Want信息。 | 346 347**返回:** 348 349| 类型 | 说明 | 350| -- | -- | 351| [AbilityRuntime_ErrorCode](capi-ability-runtime-common-h.md#abilityruntime_errorcode) | ABILITY_RUNTIME_ERROR_CODE_NO_ERROR - 接口调用成功。<br>ABILITY_RUNTIME_ERROR_CODE_PERMISSION_DENIED - 调用方权限校验失败。<br>ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID - 调用方入参校验失败。<br>ABILITY_RUNTIME_ERROR_CODE_NOT_SUPPORTED - 设备类型不支持。<br>ABILITY_RUNTIME_ERROR_CODE_NO_SUCH_ABILITY - 指定的Ability名称不存在。<br>ABILITY_RUNTIME_ERROR_CODE_INCORRECT_ABILITY_TYPE - 接口调用Ability类型错误。<br>ABILITY_RUNTIME_ERROR_CODE_CROWDTEST_EXPIRED - 众测应用到期。<br>ABILITY_RUNTIME_ERROR_CODE_WUKONG_MODE - Wukong模式,不允许启动/停止Ability。<br>ABILITY_RUNTIME_ERROR_CODE_CONTROLLED - 应用被管控。<br>ABILITY_RUNTIME_ERROR_CODE_EDM_CONTROLLED - 应用被EDM管控。<br>ABILITY_RUNTIME_ERROR_CODE_CROSS_APP - 限制API 11以上版本三方应用跳转。<br>ABILITY_RUNTIME_ERROR_CODE_INTERNAL - 内部错误。<br>ABILITY_RUNTIME_ERROR_CODE_NOT_TOP_ABILITY - 非顶层应用。<br>ABILITY_RUNTIME_ERROR_CODE_UPPER_LIMIT_REACHED - 应用多实例已达到上限(从API17开始)。<br>ABILITY_RUNTIME_ERROR_CODE_APP_INSTANCE_KEY_NOT_SUPPORTED - 不允许设置APP_INSTANCE_KEY(从API17开始)。<br>详细内容参考AbilityRuntime_ErrorCode。 | 352 353**示例代码:** 354 355```cpp 356#include <AbilityKit/ability_base/want.h> 357#include <AbilityKit/ability_runtime/application_context.h> 358 359void startSelfUIAbilityTest() 360{ 361 AbilityBase_Element element; 362 element.abilityName = const_cast<char*>("EntryAbility"); 363 element.bundleName = const_cast<char*>("com.example.myapplication"); 364 element.moduleName = const_cast<char*>("entry"); 365 AbilityBase_Want* want = OH_AbilityBase_CreateWant(element); 366 367 AbilityRuntime_ErrorCode err = OH_AbilityRuntime_StartSelfUIAbility(want); 368 if (err != ABILITY_RUNTIME_ERROR_CODE_NO_ERROR) { 369 // 记录错误日志以及其他业务处理 370 return; 371 } 372 // 销毁want,防止内存泄漏 373 OH_AbilityBase_DestroyWant(want); 374} 375``` 376 377### OH_AbilityRuntime_StartSelfUIAbilityWithStartOptions() 378 379``` 380AbilityRuntime_ErrorCode OH_AbilityRuntime_StartSelfUIAbilityWithStartOptions(AbilityBase_Want *want,AbilityRuntime_StartOptions *options) 381``` 382 383**描述** 384 385通过StartOptions启动当前应用的UIAbility。 386 387**需要权限:** ohos.permission.NDK_START_SELF_UI_ABILITY 388 389**起始版本:** 17 390 391**设备行为差异**:该接口仅在2in1和Tablet设备中可正常调用,在其他设备中返回ABILITY_RUNTIME_ERROR_CODE_NOT_SUPPORTED错误码。 392 393**参数:** 394 395| 参数项 | 描述 | 396| -- | -- | 397| [AbilityBase_Want](capi-abilitybase-want.md) *want | 启动当前应用UIAbility时需要的Want信息。 | 398| [AbilityRuntime_StartOptions](capi-abilityruntime-startoptions.md) *options | 启动当前应用UIAbility时需要的StartOptions信息。如果该参数中[startVisibility](capi-context-constant-h.md#abilityruntime_startvisibility)属性的值不为空,必须确保当前应用已添加到状态栏,否则会返回[ABILITY_RUNTIME_ERROR_VISIBILITY_SETTING_DISABLED](capi-ability-runtime-common-h.md#abilityruntime_errorcode)错误码。 | 399 400**返回:** 401 402| 类型 | 说明 | 403| -- | -- | 404| [AbilityRuntime_ErrorCode](capi-ability-runtime-common-h.md#abilityruntime_errorcode) | ABILITY_RUNTIME_ERROR_CODE_NO_ERROR - 接口调用成功。<br>ABILITY_RUNTIME_ERROR_CODE_PERMISSION_DENIED - 调用方权限校验失败。<br>ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID - 调用方入参校验失败。<br>ABILITY_RUNTIME_ERROR_CODE_NOT_SUPPORTED - 设备类型不支持。<br>ABILITY_RUNTIME_ERROR_CODE_NO_SUCH_ABILITY - 指定的Ability名称不存在。<br>ABILITY_RUNTIME_ERROR_CODE_INCORRECT_ABILITY_TYPE - 接口调用Ability类型错误。<br>ABILITY_RUNTIME_ERROR_CODE_CROWDTEST_EXPIRED - 众测应用到期。<br>ABILITY_RUNTIME_ERROR_CODE_WUKONG_MODE - Wukong模式,不允许启动/停止Ability。<br>ABILITY_RUNTIME_ERROR_CODE_CONTROLLED - 应用被管控。<br>ABILITY_RUNTIME_ERROR_CODE_EDM_CONTROLLED - 应用被EDM管控。<br>ABILITY_RUNTIME_ERROR_CODE_CROSS_APP - 限制API 11以上版本三方应用跳转。<br>ABILITY_RUNTIME_ERROR_CODE_INTERNAL - 内部错误。<br>ABILITY_RUNTIME_ERROR_CODE_NOT_TOP_ABILITY - 非顶层应用。<br>ABILITY_RUNTIME_ERROR_VISIBILITY_SETTING_DISABLED - 不允许设置窗口启动可见性。<br>ABILITY_RUNTIME_ERROR_CODE_MULTI_APP_NOT_SUPPORTED - 不支持应用分身和多实例。<br>ABILITY_RUNTIME_ERROR_CODE_INVALID_APP_INSTANCE_KEY - 无效多实例。<br> ABILITY_RUNTIME_ERROR_CODE_UPPER_LIMIT_REACHED - 应用多实例以达到上限。<br>ABILITY_RUNTIME_ERROR_MULTI_INSTANCE_NOT_SUPPORTED - 不支持应用多实例。<br>ABILITY_RUNTIME_ERROR_CODE_APP_INSTANCE_KEY_NOT_SUPPORTED - 不允许设置APP_INSTANCE_KEY。<br>详细内容参考AbilityRuntime_ErrorCode。 | 405 406**示例代码:** 407 408```cpp 409#include <AbilityKit/ability_base/want.h> 410#include <AbilityKit/ability_runtime/application_context.h> 411 412void demo() 413{ 414 AbilityBase_Element element; 415 element.abilityName = const_cast<char*>("EntryAbility"); 416 element.bundleName = const_cast<char*>("com.example.myapplication"); 417 element.moduleName = const_cast<char*>("entry"); 418 AbilityBase_Want* want = OH_AbilityBase_CreateWant(element); 419 if (want == nullptr) { 420 // 记录错误日志以及其他业务处理 421 return; 422 } 423 424 AbilityRuntime_StartOptions* options = OH_AbilityRuntime_CreateStartOptions(); 425 if (options == nullptr) { 426 // 记录错误日志以及其他业务处理 427 428 // 销毁want,防止内存泄漏 429 OH_AbilityBase_DestroyWant(want); 430 return; 431 } 432 AbilityRuntime_ErrorCode err = OH_AbilityRuntime_StartSelfUIAbilityWithStartOptions(want, options); 433 if (err != ABILITY_RUNTIME_ERROR_CODE_NO_ERROR) { 434 // 记录错误日志以及其他业务处理 435 } 436 // 销毁want,防止内存泄漏 437 OH_AbilityBase_DestroyWant(want); 438 439 // 销毁options,防止内存泄漏 440 OH_AbilityRuntime_DestroyStartOptions(&options); 441} 442``` 443