AbilityDelegator
AbilityDelegator提供添加用于監視指定能力的生命周期狀態更改的AbilityMonitor對象的能力,包括對AbilityMonitor實例的添加、刪除、等待ability到達OnCreate生命周期、設置等待時間等、獲取指定ability的生命周期狀態、獲取當前應用頂部ability、啟動指定ability等。
說明:
開發前請熟悉鴻蒙開發指導文檔:[gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md
]
本模塊首批接口從API version 8開始支持。后續版本的新增接口,采用上角標單獨標記接口的起始版本。
使用說明
通過AbilityDelegatorRegistry中getAbilityDelegator方法獲取。
import AbilityDelegatorRegistry from '@ohos.application.abilityDelegatorRegistry'
var abilityDelegator;
abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
AbilityDelegator
addAbilityMonitor9+
addAbilityMonitor(monitor: AbilityMonitor, callback: AsyncCallback): void
添加AbilityMonitor實例(callback形式)
**系統能力:**SystemCapability.Ability.AbilityRuntime.Core
參數:
參數名 | 類型 | 必填說明 | 說明 |
---|---|---|---|
monitor | [AbilityMonitor] | 是 | [AbilityMonitor]實例 |
callback | AsyncCallback | 是 | 表示指定的回調方法 |
示例:
var abilityDelegator;
function onAbilityCreateCallback(data) {
console.info("onAbilityCreateCallback");
}
var monitor = {
abilityName: "abilityname",
onAbilityCreate: onAbilityCreateCallback
}
abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.addAbilityMonitor(monitor, (err : any) = > {
console.info("addAbilityMonitor callback");
});
addAbilityMonitor9+
addAbilityMonitor(monitor: AbilityMonitor): Promise
添加AbilityMonitor實例(promise形式)
系統能力: SystemCapability.Ability.AbilityRuntime.Core
參數:
參數名 | 類型 | 必填 | 說明 |
---|---|---|---|
monitor | [AbilityMonitor] | 是 | [AbilityMonitor]實例 |
返回值:
類型 | 說明 |
---|---|
Promise | 以Promise形式返回。 |
示例:
var abilityDelegator;
function onAbilityCreateCallback(data) {
console.info("onAbilityCreateCallback");
}
var monitor = {
abilityName: "abilityname",
onAbilityCreate: onAbilityCreateCallback
}
abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.addAbilityMonitor(monitor).then(() = > {
console.info("addAbilityMonitor promise");
});
removeAbilityMonitor9+
removeAbilityMonitor(monitor: AbilityMonitor, callback: AsyncCallback): void
刪除已經添加的AbilityMonitor實例(callback形式)
系統能力: SystemCapability.Ability.AbilityRuntime.Core
參數:
參數名 | 類型 | 必填 | 說明 |
---|---|---|---|
monitor | [AbilityMonitor] | 是 | [AbilityMonitor]實例 |
callback | AsyncCallback | 是 | 表示指定的回調方法 |
示例:
var abilityDelegator;
function onAbilityCreateCallback(data) {
console.info("onAbilityCreateCallback");
}
var monitor = {
abilityName: "abilityname",
onAbilityCreate: onAbilityCreateCallback
}
abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.removeAbilityMonitor(monitor, (err : any) = > {
console.info("removeAbilityMonitor callback");
});
removeAbilityMonitor9+
removeAbilityMonitor(monitor: AbilityMonitor): Promise
刪除已經添加的AbilityMonitor實例(promise形式)
系統能力: SystemCapability.Ability.AbilityRuntime.Core
參數:
名稱 | 類型 | 必填 | 說明 |
---|---|---|---|
monitor | [AbilityMonitor] | 是 | [AbilityMonitor]實例 |
返回值:
類型 | 說明 |
---|---|
Promise | 以Promise形式返回。 |
- 示例
var abilityDelegator;
function onAbilityCreateCallback(data) {
console.info("onAbilityCreateCallback");
}
var monitor = {
abilityName: "abilityname",
onAbilityCreate: onAbilityCreateCallback
}
abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.removeAbilityMonitor(monitor).then(() = > {
console.info("removeAbilityMonitor promise");
});
waitAbilityMonitor9+
waitAbilityMonitor(monitor: AbilityMonitor, callback: AsyncCallback): void
等待與AbilityMonitor實例匹配的ability到達OnCreate生命周期,并返回ability實例(callback形式)
系統能力: SystemCapability.Ability.AbilityRuntime.Core
參數:
參數名 | 類型 | 必填 | 說明 |
---|---|---|---|
monitor | [AbilityMonitor] | 是 | [AbilityMonitor]實例 |
callback | AsyncCallback[Ability] | 是 | 表示指定的回調方法 |
示例:
var abilityDelegator;
function onAbilityCreateCallback(data) {
console.info("onAbilityCreateCallback");
}
var monitor = {
abilityName: "abilityname",
onAbilityCreate: onAbilityCreateCallback
}
abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.waitAbilityMonitor(monitor, (err : any, data : any) = > {
console.info("waitAbilityMonitor callback");
});
waitAbilityMonitor9+
waitAbilityMonitor(monitor: AbilityMonitor, timeout: number, callback: AsyncCallback): void
設置等待時間,等待與AbilityMonitor實例匹配的ability到達OnCreate生命周期,并返回ability實例(callback形式)
系統能力: SystemCapability.Ability.AbilityRuntime.Core
參數:
參數名 | 類型 | 必填 | 說明 |
---|---|---|---|
monitor | [AbilityMonitor] | 是 | [AbilityMonitor]實例 |
timeout | number | 是 | 最大等待時間,單位毫秒(ms) |
callback | AsyncCallback[Ability] | 是 | 表示指定的回調方法 |
示例:
var abilityDelegator;
var timeout = 100;
function onAbilityCreateCallback(data) {
console.info("onAbilityCreateCallback");
}
var monitor = {
abilityName: "abilityname",
onAbilityCreate: onAbilityCreateCallback
}
abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.waitAbilityMonitor(monitor, timeout, (err : any, data : any) = > {
console.info("waitAbilityMonitor callback");
});
waitAbilityMonitor9+
waitAbilityMonitor(monitor: AbilityMonitor, timeout?: number): Promise
設置等待時間,等待與AbilityMonitor實例匹配的ability到達OnCreate生命周期,并返回ability實例(promise形式)
系統能力: SystemCapability.Ability.AbilityRuntime.Core
參數:
參數名 | 類型 | 必填 | 說明 |
---|---|---|---|
monitor | [AbilityMonitor] | 是 | [AbilityMonitor]實例 |
timeout | number | 否 | 最大等待時間,單位毫秒(ms) |
返回值:
類型 | 說明 |
---|---|
Promise[Ability] | 以Promise形式返回Ability。 |
示例:
var abilityDelegator;
function onAbilityCreateCallback(data) {
console.info("onAbilityCreateCallback");
}
var monitor = {
abilityName: "abilityname",
onAbilityCreate: onAbilityCreateCallback
}
abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.waitAbilityMonitor(monitor).then((data : any) = > {
console.info("waitAbilityMonitor promise");
});
getAppContext9+
getAppContext(): Context
獲取應用Context
系統能力: SystemCapability.Ability.AbilityRuntime.Core
返回值:
類型 | 說明 |
---|---|
[Context] | 應用[Context]。 |
示例:
var abilityDelegator;
abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
var context = abilityDelegator.getAppContext();
getAbilityState9+
getAbilityState(ability: Ability): number
獲取指定ability的生命周期狀態
系統能力: SystemCapability.Ability.AbilityRuntime.Core
參數:
參數名 | 類型 | 必填 | 說明 |
---|---|---|---|
ability | [Ability] | 是 | 指定Ability對象 |
返回值:
類型 | 說明 |
---|---|
number | 指定ability的生命周期狀態。狀態枚舉值使用[AbilityLifecycleState]。 |
示例:
var abilityDelegator;
var ability;
abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.getCurrentTopAbility((err : any, data : any) = > {
console.info("getCurrentTopAbility callback");
ability = data;
var state = abilityDelegator.getAbilityState(ability);
console.info("getAbilityState" + state);
});
getCurrentTopAbility9+
getCurrentTopAbility(callback: AsyncCallback): void
獲取當前應用頂部ability(callback形式)
系統能力: SystemCapability.Ability.AbilityRuntime.Core
參數:
參數名 | 類型 | 必填 | 說明 |
---|---|---|---|
callback | AsyncCallback[Ability] | 是 | 表示指定的回調方法 |
示例:
var abilityDelegator;
var ability;
abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.getCurrentTopAbility((err : any, data : any) = > {
console.info("getCurrentTopAbility callback");
ability = data;
});
getCurrentTopAbility9+
getCurrentTopAbility(): Promise
獲取當前應用頂部ability(promise形式)
系統能力: SystemCapability.Ability.AbilityRuntime.Core
返回值:
類型 | 說明 |
---|---|
Promise[Ability] | 以Promise形式返回當前應用頂部ability。 |
示例:
var abilityDelegator;
var ability;
abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.getCurrentTopAbility().then((data : any) = > {
console.info("getCurrentTopAbility promise");
ability = data;
});
startAbility9+
startAbility(want: Want, callback: AsyncCallback): void
啟動指定ability(callback形式)
系統能力: SystemCapability.Ability.AbilityRuntime.Core
參數:
參數名 | 類型 | 必填 | 說明 |
---|---|---|---|
want | [Want] | 是 | 啟動Ability參數 |
callback | AsyncCallback | 是 | 表示指定的回調方法 |
示例:
var abilityDelegator;
var want = {
bundleName: "bundleName",
abilityName: "abilityName"
};
abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.startAbility(want, (err : any, data : any) = > {
console.info("startAbility callback");
});
startAbility9+
startAbility(want: Want): Promise
啟動指定ability(promise形式)
系統能力: SystemCapability.Ability.AbilityRuntime.Core
參數:
參數名 | 類型 | 必填 | 說明 |
---|---|---|---|
want | [Want] | 是 | 啟動Ability參數 |
返回值:
類型 | 說明 |
---|---|
Promise | 以Promise形式返回。 |
示例:
var abilityDelegator;
var want = {
bundleName: "bundleName",
abilityName: "abilityName"
};
abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.startAbility(want).then((data: any) = > {
console.info("startAbility promise");
});
doAbilityForeground9+
doAbilityForeground(ability: Ability, callback: AsyncCallback): void
調度指定ability生命周期狀態到Foreground狀態(callback形式)
系統能力: SystemCapability.Ability.AbilityRuntime.Core
參數:
參數名 | 類型 | 必填 | 說明 |
---|---|---|---|
ability | Ability | 是 | 指定Ability對象 |
callback | AsyncCallback | 是 | 表示指定的回調方法 - true:成功 - false:失敗 |
示例:
var abilityDelegator;
var ability;
abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.getCurrentTopAbility((err : any, data : any) = > {
console.info("getCurrentTopAbility callback");
ability = data;
abilityDelegator.doAbilityForeground(ability, (err : any, data : any) = > {
console.info("doAbilityForeground callback");
});
});
doAbilityForeground9+
doAbilityForeground(ability: Ability): Promise
調度指定ability生命周期狀態到Foreground狀態(promise形式)
系統能力: SystemCapability.Ability.AbilityRuntime.Core
參數:
參數名 | 類型 | 必填 | 說明 |
---|---|---|---|
ability | Ability | 是 | 指定Ability對象 |
返回值:
類型 | 說明 |
---|---|
Promise | 以Promise形式返回執行結果。 - true:成功 - false:失敗 |
示例:
var abilityDelegator;
var ability;
abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.getCurrentTopAbility((err : any, data : any) = > {
console.info("getCurrentTopAbility callback");
ability = data;
abilityDelegator.doAbilityForeground(ability).then((data : any) = > {
console.info("doAbilityForeground promise");
});
});
doAbilityBackground9+
doAbilityBackground(ability: Ability, callback: AsyncCallback): void
調度指定ability生命周期狀態到Background狀態(callback形式)
系統能力: SystemCapability.Ability.AbilityRuntime.Core
參數:
參數名 | 類型 | 必填 | 說明 |
---|---|---|---|
ability | Ability | 是 | 指定Ability對象 |
callback | AsyncCallback | 是 | 表示指定的回調方法 - true:成功 - false:失敗 |
示例:
var abilityDelegator;
var ability;
abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.getCurrentTopAbility((err : any, data : any) = > {
console.info("getCurrentTopAbility callback");
ability = data;
abilityDelegator.doAbilityBackground(ability, (err : any, data : any) = > {
console.info("doAbilityBackground callback");
});
});
doAbilityBackground9+
doAbilityBackground(ability: Ability): Promise
調度指定ability生命周期狀態到Background狀態(promise形式)
系統能力: SystemCapability.Ability.AbilityRuntime.Core
參數:
參數名 | 類型 | 必填 | 說明 |
---|---|---|---|
ability | Ability | 是 | 指定Ability對象 |
返回值:
類型 | 說明 |
---|---|
Promise | 以Promise形式返回執行結果。 - true:成功 - false:失敗 |
示例:
var abilityDelegator;
var ability;
abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.getCurrentTopAbility((err : any, data : any) = > {
console.info("getCurrentTopAbility callback");
ability = data;
abilityDelegator.doAbilityBackground(ability).then((data : any) = > {
console.info("doAbilityBackground promise");
});
});
print(msg: string, callback: AsyncCallback): void
打印日志信息到單元測試終端控制臺(callback形式)
系統能力: SystemCapability.Ability.AbilityRuntime.Core
參數:
參數名 | 類型 | 必填 | 說明 |
---|---|---|---|
msg | string | 是 | 日志字符串 |
callback | AsyncCallback | 是 | 表示指定的回調方法 |
示例:
var abilityDelegator;
var msg = "msg";
abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.print(msg, (err : any) = > {
console.info("print callback");
});
print(msg: string): Promise
打印日志信息到單元測試終端控制臺(promise形式)
系統能力: SystemCapability.Ability.AbilityRuntime.Core
參數:
參數名 | 類型 | 必填 | 說明 |
---|---|---|---|
msg | string | 是 | 日志字符串 |
返回值:
類型 | 說明 |
---|---|
Promise | 以Promise形式返回。 |
示例:
var abilityDelegator;
var msg = "msg";
abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.print(msg).then(() = > {
console.info("print promise");
});
executeShellCommand
executeShellCommand(cmd: string, callback: AsyncCallback): void
執行指定的shell命令(callback形式)
系統能力: SystemCapability.Ability.AbilityRuntime.Core
參數:
參數名 | 類型 | 必填 | 說明 |
---|---|---|---|
cmd | string | 是 | shell命令字符串 |
callback | AsyncCallback[ShellCmdResult] | 是 | 表示指定的回調方法 |
示例:
var abilityDelegator;
var cmd = "cmd";
abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.executeShellCommand(cmd, (err : any, data : any) = > {
console.info("executeShellCommand callback");
});
executeShellCommand
executeShellCommand(cmd: string, timeoutSecs: number, callback: AsyncCallback): void
指定超時時間,并執行指定的shell命令(callback形式)
系統能力: SystemCapability.Ability.AbilityRuntime.Core
參數:
參數名 | 類型 | 必填 | 說明 |
---|---|---|---|
cmd | string | 是 | shell命令字符串 |
timeoutSecs | number | 是 | 設定命令超時時間,單位秒(s) |
callback | AsyncCallback[ShellCmdResult] | 是 | 表示指定的回調方法 |
示例:
var abilityDelegator;
var cmd = "cmd";
var timeout = 100;
abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.executeShellCommand(cmd, timeout, (err : any, data : any) = > {
console.info("executeShellCommand callback");
});
executeShellCommand
executeShellCommand(cmd: string, timeoutSecs?: number): Promise
指定超時時間,并執行指定的shell命令(promise形式)
系統能力: SystemCapability.Ability.AbilityRuntime.Core
參數:
參數名 | 類型 | 必填 | 說明 |
---|---|---|---|
cmd | string | 是 | shell命令字符串 |
timeoutSecs | number | 否 | 設定命令超時時間,單位秒(s) |
返回值:
類型 | 說明 |
---|---|
Promise[ShellCmdResult] | 以Promise形式返回Shell命令執行結果[ShellCmdResult]對象。 |
示例:
var abilityDelegator;
var cmd = "cmd";
var timeout = 100;
abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.executeShellCommand(cmd, timeout).then((data : any) = > {
console.info("executeShellCommand promise");
});
finishTest9+
finishTest(msg: string, code: number, callback: AsyncCallback): void
結束測試并打印日志信息到單元測試終端控制臺(callback形式)
系統能力: SystemCapability.Ability.AbilityRuntime.Core
參數:
參數名 | 類型 | 必填 | 說明 |
---|---|---|---|
msg | string | 是 | 日志字符串 |
code | number | 是 | 日志碼 |
callback | AsyncCallback | 是 | 表示指定的回調方法 |
示例:
var abilityDelegator;
var msg = "msg";
abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.finishTest(msg, 0, (err : any) = > {
console.info("finishTest callback");
});
finishTest9+
finishTest(msg: string, code: number): Promise
結束測試并打印日志信息到單元測試終端控制臺(promise形式)
系統能力: SystemCapability.Ability.AbilityRuntime.Core
參數:
參數名 | 類型 | 必填 | 說明 |
---|---|---|---|
msg | string | 是 | 日志字符串 |
code | number | 是 | 日志碼 |
返回值:
類型 | 說明 |
---|---|
Promise | 以Promise形式返回。 |
示例:
var abilityDelegator;
var msg = "msg";
abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.finishTest(msg, 0).then(() = > {
console.info("finishTest promise");
});
審核編輯 黃宇
-
接口
+關注
關注
33文章
8598瀏覽量
151164 -
框架
+關注
關注
0文章
403瀏覽量
17489 -
鴻蒙
+關注
關注
57文章
2352瀏覽量
42859
發布評論請先 登錄
相關推薦
評論