提交 d7ae8dcc 编写于 作者: zyjhandsome's avatar zyjhandsome

优化《@ohos.app.ability.appManager (appManager)》章节部分内容,修复'on'示例代码问题

Signed-off-by: zyjhandsome's avatarzyjhandsome <zyjhandsome@126.com>
上级 d7a488ad
......@@ -27,10 +27,10 @@ static isRunningInStabilityTest(callback: AsyncCallback&lt;boolean&gt;): void
|AsyncCallback&lt;boolean&gt; |以回调方式返回接口运行结果及当前是否处于稳定性测试场景,可进行错误处理或其他自定义处理。true: 处于稳定性测试场景,false:处于非稳定性测试场景。 |
**示例:**
```ts
appManager.isRunningInStabilityTest((err, flag) => {
if (err.code != 0) {
if (err.code !== 0) {
conseole.log("isRunningInStabilityTest faile, err: " + JSON.stringify(err));
} else {
console.log("The result of isRunningInStabilityTest is:" + JSON.stringify(flag));
......@@ -54,7 +54,7 @@ static isRunningInStabilityTest(): Promise&lt;boolean&gt;
| Promise&lt;boolean&gt; | 以Promise方式返回接口运行结果及当前是否处于稳定性测试场景,可进行错误处理或其他自定义处理。true: 处于稳定性测试场景,false:处于非稳定性测试场景。 |
**示例:**
```ts
appManager.isRunningInStabilityTest().then((flag) => {
console.log("The result of isRunningInStabilityTest is:" + JSON.stringify(flag));
......@@ -79,7 +79,7 @@ isRamConstrainedDevice(): Promise\<boolean>;
| Promise&lt;boolean&gt; | 以Promise方式返回接口运行结果及当前设备是否为ram受限设备,可进行错误处理或其他自定义处理。true:当前设备为ram受限设备,false:当前设备为非ram受限设备。 |
**示例:**
```ts
appManager.isRamConstrainedDevice().then((data) => {
console.log("The result of isRamConstrainedDevice is:" + JSON.stringify(data));
......@@ -103,10 +103,10 @@ isRamConstrainedDevice(callback: AsyncCallback\<boolean>): void;
| AsyncCallback&lt;boolean&gt; |以回调方式返回接口运行结果及当前设备是否为ram受限设备,可进行错误处理或其他自定义处理。true:当前设备为ram受限设备,false:当前设备为非ram受限设备。 |
**示例:**
```ts
appManager.isRamConstrainedDevice((err, data) => {
if (err.code != 0) {
if (err.code !== 0) {
console.log("isRamConstrainedDevice faile, err: " + JSON.stringify(err));
} else {
console.log("The result of isRamConstrainedDevice is:" + JSON.stringify(data));
......@@ -129,7 +129,7 @@ getAppMemorySize(): Promise\<number>;
| Promise&lt;number&gt; | 以Promise方式返回接口运行结果及应用程序内存大小,可进行错误处理或其他自定义处理。 |
**示例:**
```ts
appManager.getAppMemorySize().then((data) => {
console.log("The size of app memory is:" + JSON.stringify(data));
......@@ -153,10 +153,10 @@ getAppMemorySize(callback: AsyncCallback\<number>): void;
|AsyncCallback&lt;number&gt; |以回调方式返回接口运行结果及应用程序内存大小,可进行错误处理或其他自定义处理。 |
**示例:**
```ts
appManager.getAppMemorySize((err, data) => {
if (err.code != 0) {
if (err.code !== 0) {
console.log("getAppMemorySize faile, err: " + JSON.stringify(err));
} else {
console.log("The size of app memory is:" + JSON.stringify(data));
......@@ -183,7 +183,7 @@ getProcessRunningInformation(): Promise\<Array\<ProcessRunningInformation>>;
| Promise\<Array\<[ProcessRunningInformation](js-apis-inner-application-processRunningInformation.md)>> | 以Promise方式返回接口运行结果及有关运行进程的信息,可进行错误处理或其他自定义处理。 |
**示例:**
```ts
appManager.getProcessRunningInformation().then((data) => {
console.log("The process running information is:" + JSON.stringify(data));
......@@ -211,10 +211,10 @@ getProcessRunningInformation(callback: AsyncCallback\<Array\<ProcessRunningInfor
|AsyncCallback\<Array\<[ProcessRunningInformation](js-apis-inner-application-processRunningInformation.md)>> | 以回调方式返回接口运行结果及有关运行进程的信息,可进行错误处理或其他自定义处理。 |
**示例:**
```ts
appManager.getProcessRunningInformation((err, data) => {
if (err.code != 0) {
if (err.code !== 0) {
console.log("getProcessRunningInformation faile, err: " + JSON.stringify(err));
} else {
console.log("The process running information is:" + JSON.stringify(data));
......@@ -248,30 +248,30 @@ on(type: "applicationState", observer: ApplicationStateObserver): number;
| number | 已注册观测器的数字代码,可用于off接口取消注册观测器。|
**示例:**
```js
var applicationStateObserver = {
let applicationStateObserver = {
onForegroundApplicationChanged(appStateData) {
console.log('------------ onForegroundApplicationChanged -----------' + JSON.stringify(appStateData));
console.log(`[appManager] onForegroundApplicationChanged: ${JSON.stringify(appStateData)}`);
},
onAbilityStateChanged(abilityStateData) {
console.log('------------ onAbilityStateChanged -----------' + JSON.stringify(abilityStateData));
console.log(`[appManager] onAbilityStateChanged: ${JSON.stringify(abilityStateData)}`);
},
onProcessCreated(processData) {
console.log('------------ onProcessCreated -----------' + JSON.stringify(processData));
console.log(`[appManager] onProcessCreated: ${JSON.stringify(processData)}`);
},
onProcessDied(processData) {
console.log('------------ onProcessDied -----------' + JSON.stringify(processData));
console.log(`[appManager] onProcessDied: ${JSON.stringify(processData)}`);
},
onProcessStateChanged(processData) {
console.log('------------ onProcessStateChanged -----------' + JSON.stringify(processData));
console.log(`[appManager] onProcessStateChanged: ${JSON.stringify(processData)}`);
}
}
try {
const observerCode = appManager.on(applicationStateObserver);
console.log('-------- observerCode: ---------' + observerCode);
const observerCode = appManager.on('applicationState', applicationStateObserver);
console.log(`[appManager] observerCode: ${observerCode}`);
} catch (paramError) {
console.log('error: ' + paramError.code + ', ' + paramError.message);
console.log(`[appManager] error: ${paramError.code}, ${paramError.message} `);
}
```
......@@ -293,7 +293,7 @@ on(type: "applicationState", observer: ApplicationStateObserver, bundleNameList:
| -------- | -------- | -------- | -------- |
| type | string | 是 | 调用接口类型,固定填"applicationState"字符串。 |
| observer | [ApplicationStateObserver](./js-apis-inner-application-applicationStateObserver.md) | 是 | 应用状态观测器,用于观测应用的生命周期变化。 |
| bundleNameList | Array<string> | 是 | 表示需要注册监听的bundleName数组。最大值128。 |
| bundleNameList | `Array<string>` | 是 | 表示需要注册监听的bundleName数组。最大值128。 |
**返回值:**
......@@ -302,33 +302,33 @@ on(type: "applicationState", observer: ApplicationStateObserver, bundleNameList:
| number | 已注册观测器的数字代码,可用于off接口注销观测器。|
**示例:**
```js
var applicationStateObserver = {
let applicationStateObserver = {
onForegroundApplicationChanged(appStateData) {
console.log('------------ onForegroundApplicationChanged -----------' + JSON.stringify(appStateData));
console.log(`[appManager] onForegroundApplicationChanged: ${JSON.stringify(appStateData)}`);
},
onAbilityStateChanged(abilityStateData) {
console.log('------------ onAbilityStateChanged -----------' + JSON.stringify(abilityStateData));
console.log(`[appManager] onAbilityStateChanged: ${JSON.stringify(abilityStateData)}`);
},
onProcessCreated(processData) {
console.log('------------ onProcessCreated -----------' + JSON.stringify(processData));
console.log(`[appManager] onProcessCreated: ${JSON.stringify(processData)}`);
},
onProcessDied(processData) {
console.log('------------ onProcessDied -----------' + JSON.stringify(processData));
console.log(`[appManager] onProcessDied: ${JSON.stringify(processData)}`);
},
onProcessStateChanged(processData) {
console.log('------------ onProcessStateChanged -----------' + JSON.stringify(processData));
console.log(`[appManager] onProcessStateChanged: ${JSON.stringify(processData)}`);
}
}
var bundleNameList = ['bundleName1', 'bundleName2'];
let bundleNameList = ['bundleName1', 'bundleName2'];
try {
const observerCode = appManager.on("applicationState", applicationStateObserver, bundleNameList);
console.log('-------- observerCode: ---------', observerCode);
console.log(`[appManager] observerCode: ${observerCode}`);
} catch (paramError) {
console.log('error: ' + paramError.code + ', ' + paramError.message);
console.log(`[appManager] error: ${paramError.code}, ${paramError.message} `);
}
```
```
## appManager.off
......@@ -351,12 +351,12 @@ off(type: "applicationState", observerId: number, callback: AsyncCallback\<void
| callback | AsyncCallback\<void> | 是 | 以回调方式返回接口运行结果,可进行错误处理或其他自定义处理。 |
**示例:**
```ts
var observerId = 100;
let observerId = 100;
function unregisterApplicationStateObserverCallback(err) {
if (err.code != 0) {
if (err.code !== 0) {
console.log("unregisterApplicationStateObserverCallback faile, err: " + JSON.stringify(err));
} else {
console.log("unregisterApplicationStateObserverCallback success.");
......@@ -395,9 +395,9 @@ off(type: "applicationState", observerId: number): Promise\<void>;
| Promise\<void> | 以Promise方式返回接口运行结果,可进行错误处理或其他自定义处理。 |
**示例:**
```ts
var observerId = 100;
let observerId = 100;
try {
appManager.off(observerId).then((data) => {
......@@ -429,10 +429,10 @@ getForegroundApplications(callback: AsyncCallback\<Array\<AppStateData>>): void;
| callback | AsyncCallback\<Array\<[AppStateData](js-apis-inner-application-appStateData.md)>> | 是 | 以回调方式返回接口运行结果及应用状态数据数组,可进行错误处理或其他自定义处理。 |
**示例:**
```ts
function getForegroundApplicationsCallback(err, data) {
if (err.code != 0) {
if (err.code !== 0) {
console.log("getForegroundApplicationsCallback fail, err: " + JSON.stringify(err));
} else {
console.log("getForegroundApplicationsCallback success, data: " + JSON.stringify(data));
......@@ -464,10 +464,10 @@ getForegroundApplications(callback: AsyncCallback\<Array\<AppStateData>>): void;
| callback | AsyncCallback\<Array\<[AppStateData](js-apis-inner-application-appStateData.md)>> | 是 | 以Promise方式返回接口运行结果及应用状态数据数组,可进行错误处理或其他自定义处理。 |
**示例:**
```ts
function getForegroundApplicationsCallback(err, data) {
if (err.code != 0) {
if (err.code !== 0) {
console.log("getForegroundApplicationsCallback fail, err: " + JSON.stringify(err));
} else {
console.log("getForegroundApplicationsCallback success, data: " + JSON.stringify(data));
......@@ -499,7 +499,7 @@ getForegroundApplications(): Promise\<Array\<AppStateData>>;
| Promise\<Array\<[AppStateData](js-apis-inner-application-appStateData.md)>> | 返回前台进程应用程序的数组。 |
**示例:**
```ts
appManager.getForegroundApplications().then((data) => {
console.log("getForegroundApplications success, data: " + JSON.stringify(data));
......@@ -530,8 +530,8 @@ killProcessWithAccount(bundleName: string, accountId: number): Promise\<void\>
**示例:**
```ts
var bundleName = 'bundleName';
var accountId = 0;
let bundleName = 'bundleName';
let accountId = 0;
try {
appManager.killProcessWithAccount(bundleName, accountId).then(() => {
console.log("killProcessWithAccount success");
......@@ -570,7 +570,7 @@ killProcessWithAccount(bundleName: string, accountId: number, callback: AsyncCal
var bundleName = 'bundleName';
var accountId = 0;
function killProcessWithAccountCallback(err, data) {
if (err.code != 0) {
if (err.code !== 0) {
console.log("killProcessWithAccountCallback fail, err: " + JSON.stringify(err));
} else {
console.log("killProcessWithAccountCallback success.");
......@@ -599,11 +599,11 @@ killProcessesByBundleName(bundleName: string, callback: AsyncCallback\<void>);
| callback | AsyncCallback\<void> | 是 | 以回调方式返回接口运行结果,可进行错误处理或其他自定义处理。 |
**示例:**
```ts
var bundleName = 'bundleName';
function killProcessesByBundleNameCallback(err, data) {
if (err.code != 0) {
if (err.code !== 0) {
console.log("killProcessesByBundleNameCallback fail, err: " + JSON.stringify(err));
} else {
console.log("killProcessesByBundleNameCallback success.");
......@@ -641,9 +641,9 @@ killProcessesByBundleName(bundleName: string): Promise\<void>;
| Promise\<void> | 返回执行结果。 |
**示例:**
```ts
var bundleName = 'bundleName';
let bundleName = 'bundleName';
try {
appManager.killProcessesByBundleName(bundleName).then((data) => {
console.log("killProcessesByBundleName success.");
......@@ -675,9 +675,9 @@ clearUpApplicationData(bundleName: string, callback: AsyncCallback\<void>);
| callback | AsyncCallback\<void> | 是 | 以回调方式返回接口运行结果,可进行错误处理或其他自定义处理。 |
**示例:**
```ts
var bundleName = 'bundleName';
let bundleName = 'bundleName';
function clearUpApplicationDataCallback(err, data) {
if (err) {
console.log("clearUpApplicationDataCallback fail, err: " + JSON.stringify(err));
......@@ -717,9 +717,9 @@ clearUpApplicationData(bundleName: string): Promise\<void>;
| Promise\<void> | 以Promise方式返回接口运行结果,可进行错误处理或其他自定义处理。 |
**示例:**
```ts
var bundleName = 'bundleName';
let bundleName = 'bundleName';
try {
appManager.clearUpApplicationData(bundleName).then((data) => {
console.log("clearUpApplicationData success.");
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册