未验证 提交 51ff2a3b 编写于 作者: O openharmony_ci 提交者: Gitee

!23577 元能力ArlTS整改补充

Merge pull request !23577 from liuliu/dfx
...@@ -9,32 +9,32 @@ ...@@ -9,32 +9,32 @@
```ts ```ts
import featureAbility from '@ohos.ability.featureAbility'; import featureAbility from '@ohos.ability.featureAbility';
import dataAbility from '@ohos.data.dataAbility'; import dataAbility from '@ohos.data.dataAbility';
import relationalStore from '@ohos.data.relationalStore';
import Want from '@ohos.app.ability.Want'; import Want from '@ohos.app.ability.Want';
import common from '@ohos.app.ability.common';
import { AsyncCallback, BusinessError } from '@ohos.base'; import { AsyncCallback, BusinessError } from '@ohos.base';
import rdb from '@ohos.data.rdb'; import rdb from '@ohos.data.rdb';
const TABLE_NAME = 'book' const TABLE_NAME = 'book'
const STORE_CONFIG: relationalStore.StoreConfig = { name: 'book.db',securityLevel: 1 } const STORE_CONFIG: rdb.StoreConfig = { name: 'book.db'}
const SQL_CREATE_TABLE = 'CREATE TABLE IF NOT EXISTS book(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, introduction TEXT NOT NULL)' const SQL_CREATE_TABLE = 'CREATE TABLE IF NOT EXISTS book(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, introduction TEXT NOT NULL)'
let rdbStore: relationalStore.RdbStore | undefined = undefined let rdbStore: rdb.RdbStore | undefined = undefined
export default class DataAbility { export default class DataAbility {
onInitialized(want: Want) { onInitialized(want: Want) {
console.info('DataAbility onInitialized, abilityInfo:' + want.bundleName) console.info('DataAbility onInitialized, abilityInfo:' + want.bundleName)
let context = featureAbility.getContext() let context: common.BaseContext = {stageMode: featureAbility.getContext().stageMode}
relationalStore.getRdbStore(context, STORE_CONFIG, (err, store) => { rdb.getRdbStore(context, STORE_CONFIG, 1, (err, store) => {
console.info('DataAbility getRdbStore callback') console.info('DataAbility getRdbStore callback')
store.executeSql(SQL_CREATE_TABLE, []) store.executeSql(SQL_CREATE_TABLE, [])
rdbStore = store rdbStore = store
}); });
}; }
insert(uri: string, valueBucket: rdb.ValuesBucket, callback: AsyncCallback<number>) { insert(uri: string, valueBucket: rdb.ValuesBucket, callback: AsyncCallback<number>) {
console.info('DataAbility insert start') console.info('DataAbility insert start')
if (rdbStore) { if (rdbStore) {
rdbStore.insert(TABLE_NAME, valueBucket, callback) rdbStore.insert(TABLE_NAME, valueBucket, callback)
} }
}; }
batchInsert(uri: string, valueBuckets: Array<rdb.ValuesBucket>, callback: AsyncCallback<number>) { batchInsert(uri: string, valueBuckets: Array<rdb.ValuesBucket>, callback: AsyncCallback<number>) {
console.info('DataAbility batch insert start') console.info('DataAbility batch insert start')
if (rdbStore) { if (rdbStore) {
...@@ -49,15 +49,15 @@ export default class DataAbility { ...@@ -49,15 +49,15 @@ export default class DataAbility {
} }
} }
} }
}; }
query(uri: string, columns: Array<string>, predicates: dataAbility.DataAbilityPredicates, query(uri: string, columns: Array<string>, predicates: dataAbility.DataAbilityPredicates,
callback: AsyncCallback<relationalStore.ResultSet>) { callback: AsyncCallback<rdb.ResultSet>) {
console.info('DataAbility query start') console.info('DataAbility query start')
let rdbPredicates = dataAbility.createRdbPredicates(TABLE_NAME, predicates) let rdbPredicates = dataAbility.createRdbPredicates(TABLE_NAME, predicates)
if (rdbStore) { if (rdbStore) {
rdbStore.query(rdbPredicates, columns, callback) rdbStore.query(rdbPredicates, columns, callback)
} }
}; }
update(uri: string, valueBucket: rdb.ValuesBucket, predicates: dataAbility.DataAbilityPredicates, update(uri: string, valueBucket: rdb.ValuesBucket, predicates: dataAbility.DataAbilityPredicates,
callback: AsyncCallback<number>) { callback: AsyncCallback<number>) {
console.info('DataAbilityupdate start') console.info('DataAbilityupdate start')
...@@ -65,13 +65,13 @@ export default class DataAbility { ...@@ -65,13 +65,13 @@ export default class DataAbility {
if (rdbStore) { if (rdbStore) {
rdbStore.update(valueBucket, rdbPredicates, callback) rdbStore.update(valueBucket, rdbPredicates, callback)
} }
}; }
delete(uri: string, predicates: dataAbility.DataAbilityPredicates, callback: AsyncCallback<number>) { delete(uri: string, predicates: dataAbility.DataAbilityPredicates, callback: AsyncCallback<number>) {
console.info('DataAbilitydelete start') console.info('DataAbilitydelete start')
let rdbPredicates = dataAbility.createRdbPredicates(TABLE_NAME, predicates) let rdbPredicates = dataAbility.createRdbPredicates(TABLE_NAME, predicates)
if (rdbStore) { if (rdbStore) {
rdbStore.delete(rdbPredicates, callback) rdbStore.delete(rdbPredicates, callback)
} }
}; }
}; }
``` ```
...@@ -128,8 +128,13 @@ import AbilityConstant from '@ohos.app.ability.AbilityConstant'; ...@@ -128,8 +128,13 @@ import AbilityConstant from '@ohos.app.ability.AbilityConstant';
- 定义和注册[ErrorObserver](../reference/apis/js-apis-inner-application-errorObserver.md) callback,具体可参考[errorManager](../reference/apis/js-apis-app-ability-errorManager.md)里的使用方法。 - 定义和注册[ErrorObserver](../reference/apis/js-apis-inner-application-errorObserver.md) callback,具体可参考[errorManager](../reference/apis/js-apis-app-ability-errorManager.md)里的使用方法。
```ts ```ts
var registerId = -1; import appRecovery from '@ohos.app.ability.appRecovery';
var callback = { import errorManager from '@ohos.app.ability.errorManager';
import UIAbility from '@ohos.app.ability.UIAbility';
import window from '@ohos.window';
let registerId = -1;
let callback: errorManager.ErrorObserver = {
onUnhandledException(errMsg) { onUnhandledException(errMsg) {
console.log(errMsg); console.log(errMsg);
appRecovery.saveAppState(); appRecovery.saveAppState();
...@@ -137,15 +142,20 @@ import AbilityConstant from '@ohos.app.ability.AbilityConstant'; ...@@ -137,15 +142,20 @@ import AbilityConstant from '@ohos.app.ability.AbilityConstant';
} }
} }
onWindowStageCreate(windowStage) { export default class EntryAbility extends UIAbility {
onWindowStageCreate(windowStage: window.WindowStage) {
// Main window is created, set main page for this ability // Main window is created, set main page for this ability
console.log("[Demo] EntryAbility onWindowStageCreate") console.log("[Demo] EntryAbility onWindowStageCreate")
globalThis.registerObserver = (() => {
registerId = errorManager.on('error', callback); registerId = errorManager.on('error', callback);
})
windowStage.loadContent("pages/index", null); windowStage.loadContent("pages/index", (err, data) => {
if (err.code) {
console.error('Failed to load the content. Cause:' + JSON.stringify(err));
return;
}
console.info('Succeeded in loading the content. Data: ' + JSON.stringify(data))
})
}
} }
``` ```
...@@ -154,12 +164,17 @@ import AbilityConstant from '@ohos.app.ability.AbilityConstant'; ...@@ -154,12 +164,17 @@ import AbilityConstant from '@ohos.app.ability.AbilityConstant';
callback触发appRecovery.saveAppState()调用后,会触发EntryAbility的onSaveState(state, wantParams)函数回调。 callback触发appRecovery.saveAppState()调用后,会触发EntryAbility的onSaveState(state, wantParams)函数回调。
```ts ```ts
onSaveState(state, wantParams) { import AbilityConstant from '@ohos.app.ability.AbilityConstant';
import UIAbility from '@ohos.app.ability.UIAbility';
export default class EntryAbility extends UIAbility {
onSaveState(state:AbilityConstant.StateType, wantParams: Record<string, Object>) {
// Ability has called to save app data // Ability has called to save app data
console.log("[Demo] EntryAbility onSaveState") console.log("[Demo] EntryAbility onSaveState")
wantParams["myData"] = "my1234567"; wantParams["myData"] = "my1234567";
return AbilityConstant.OnSaveResult.ALL_AGREE; return AbilityConstant.OnSaveResult.ALL_AGREE;
} }
}
``` ```
- 数据恢复 - 数据恢复
...@@ -167,31 +182,47 @@ callback触发appRecovery.saveAppState()调用后,会触发EntryAbility的onSa ...@@ -167,31 +182,47 @@ callback触发appRecovery.saveAppState()调用后,会触发EntryAbility的onSa
callback触发后appRecovery.restartApp()调用后,应用会重启,重启后会走到EntryAbility的onCreate(want, launchParam)函数,保存的数据会在want参数的parameters里。 callback触发后appRecovery.restartApp()调用后,应用会重启,重启后会走到EntryAbility的onCreate(want, launchParam)函数,保存的数据会在want参数的parameters里。
```ts ```ts
storage: LocalStorage import AbilityConstant from '@ohos.app.ability.AbilityConstant';
onCreate(want, launchParam) { import UIAbility from '@ohos.app.ability.UIAbility';
import Want from '@ohos.app.ability.Want';
let abilityWant: Want;
export default class EntryAbility extends UIAbility {
storage: LocalStorage | undefined = undefined;
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
console.log("[Demo] EntryAbility onCreate") console.log("[Demo] EntryAbility onCreate")
globalThis.abilityWant = want; abilityWant = want;
if (launchParam.launchReason == AbilityConstant.LaunchReason.APP_RECOVERY) { if (launchParam.launchReason == AbilityConstant.LaunchReason.APP_RECOVERY) {
this.storage = new LocalStorage(); this.storage = new LocalStorage();
if (want.parameters) {
let recoveryData = want.parameters["myData"]; let recoveryData = want.parameters["myData"];
this.storage.setOrCreate("myData", recoveryData); this.storage.setOrCreate("myData", recoveryData);
this.context.restoreWindowStage(this.storage); this.context.restoreWindowStage(this.storage);
} }
}
}
} }
``` ```
- 取消注册ErrorObserver callback - 取消注册ErrorObserver callback
```ts ```ts
onWindowStageDestroy() { import errorManager from '@ohos.app.ability.errorManager';
import UIAbility from '@ohos.app.ability.UIAbility';
let registerId = -1;
export default class EntryAbility extends UIAbility {
onWindowStageDestroy() {
// Main window is destroyed, release UI related resources // Main window is destroyed, release UI related resources
console.log("[Demo] EntryAbility onWindowStageDestroy") console.log("[Demo] EntryAbility onWindowStageDestroy")
globalThis.unRegisterObserver = (() => {
errorManager.off('error', registerId, (err) => { errorManager.off('error', registerId, (err) => {
console.error("[Demo] err:", err); console.error("[Demo] err:", err);
}); });
}) }
} }
``` ```
...@@ -200,20 +231,28 @@ onWindowStageDestroy() { ...@@ -200,20 +231,28 @@ onWindowStageDestroy() {
被动保存和恢复依赖恢复框架底层触发,无需注册监听ErrorObserver callback,只需实现Ability的onSaveState接口数据保存和onCreate接口数据恢复流程即可。 被动保存和恢复依赖恢复框架底层触发,无需注册监听ErrorObserver callback,只需实现Ability的onSaveState接口数据保存和onCreate接口数据恢复流程即可。
```ts ```ts
export default class EntryAbility extends Ability { import AbilityConstant from '@ohos.app.ability.AbilityConstant';
storage: LocalStorage import UIAbility from '@ohos.app.ability.UIAbility';
onCreate(want, launchParam) { import Want from '@ohos.app.ability.Want';
let abilityWant: Want;
export default class EntryAbility extends UIAbility {
storage: LocalStorage | undefined = undefined
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
console.log("[Demo] EntryAbility onCreate") console.log("[Demo] EntryAbility onCreate")
globalThis.abilityWant = want; abilityWant = want;
if (launchParam.launchReason == AbilityConstant.LaunchReason.APP_RECOVERY) { if (launchParam.launchReason == AbilityConstant.LaunchReason.APP_RECOVERY) {
this.storage = new LocalStorage(); this.storage = new LocalStorage();
if (want.parameters) {
let recoveryData = want.parameters["myData"]; let recoveryData = want.parameters["myData"];
this.storage.setOrCreate("myData", recoveryData); this.storage.setOrCreate("myData", recoveryData);
this.context.restoreWindowStage(this.storage); this.context.restoreWindowStage(this.storage);
} }
} }
}
onSaveState(state, wantParams) { onSaveState(state:AbilityConstant.StateType, wantParams: Record<string, Object>) {
// Ability has called to save app data // Ability has called to save app data
console.log("[Demo] EntryAbility onSaveState") console.log("[Demo] EntryAbility onSaveState")
wantParams["myData"] = "my1234567"; wantParams["myData"] = "my1234567";
...@@ -227,10 +266,16 @@ export default class EntryAbility extends Ability { ...@@ -227,10 +266,16 @@ export default class EntryAbility extends Ability {
发生故障的Ability再次重新启动时,在调度onCreate生命周期里,参数want的parameters成员会有[ABILITY_RECOVERY_RESTART](../reference/apis/js-apis-app-ability-wantConstant.md#wantconstantparams)标记数据,并且值为true。 发生故障的Ability再次重新启动时,在调度onCreate生命周期里,参数want的parameters成员会有[ABILITY_RECOVERY_RESTART](../reference/apis/js-apis-app-ability-wantConstant.md#wantconstantparams)标记数据,并且值为true。
```ts ```ts
import AbilityConstant from '@ohos.app.ability.AbilityConstant';
import UIAbility from '@ohos.app.ability.UIAbility'; import UIAbility from '@ohos.app.ability.UIAbility';
import Want from '@ohos.app.ability.Want';
import wantConstant from '@ohos.app.ability.wantConstant'; import wantConstant from '@ohos.app.ability.wantConstant';
export default class EntryAbility extends UIAbility { export default class EntryAbility extends UIAbility {
onCreate(want, launchParam) { onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
if (want.parameters === undefined) {
return;
}
if (want.parameters[wantConstant.Params.ABILITY_RECOVERY_RESTART] != undefined && if (want.parameters[wantConstant.Params.ABILITY_RECOVERY_RESTART] != undefined &&
want.parameters[wantConstant.Params.ABILITY_RECOVERY_RESTART] == true) { want.parameters[wantConstant.Params.ABILITY_RECOVERY_RESTART] == true) {
console.log("This ability need to recovery"); console.log("This ability need to recovery");
......
...@@ -38,14 +38,17 @@ ...@@ -38,14 +38,17 @@
## 开发示例 ## 开发示例
```ts ```ts
import UIAbility from '@ohos.app.ability.UIAbility'; import UIAbility from '@ohos.app.ability.UIAbility';
import AbilityConstant from '@ohos.app.ability.AbilityConstant';
import errorManager from '@ohos.app.ability.errorManager'; import errorManager from '@ohos.app.ability.errorManager';
import Want from '@ohos.app.ability.Want';
import window from '@ohos.window';
let registerId = -1; let registerId = -1;
let callback = { let callback: errorManager.ErrorObserver = {
onUnhandledException: function (errMsg) { onUnhandledException: (errMsg) => {
console.log(errMsg); console.log(errMsg);
}, },
onException: function (errorObj) { onException: (errorObj) => {
console.log('onException, name: ', errorObj.name); console.log('onException, name: ', errorObj.name);
console.log('onException, message: ', errorObj.message); console.log('onException, message: ', errorObj.message);
if (typeof(errorObj.stack) === 'string') { if (typeof(errorObj.stack) === 'string') {
...@@ -53,12 +56,13 @@ let callback = { ...@@ -53,12 +56,13 @@ let callback = {
} }
} }
} }
let abilityWant: Want;
export default class EntryAbility extends UIAbility { export default class EntryAbility extends UIAbility {
onCreate(want, launchParam) { onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
console.log("[Demo] EntryAbility onCreate") console.log("[Demo] EntryAbility onCreate")
registerId = errorManager.on("error", callback); registerId = errorManager.on("error", callback);
globalThis.abilityWant = want; abilityWant = want;
} }
onDestroy() { onDestroy() {
...@@ -68,7 +72,7 @@ export default class EntryAbility extends UIAbility { ...@@ -68,7 +72,7 @@ export default class EntryAbility extends UIAbility {
}); });
} }
onWindowStageCreate(windowStage) { onWindowStageCreate(windowStage: window.WindowStage) {
// Main window is created, set main page for this ability // Main window is created, set main page for this ability
console.log("[Demo] EntryAbility onWindowStageCreate") console.log("[Demo] EntryAbility onWindowStageCreate")
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册