W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
UIAbility是包含UI界面的應(yīng)用組件,提供組件創(chuàng)建、銷毀、前后臺切換等生命周期回調(diào),同時也具備組件協(xié)同的能力,組件協(xié)同主要提供如下常用功能:
本模塊首批接口從API version 9 開始支持。后續(xù)版本的新增接口,采用上角標單獨標記接口的起始版本。
本模塊接口僅可在Stage模型下使用。
系統(tǒng)能力:以下各項對應(yīng)的系統(tǒng)能力均為SystemCapability.Ability.AbilityRuntime.AbilityCore
名稱 | 類型 | 可讀 | 可寫 | 說明 |
---|---|---|---|---|
context | UIAbilityContext | 是 | 否 | 上下文。 |
launchWant | Want | 是 | 否 | UIAbility啟動時的參數(shù)。 |
lastRequestWant | Want | 是 | 否 | UIAbility最后請求時的參數(shù)。 |
callee | Callee | 是 | 否 | 調(diào)用Stub(樁)服務(wù)對象。 |
onCreate(want: Want, param: AbilityConstant.LaunchParam): void;
Ability創(chuàng)建時回調(diào),執(zhí)行初始化業(yè)務(wù)邏輯操作。
系統(tǒng)能力:SystemCapability.Ability.AbilityRuntime.AbilityCore
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
want | Want | 是 | 當前UIAbility的Want類型信息,包括ability名稱、bundle名稱等。 |
param | AbilityConstant.LaunchParam | 是 | 創(chuàng)建UIAbility、上次異常退出的原因信息。 |
示例:
- class myAbility extends Ability {
- onCreate(want, param) {
- console.log('onCreate, want:' + want.abilityName);
- }
- }
onWindowStageCreate(windowStage: window.WindowStage): void
當WindowStage創(chuàng)建后調(diào)用。
系統(tǒng)能力:SystemCapability.Ability.AbilityRuntime.AbilityCore
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
windowStage | window.WindowStage | 是 | WindowStage相關(guān)信息。 |
示例:
- class myAbility extends Ability {
- onWindowStageCreate(windowStage) {
- console.log('onWindowStageCreate');
- }
- }
onWindowStageDestroy(): void
當WindowStage銷毀后調(diào)用。
系統(tǒng)能力:SystemCapability.Ability.AbilityRuntime.AbilityCore
示例:
- class myAbility extends Ability {
- onWindowStageDestroy() {
- console.log('onWindowStageDestroy');
- }
- }
onWindowStageRestore(windowStage: window.WindowStage): void
當遷移多實例ability時,恢復(fù)WindowStage后調(diào)用。
系統(tǒng)能力:SystemCapability.Ability.AbilityRuntime.AbilityCore
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
windowStage | window.WindowStage | 是 | WindowStage相關(guān)信息。 |
示例:
- class myAbility extends Ability {
- onWindowStageRestore(windowStage) {
- console.log('onWindowStageRestore');
- }
- }
onDestroy(): void | Promise<void>;
Ability生命周期回調(diào),在銷毀時回調(diào),執(zhí)行資源清理等操作。
系統(tǒng)能力:SystemCapability.Ability.AbilityRuntime.AbilityCore
示例:
- class myAbility extends Ability {
- onDestroy() {
- console.log('onDestroy');
- }
- }
onForeground(): void;
Ability生命周期回調(diào),當應(yīng)用從后臺轉(zhuǎn)到前臺時觸發(fā)。
系統(tǒng)能力:SystemCapability.Ability.AbilityRuntime.AbilityCore
示例:
- class myAbility extends Ability {
- onForeground() {
- console.log('onForeground');
- }
- }
onBackground(): void;
Ability生命周期回調(diào),當應(yīng)用從前臺轉(zhuǎn)到后臺時觸發(fā)。
系統(tǒng)能力:SystemCapability.Ability.AbilityRuntime.AbilityCore
示例:
- class myAbility extends Ability {
- onBackground() {
- console.log('onBackground');
- }
- }
onContinue(wantParam: { [key: string]: Object }): AbilityConstant.OnContinueResult;
當ability遷移準備遷移時觸發(fā),保存數(shù)據(jù)。
系統(tǒng)能力:SystemCapability.Ability.AbilityRuntime.AbilityCore
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
wantParam | {[key: string]: any} | 是 | want相關(guān)參數(shù)。 |
返回值:
類型 | 說明 |
---|---|
AbilityConstant.OnContinueResult | 繼續(xù)的結(jié)果。 |
示例:
- import AbilityConstant from '@ohos.app.ability.AbilityConstant';
- class MyUIAbility extends Ability {
- onContinue(wantParams) {
- console.log('onContinue');
- wantParams['myData'] = 'my1234567';
- return AbilityConstant.OnContinueResult.AGREE;
- }
- }
onNewWant(want: Want, launchParams: AbilityConstant.LaunchParam): void;
當傳入新的Want,ability再次被拉起時會回調(diào)執(zhí)行該方法。
系統(tǒng)能力:SystemCapability.Ability.AbilityRuntime.AbilityCore
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
want | Want | 是 | Want類型參數(shù),如ability名稱,包名等。 |
launchParams | AbilityConstant.LaunchParam | 是 | UIAbility啟動的原因、上次異常退出的原因信息。 |
示例:
- class MyUIAbility extends Ability {
- onNewWant(want, launchParams) {
- console.log('onNewWant, want:' + want.abilityName);
- console.log('onNewWant, launchParams:' + JSON.stringify(launchParams));
- }
- }
onDump(params: Array<string>): Array<string>;
轉(zhuǎn)儲客戶端信息時調(diào)用。
系統(tǒng)能力:SystemCapability.Ability.AbilityRuntime.AbilityCore
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
params | Array<string> | 是 | 表示命令形式的參數(shù)。 |
示例:
- class myAbility extends Ability {
- onDump(params) {
- console.log('dump, params:' + JSON.stringify(params));
- return ['params'];
- }
- }
onSaveState(reason: AbilityConstant.StateType, wantParam : {[key: string]: Object}): AbilityConstant.OnSaveResult;
該API配合appRecovery使用。在應(yīng)用故障時,如果使能了自動保存狀態(tài),框架將回調(diào)onSaveState保存Ability狀態(tài)。
系統(tǒng)能力:SystemCapability.Ability.AbilityRuntime.AbilityCore
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
reason | AbilityConstant.StateType | 是 | 回調(diào)保存狀態(tài)的原因。 |
wantParam | {[key: string]: any} | 是 | want相關(guān)參數(shù)。 |
返回值:
類型 | 說明 |
---|---|
AbilityConstant.OnSaveResult | 是否同意保存當前Ability的狀態(tài)。 |
示例:
- import AbilityConstant from '@ohos.app.ability.AbilityConstant';
- class MyUIAbility extends Ability {
- onSaveState(reason, wantParam) {
- console.log('onSaveState');
- wantParam['myData'] = 'my1234567';
- return AbilityConstant.OnSaveResult.RECOVERY_AGREE;
- }
- }
call(method: string, data: rpc.Parcelable): Promise<void>;
向通用組件服務(wù)端發(fā)送約定序列化數(shù)據(jù)。
系統(tǒng)能力:SystemCapability.Ability.AbilityRuntime.AbilityCore
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
method | string | 是 | 約定的服務(wù)端注冊事件字符串。 |
data | rpc.Parcelable | 是 | 由開發(fā)者實現(xiàn)的Parcelable可序列化數(shù)據(jù)。 |
返回值:
類型 | 說明 |
---|---|
Promise<void> | Promise形式返回應(yīng)答。 |
錯誤碼:
錯誤碼ID | 錯誤信息 |
---|---|
401 | If the input parameter is not valid parameter. |
其他ID見元能力子系統(tǒng)錯誤碼 |
示例:
- class MyMessageAble{ // 自定義的Parcelable數(shù)據(jù)結(jié)構(gòu)
- name:''
- str:''
- num: 1
- constructor(name, str) {
- this.name = name;
- this.str = str;
- }
- marshalling(messageSequence) {
- messageSequence.writeInt(this.num);
- messageSequence.writeString(this.str);
- console.log('MyMessageAble marshalling num[' + this.num + '] str[' + this.str + ']');
- return true;
- }
- unmarshalling(messageSequence) {
- this.num = messageSequence.readInt();
- this.str = messageSequence.readString();
- console.log('MyMessageAble unmarshalling num[' + this.num + '] str[' + this.str + ']');
- return true;
- }
- };
- let method = 'call_Function'; // 約定的通知消息字符串
- let caller;
- export default class MainAbility extends Ability {
- onWindowStageCreate(windowStage) {
- this.context.startAbilityByCall({
- bundleName: 'com.example.myservice',
- abilityName: 'MainAbility',
- deviceId: ''
- }).then((obj) => {
- caller = obj;
- let msg = new MyMessageAble('msg', 'world'); // 參考Parcelable數(shù)據(jù)定義
- caller.call(method, msg)
- .then(() => {
- console.log('Caller call() called');
- })
- .catch((callErr) => {
- console.log('Caller.call catch error, error.code: ' + JSON.stringify(callErr.code) +
- ' error.message: ' + JSON.stringify(callErr.message));
- });
- }).catch((err) => {
- console.log('Caller GetCaller error, error.code: ' + JSON.stringify(err.code) +
- ' error.message: ' + JSON.stringify(err.message));
- });
- }
- }
callWithResult(method: string, data: rpc.Parcelable): Promise<rpc.MessageSequence>;
向通用組件服務(wù)端發(fā)送約定序列化數(shù)據(jù), 并將服務(wù)端返回的約定序列化數(shù)據(jù)帶回。
系統(tǒng)能力:SystemCapability.Ability.AbilityRuntime.AbilityCore
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
method | string | 是 | 約定的服務(wù)端注冊事件字符串。 |
data | rpc.Parcelable | 是 | 由開發(fā)者實現(xiàn)的Parcelable可序列化數(shù)據(jù)。 |
返回值:
類型 | 說明 |
---|---|
Promise<rpc.MessageSequence> | Promise形式返回通用組件服務(wù)端應(yīng)答數(shù)據(jù)。 |
錯誤碼:
錯誤碼ID | 錯誤信息 |
---|---|
401 | If the input parameter is not valid parameter. |
其他ID見元能力子系統(tǒng)錯誤碼 |
示例:
- class MyMessageAble{
- name:''
- str:''
- num: 1
- constructor(name, str) {
- this.name = name;
- this.str = str;
- }
- marshalling(messageSequence) {
- messageSequence.writeInt(this.num);
- messageSequence.writeString(this.str);
- console.log('MyMessageAble marshalling num[' + this.num + '] str[' + this.str + ']');
- return true;
- }
- unmarshalling(messageSequence) {
- this.num = messageSequence.readInt();
- this.str = messageSequence.readString();
- console.log('MyMessageAble unmarshalling num[' + this.num + '] str[' + this.str + ']');
- return true;
- }
- };
- let method = 'call_Function';
- let caller;
- export default class MainAbility extends Ability {
- onWindowStageCreate(windowStage) {
- this.context.startAbilityByCall({
- bundleName: 'com.example.myservice',
- abilityName: 'MainAbility',
- deviceId: ''
- }).then((obj) => {
- caller = obj;
- let msg = new MyMessageAble(1, 'world');
- caller.callWithResult(method, msg)
- .then((data) => {
- console.log('Caller callWithResult() called');
- let retmsg = new MyMessageAble(0, '');
- data.readParcelable(retmsg);
- })
- .catch((callErr) => {
- console.log('Caller.callWithResult catch error, error.code: ' + JSON.stringify(callErr.code) +
- ' error.message: ' + JSON.stringify(callErr.message));
- });
- }).catch((err) => {
- console.log('Caller GetCaller error, error.code: ' + JSON.stringify(err.code) +
- ' error.message: ' + JSON.stringify(err.message));
- });
- }
- }
release(): void;
主動釋放通用組件服務(wù)端的通信接口。
系統(tǒng)能力:SystemCapability.UIAbility.UIAbilityRuntime.UIAbilityCore
錯誤碼:
錯誤碼ID | 錯誤信息 |
---|---|
401 | Invalid input parameter. |
16200001 | Caller released. The caller has been released. |
16200002 | Callee invalid. The callee does not exist. |
16000050 | Internal Error. |
示例:
- let caller;
- export default class MainAbility extends Ability {
- onWindowStageCreate(windowStage) {
- this.context.startAbilityByCall({
- bundleName: 'com.example.myservice',
- abilityName: 'MainAbility',
- deviceId: ''
- }).then((obj) => {
- caller = obj;
- try {
- caller.release();
- } catch (releaseErr) {
- console.log('Caller.release catch error, error.code: ' + JSON.stringify(releaseErr.code) +
- ' error.message: ' + JSON.stringify(releaseErr.message));
- }
- }).catch((err) => {
- console.log('Caller GetCaller error, error.code: ' + JSON.stringify(err.code) +
- ' error.message: ' + JSON.stringify(err.message));
- });
- }
- }
onRelease(callback: OnReleaseCallback): void;
注冊通用組件服務(wù)端Stub(樁)斷開監(jiān)聽通知。
系統(tǒng)能力:SystemCapability.Ability.AbilityRuntime.AbilityCore
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
callback | OnReleaseCallback | 是 | 返回onRelease回調(diào)結(jié)果。 |
示例:
- let caller;
- export default class MainAbility extends Ability {
- onWindowStageCreate(windowStage) {
- this.context.startAbilityByCall({
- bundleName: 'com.example.myservice',
- abilityName: 'MainAbility',
- deviceId: ''
- }).then((obj) => {
- caller = obj;
- try {
- caller.onRelease((str) => {
- console.log(' Caller OnRelease CallBack is called ' + str);
- });
- } catch (error) {
- console.log('Caller.onRelease catch error, error.code: ' + JSON.stringify(error.code) +
- ' error.message: ' + JSON.stringify(error.message));
- }
- }).catch((err) => {
- console.log('Caller GetCaller error, error.code: ' + JSON.stringify(err.code) +
- ' error.message: ' + JSON.stringify(err.message));
- });
- }
- }
on(type: 'release', callback: OnReleaseCallback): void;
注冊通用組件服務(wù)端Stub(樁)斷開監(jiān)聽通知。
系統(tǒng)能力:SystemCapability.Ability.AbilityRuntime.AbilityCore
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
type | string | 是 | 監(jiān)聽releaseCall事件,固定為'release'。 |
callback | OnReleaseCallback | 是 | 返回onRelease回調(diào)結(jié)果。 |
錯誤碼:
錯誤碼ID | 錯誤信息 |
---|---|
401 | If the input parameter is not valid parameter. |
其他ID見元能力子系統(tǒng)錯誤碼 |
示例:
- let caller;
- export default class MainAbility extends Ability {
- onWindowStageCreate(windowStage) {
- this.context.startAbilityByCall({
- bundleName: 'com.example.myservice',
- abilityName: 'MainAbility',
- deviceId: ''
- }).then((obj) => {
- caller = obj;
- try {
- caller.on('release', (str) => {
- console.log(' Caller OnRelease CallBack is called ' + str);
- });
- } catch (error) {
- console.log('Caller.on catch error, error.code: ' + JSON.stringify(error.code) +
- ' error.message: ' + JSON.stringify(error.message));
- }
- }).catch((err) => {
- console.log('Caller GetCaller error, error.code: ' + JSON.stringify(err.code) +
- ' error.message: ' + JSON.stringify(err.message));
- });
- }
- }
off(type: 'release', callback: OnReleaseCallback): void;
取消注冊通用組件服務(wù)端Stub(樁)斷開監(jiān)聽通知。預(yù)留能力,當前暫未支持。
系統(tǒng)能力:SystemCapability.Ability.AbilityRuntime.AbilityCore
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
type | string | 是 | 監(jiān)聽releaseCall事件,固定為'release'。 |
callback | OnReleaseCallback | 是 | 返回off回調(diào)結(jié)果。 |
錯誤碼:
錯誤碼ID | 錯誤信息 |
---|---|
401 | If the input parameter is not valid parameter. |
其他ID見元能力子系統(tǒng)錯誤碼 |
示例:
- let caller;
- export default class MainUIAbility extends Ability {
- onWindowStageCreate(windowStage) {
- this.context.startAbilityByCall({
- bundleName: 'com.example.myservice',
- abilityName: 'MainUIAbility',
- deviceId: ''
- }).then((obj) => {
- caller = obj;
- try {
- let onReleaseCallBack = (str) => {
- console.log(' Caller OnRelease CallBack is called ' + str);
- };
- caller.on('release', onReleaseCallBack);
- caller.off('release', onReleaseCallBack);
- } catch (error) {
- console.log('Caller.on or Caller.off catch error, error.code: ' + JSON.stringify(error.code) +
- ' error.message: ' + JSON.stringify(error.message));
- }
- }).catch((err) => {
- console.log('Caller GetCaller error, error.code: ' + JSON.stringify(err.code) +
- ' error.message: ' + JSON.stringify(err.message));
- });
- }
- }
off(type: 'release'): void;
取消注冊通用組件服務(wù)端Stub(樁)斷開監(jiān)聽通知。預(yù)留能力,當前暫未支持。
系統(tǒng)能力:SystemCapability.Ability.AbilityRuntime.AbilityCore
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
type | string | 是 | 監(jiān)聽releaseCall事件,固定為'release'。 |
錯誤碼:
錯誤碼ID | 錯誤信息 |
---|---|
401 | If the input parameter is not valid parameter. |
其他ID見元能力子系統(tǒng)錯誤碼 |
示例:
- let caller;
- export default class MainUIAbility extends Ability {
- onWindowStageCreate(windowStage) {
- this.context.startAbilityByCall({
- bundleName: 'com.example.myservice',
- abilityName: 'MainUIAbility',
- deviceId: ''
- }).then((obj) => {
- caller = obj;
- try {
- let onReleaseCallBack = (str) => {
- console.log(' Caller OnRelease CallBack is called ' + str);
- };
- caller.on('release', onReleaseCallBack);
- caller.off('release');
- } catch (error) {
- console.error('Caller.on or Caller.off catch error, error.code: ' + JSON.stringify(error.code) +
- ' error.message: ' + JSON.stringify(error.message));
- }
- }).catch((err) => {
- console.error('Caller GetCaller error, error.code: ' + JSON.stringify(err.code) +
- ' error.message: ' + JSON.stringify(err.message));
- });
- }
- }
on(method: string, callback: CalleeCallback): void;
通用組件服務(wù)端注冊消息通知callback。
系統(tǒng)能力:SystemCapability.Ability.AbilityRuntime.AbilityCore
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
method | string | 是 | 與客戶端約定的通知消息字符串。 |
callback | CalleeCallback | 是 | 一個rpc.MessageSequence類型入?yún)⒌膉s通知同步回調(diào)函數(shù), 回調(diào)函數(shù)至少要返回一個空的rpc.Parcelable數(shù)據(jù)對象, 其他視為函數(shù)執(zhí)行錯誤。 |
錯誤碼:
錯誤碼ID | 錯誤信息 |
---|---|
401 | If the input parameter is not valid parameter. |
其他ID見元能力子系統(tǒng)錯誤碼 |
示例:
- class MyMessageAble{
- name:''
- str:''
- num: 1
- constructor(name, str) {
- this.name = name;
- this.str = str;
- }
- marshalling(messageSequence) {
- messageSequence.writeInt(this.num);
- messageSequence.writeString(this.str);
- console.log('MyMessageAble marshalling num[' + this.num + '] str[' + this.str + ']');
- return true;
- }
- unmarshalling(messageSequence) {
- this.num = messageSequence.readInt();
- this.str = messageSequence.readString();
- console.log('MyMessageAble unmarshalling num[' + this.num + '] str[' + this.str + ']');
- return true;
- }
- };
- let method = 'call_Function';
- function funcCallBack(pdata) {
- console.log('Callee funcCallBack is called ' + pdata);
- let msg = new MyMessageAble('test', '');
- pdata.readParcelable(msg);
- return new MyMessageAble('test1', 'Callee test');
- }
- export default class MainAbility extends Ability {
- onCreate(want, launchParam) {
- console.log('Callee onCreate is called');
- try {
- this.callee.on(method, funcCallBack);
- } catch (error) {
- console.log('Callee.on catch error, error.code: ' + JSON.stringify(error.code) +
- ' error.message: ' + JSON.stringify(error.message));
- }
- }
- }
off(method: string): void;
解除通用組件服務(wù)端注冊消息通知callback。
系統(tǒng)能力:SystemCapability.UIAbility.UIAbilityRuntime.UIAbilityCore
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
method | string | 是 | 已注冊的通知事件字符串。 |
錯誤碼:
錯誤碼ID | 錯誤信息 |
---|---|
401 | If the input parameter is not valid parameter. |
其他ID見元能力子系統(tǒng)錯誤碼 |
示例:
- let method = 'call_Function';
- export default class MainAbility extends Ability {
- onCreate(want, launchParam) {
- console.log('Callee onCreate is called');
- try {
- this.callee.off(method);
- } catch (error) {
- console.log('Callee.off catch error, error.code: ' + JSON.stringify(error.code) +
- ' error.message: ' + JSON.stringify(error.message));
- }
- }
- }
(msg: string): void;
系統(tǒng)能力:SystemCapability.Ability.AbilityRuntime.AbilityCore
名稱 | 可讀 | 可寫 | 類型 | 說明 |
---|---|---|---|---|
(msg: string) | 是 | 否 | function | 調(diào)用者注冊的偵聽器函數(shù)接口的原型。 |
(indata: rpc.MessageSequence): rpc.Parcelable;
系統(tǒng)能力:SystemCapability.Ability.AbilityRuntime.AbilityCore
名稱 | 可讀 | 可寫 | 類型 | 說明 |
---|---|---|---|---|
(indata: rpc.MessageSequence) | 是 | 否 | rpc.Parcelable | 被調(diào)用方注冊的消息偵聽器函數(shù)接口的原型。 |
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: