OpenHarmony应用开发-FA模型切换Stage模型指导-API切换
版本:v3.2 Beta5
API切换概述
FA模型和Stage模型由于线程模型和进程模型的差异,部分接口仅在FA模型下才能使用,针对这部分接口在SDK的接口中有FAModelOnly的标记,用于提醒开发者这部分接口仅能在FA模型下使用。因此在切换到Stage模型时,需要将应用中用到的FAModelOnly接口替换成Stage模型下对应的接口。下面是startAbility的接口切换示例,全量接口列表请查看后续章节:
startAbility接口由FA模型切换到Stage模型的示例:
- FA模型示例
import fa from '@ohos.ability.featureAbility';
let parameter = {
"want": {
bundleName: "ohos.samples.demo",
abilityName: "ohos.samples.demo.MainAbility"
}
}
fa.startAbility(parameter).then((data) => {
console.info('startAbility success');
}).catch((error) => {
console.error('startAbility failed.');
})
- Stage示例示例
// context为Ability对象的成员,在非Ability对象内部调用需要
// 将Context对象传递过去
let wantInfo = {
bundleName: "ohos.samples.demo",
abilityName: "ohos.samples.demo.MainAbility"
};
this.context.startAbility(wantInfo).then((data) => {
console.info('startAbility success.');
}).catch((error) => {
console.error('startAbility failed.');
})
Context接口切换
featureAbility接口切换
particleAbility接口切换
LifecycleForm接口切换
FA模型接口 | Stage模型接口对应d.ts文件 | Stage模型对应接口 |
onCreate?(want: Want): formBindingData.FormBindingData; | @ohos.app.form.FormExtensionAbility.d.ts | |
onCastToNormal?(formId: string): void; | @ohos.app.form.FormExtensionAbility.d.ts | |
onUpdate?(formId: string): void; | @ohos.app.form.FormExtensionAbility.d.ts | |
onVisibilityChange?(newStatus: { [key: string]: number }): void; | @ohos.app.form.FormExtensionAbility.d.ts | onChangeFormVisibility(newStatus: { [key: string]: number }): void; |
onEvent?(formId: string, message: string): void; | @ohos.app.form.FormExtensionAbility.d.ts | |
onDestroy?(formId: string): void; | @ohos.app.form.FormExtensionAbility.d.ts | |
onAcquireFormState?(want: Want): formInfo.FormState; | @ohos.app.form.FormExtensionAbility.d.ts | |
onShare?(formId: string): {[key: string]: any}; | @ohos.app.form.FormExtensionAbility.d.ts |
LifecycleApp接口切换
FA模型接口 | Stage模型接口对应d.ts文件 | Stage模型对应接口 |
onShow?(): void; | @ohos.window.d.ts | on(eventType: ‘windowStageEvent’, callback: Callback<WindowStageEventType>): void;监听FOREGROUND切到前台状态 |
onHide?(): void; | @ohos.window.d.ts | on(eventType: ‘windowStageEvent’, callback: Callback<WindowStageEventType>): void;监听BACKGROUND切到后台状态 |
onDestroy?(): void; | @ohos.app.ability.UIAbility.d.ts | |
onCreate?(): void; | @ohos.app.ability.UIAbility.d.ts | onCreate(want: Want, param: AbilityConstant.LaunchParam): void; |
onWindowDisplayModeChanged?(isShownInMultiWindow: boolean, newConfig: resourceManager.Configuration): void; | Stage模型无对应接口 | 暂时未提供对应接口 |
onStartContinuation?(): boolean; | Stage模型无对应接口 | Stage模型上,应用无需感知迁移是否成功(由应用发起迁移请求时感知),onStartContinuation废弃 |
onSaveData?(data: Object): boolean; | @ohos.app.ability.UIAbility.d.ts | onContinue(wantParam : {[key: string]: any}): AbilityConstant.OnContinueResult; |
onCompleteContinuation?(result: number): void; | application\ContinueCallback.d.ts | |
onRestoreData?(data: Object): void; | @ohos.app.ability.UIAbility.d.ts | onCreate(want: Want, param: AbilityConstant.LaunchParam): void;onNewWant(want: Want, launchParams: AbilityConstant.LaunchParam): void; 标准实例模式Ability迁移目标端在onCreate回调中完成数据恢复,单实例应用迁移目标端在onCreate回调中完成数据恢复,回调中通过判断launchParam.launchReason可获取迁移启动的场景,从而可以从Want中获取迁移前保存的数据 |
onRemoteTerminated?(): void; | application\ContinueCallback.d.ts | |
onSaveAbilityState?(outState: PacMap): void; | @ohos.app.ability.UIAbility.d.ts | |
onRestoreAbilityState?(inState: PacMap): void; | @ohos.app.ability.UIAbility.d.ts | onCreate(want: Want, param: AbilityConstant.LaunchParam): void;应用重启后会触发Ability的onCreate方法,通过判断launchParam.launchReason可获取自恢复的场景,从而可以从Want中获取重启前保存的数据 |
onInactive?(): void; | @ohos.app.ability.UIAbility.d.ts | |
onActive?(): void; | @ohos.app.ability.UIAbility.d.ts | |
onNewWant?(want: Want): void; | @ohos.app.ability.UIAbility.d.ts | onNewWant(want: Want, launchParams: AbilityConstant.LaunchParam): void; |
onMemoryLevel?(level: number): void | @ohos.app.ability.UIAbility.d.ts | onMemoryLevel(level: AbilityConstant.MemoryLevel): void; |
LifecycleService接口切换
FA模型接口 | Stage模型接口对应d.ts文件 | Stage模型对应接口 |
onStart?(): void; | @ohos.app.ability.ServiceExtensionAbility.d.ts | |
onCommand?(want: Want, startId: number): void; | @ohos.app.ability.ServiceExtensionAbility.d.ts | |
onStop?(): void; | @ohos.app.ability.ServiceExtensionAbility.d.ts | |
onConnect?(want: Want): rpc.RemoteObject; | @ohos.app.ability.ServiceExtensionAbility.d.ts | |
onDisconnect?(want: Want): void; | @ohos.app.ability.ServiceExtensionAbility.d.ts | |
onReconnect?(want: Want): void; | @ohos.app.ability.ServiceExtensionAbility.d.ts |
LifecycleData接口切换
FA模型接口 | Stage模型接口对应d.ts文件 | Stage模型对应接口 |
update?(uri: string, valueBucket: rdb.ValuesBucket, predicates: dataAbility.DataAbilityPredicates, callback: AsyncCallback<number>): void; | @ohos.application.DataShareExtensionAbility.d.ts | |
query?(uri: string, columns: Array<string>, predicates: dataAbility.DataAbilityPredicates, callback: AsyncCallback<ResultSet>): void; | @ohos.application.DataShareExtensionAbility.d.ts | |
delete?(uri: string, predicates: dataAbility.DataAbilityPredicates, callback: AsyncCallback<number>): void; | @ohos.application.DataShareExtensionAbility.d.ts | |
normalizeUri?(uri: string, callback: AsyncCallback<string>): void; | @ohos.application.DataShareExtensionAbility.d.ts | normalizeUri?(uri: string, callback: AsyncCallback<string>): void; |
batchInsert?(uri: string, valueBuckets: Array<rdb.ValuesBucket>, callback: AsyncCallback<number>): void; | @ohos.application.DataShareExtensionAbility.d.ts | |
denormalizeUri?(uri: string, callback: AsyncCallback<string>): void; | @ohos.application.DataShareExtensionAbility.d.ts | denormalizeUri?(uri: string, callback: AsyncCallback<string>): void; |
insert?(uri: string, valueBucket: rdb.ValuesBucket, callback: AsyncCallback<number>): void; | @ohos.application.DataShareExtensionAbility.d.ts | insert?(uri: string, valueBucket: ValuesBucket, callback: AsyncCallback<number>): void; |
openFile?(uri: string, mode: string, callback: AsyncCallback<number>): void; | Stage模型无对应接口 | Stage模型不支持uri跨进程访问,建议通过want携带fd和文件信息进行跨进程文件访问 |
getFileTypes?(uri: string, mimeTypeFilter: string, callback: AsyncCallback<Array<string>>): void; | Stage模型无对应接口 | Stage模型不支持uri跨进程访问,建议通过want携带fd和文件信息进行跨进程文件访问 |
onInitialized?(info: AbilityInfo): void; | @ohos.application.DataShareExtensionAbility.d.ts | onCreate?(want: Want, callback: AsyncCallback<void>): void; |
getType?(uri: string, callback: AsyncCallback<string>): void; | Stage模型无对应接口 | Stage模型不支持uri跨进程访问,建议通过want携带fd和文件信息进行跨进程文件访问 |
executeBatch?(ops: Array<DataAbilityOperation>, callback: AsyncCallback<Array<DataAbilityResult>>): void; | Stage模型无对应接口 | 暂时未提供对应接口 |
call?(method: string, arg: string, extras: PacMap, callback: AsyncCallback<PacMap>): void; | Stage模型无对应接口 | 暂时未提供对应接口 |
DataAbilityHelper接口切换
mediaLibrary接口切换
FA模型接口 | Stage模型接口对应d.ts文件 | Stage模型对应接口 |
@ohos.multimedia.mediaLibrary.d.ts |
request接口切换
resourceManager接口切换
window接口切换
FA模型接口 | Stage模型接口对应d.ts文件 | Stage模型对应接口 |
@ohos.window.d.ts | createSubWindow(name: string, callback: AsyncCallback<Window>): void;createSubWindow(name: string): Promise; FA模型应用通过window.create(id, WindowType.TYPE_APP)接口创建应用子窗口,Stage模型应用可使用WindowStage.CreateSubWindow()接口代替 | |
create(id: string, type: WindowType, callback: AsyncCallback<Window>): void;create(id: string, type: WindowType): Promise<Window>; | @ohos.window.d.ts | createWindow(config: Configuration, callback: AsyncCallback<Window>): void;createWindow(config: Configuration): Promise<Window>; |
getTopWindow(callback: AsyncCallback<Window>): void;getTopWindow(): Promise<Window>; | @ohos.window.d.ts | getLastWindow(ctx: BaseContext, callback: AsyncCallback<Window>): void;getLastWindow(ctx: BaseContext): Promise<Window>; |
Storage接口切换
FA模型接口 | Stage模型接口对应d.ts文件 | Stage模型对应接口 |
GetStorageOptions | Stage模型无对应接口 | Storage接口功能使用Prefereces接口来代替,接口入参已经重新设计 |
SetStorageOptions | Stage模型无对应接口 | Storage接口功能使用Prefereces接口来代替,接口入参已经重新设计 |
ClearStorageOptions | Stage模型无对应接口 | Storage接口功能使用Prefereces接口来代替,接口入参已经重新设计 |
DeleteStorageOptions | Stage模型无对应接口 | Storage接口功能使用Prefereces接口来代替,接口入参已经重新设计 |
@ohos.data.preferences.d.ts | get(key: string, defValue: ValueType, callback: AsyncCallback<ValueType>): void;get(key: string, defValue: ValueType): Promise<ValueType>; | |
@ohos.data.preferences.d.ts | put(key: string, value: ValueType, callback: AsyncCallback<void>): void;put(key: string, value: ValueType): Promise<void>; | |
@ohos.data.preferences.d.ts | clear(callback: AsyncCallback<void>): void;clear(): Promise<void>; | |
@ohos.data.preferences.d.ts | delete(key: string, callback: AsyncCallback<void>): void;delete(key: string): Promise<void>; |