• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.app.ability.dialogRequest (dialogRequest模块)
2
3dialogRequest模块用于处理模态弹框的能力,包括获取RequestInfo(用于绑定模态弹框)、获取RequestCallback(用于设置结果)。
4模态弹框是指一个系统弹出框,其特点在于:该弹出框会拦截弹框之下的页面的鼠标、键盘、触屏等事件,销毁该弹框,才能操作下面的页面。
5
6> **说明:**
7>
8>  - 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
9>  - 本模块接口在ServiceExtensionAbility下使用,如果ServiceExtensionAbility实现了模态弹框,则可以使用本模块的接口获取请求方的RequestInfo、RequestCallback并返回请求结果。
10
11## 导入模块
12
13```ts
14import dialogRequest from '@ohos.app.ability.dialogRequest';
15```
16
17## dialogRequest.getRequestInfo
18
19getRequestInfo(want: Want): RequestInfo
20
21从Want中获取请求方的RequestInfo。
22
23**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
24
25**参数:**
26
27| 参数名 | 类型   | 必填 | 说明                        |
28| ---- | ------ | ---- | --------------------------- |
29| want  | [Want](js-apis-app-ability-want.md) | 是   | 表示发起方请求弹框时传入的want信息。 |
30
31**返回值:**
32
33| 类型   | 说明                     |
34| ------ | ------------------------ |
35| [RequestInfo](#requestinfo) | 请求方RequestInfo,用于绑定模态窗口。 |
36
37**示例:**
38
39```ts
40   import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility';
41   import Want from '@ohos.app.ability.Want';
42   import rpc from '@ohos.rpc';
43   import dialogRequest from '@ohos.app.ability.dialogRequest';
44
45    const REQUEST_VALUE = 1;
46
47    class StubTest extends rpc.RemoteObject {
48      constructor(des: string) {
49        super(des);
50      }
51
52      onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption) {
53        if (code === REQUEST_VALUE) {
54          let optFir = data.readInt();
55          let optSec = data.readInt();
56          reply.writeInt(optFir + optSec);
57        }
58        return true;
59      }
60
61      getInterfaceDescriptor() {
62        return "";
63      }
64
65      getCallingPid() {
66        return REQUEST_VALUE;
67      }
68
69      getCallingUid() {
70        return REQUEST_VALUE;
71      }
72
73      attachLocalInterface(localInterface: rpc.IRemoteBroker, descriptor: string) {
74      }
75    }
76
77    let TAG = "getRequestInfoTest";
78
79    export default class ServiceExtAbility extends ServiceExtensionAbility {
80      onCreate(want: Want) {
81        console.info(TAG, `onCreate, want: ${want.abilityName}`);
82      }
83
84      onRequest(want: Want, startId: number) {
85        console.info(TAG, `onRequest, want: ${want.abilityName}`);
86        try {
87          let requestInfo = dialogRequest.getRequestInfo(want);
88        } catch (err) {
89          console.error(`getRequestInfo err= ${JSON.stringify(err)}`);
90        }
91      }
92
93      onConnect(want: Want) {
94        console.info(TAG, `onConnect, want: ${want.abilityName}`);
95        return new StubTest("test");
96      }
97
98      onDisconnect(want: Want) {
99        console.info(TAG, `onDisconnect, want: ${want.abilityName}`);
100      }
101
102      onDestroy() {
103        console.info(TAG, `onDestroy`);
104      }
105    }
106   ```
107
108## dialogRequest.getRequestCallback
109
110getRequestCallback(want: Want): RequestCallback
111
112从Want中获取请求方的RequestCallback。
113
114**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
115
116**参数:**
117
118| 参数名 | 类型   | 必填 | 说明                        |
119| ---- | ------ | ---- | --------------------------- |
120| want  | [Want](js-apis-app-ability-want.md) | 是   | 表示发起方请求弹框时传入的want信息。 |
121
122**返回值:**
123
124| 类型   | 说明                     |
125| ------ | ------------------------ |
126| [RequestCallback](#requestcallback) | 请求方RequestCallback,用于设置返回结果。 |
127
128**示例:**
129
130```ts
131   import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility';
132   import Want from '@ohos.app.ability.Want';
133   import rpc from '@ohos.rpc';
134   import dialogRequest from '@ohos.app.ability.dialogRequest';
135
136   let TAG = "getRequestCallbackTest";
137
138   const REQUEST_VALUE = 1;
139
140    class StubTest extends rpc.RemoteObject {
141      constructor(des: string) {
142        super(des);
143      }
144
145      onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption) {
146        if (code === REQUEST_VALUE) {
147          let optFir = data.readInt();
148          let optSec = data.readInt();
149          reply.writeInt(optFir + optSec);
150        }
151        return true;
152      }
153
154      getInterfaceDescriptor() {
155        return "";
156      }
157
158      getCallingPid() {
159        return REQUEST_VALUE;
160      }
161
162      getCallingUid() {
163        return REQUEST_VALUE;
164      }
165
166      attachLocalInterface(localInterface: rpc.IRemoteBroker, descriptor: string) {
167      }
168    }
169
170   export default class ServiceExtAbility extends ServiceExtensionAbility {
171     onCreate(want: Want) {
172       console.info(TAG, `onCreate, want: ${want.abilityName}`);
173     }
174
175     onRequest(want: Want, startId: number) {
176       console.info(TAG, `onRequest, want: ${want.abilityName}`);
177       try {
178            let requestCallback = dialogRequest.getRequestCallback(want);
179        } catch(err) {
180            console.error(`getRequestInfo err= ${JSON.stringify(err)}`);
181        }
182     }
183
184     onConnect(want: Want) {
185       console.info(TAG, `onConnect, want: ${want.abilityName}`);
186       return new StubTest("test");
187     }
188
189     onDisconnect(want: Want) {
190       console.info(TAG, `onDisconnect, want: ${want.abilityName}`);
191     }
192
193     onDestroy() {
194       console.info(TAG, `onDestroy`);
195     }
196   }
197   ```
198
199## WindowRect<sup>10+</sup>
200
201表示模态弹框的属性。
202
203**模型约束**:此接口仅可在Stage模型下使用。
204
205**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
206
207| 名称 | 类型   | 必填 | 说明                        |
208| ---- | ------ | ---- | --------------------------- |
209| left  | number | 否   | 弹框边框的左上角的X坐标。 |
210| top  | number | 否   | 弹框边框的左上角的Y坐标。 |
211| width  | number | 否   | 弹框的宽度。 |
212| height  | number | 否   | 弹框的高度。 |
213
214## RequestInfo
215
216表示发起方请求信息,作为窗口绑定模态弹框的入参。
217
218**模型约束**:此接口仅可在Stage模型下使用。
219
220**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
221
222| 名称      | 类型       | 必填   | 说明     |
223| ------------ | ------------------| ------ | ---------------------- |
224| windowRect<sup>10+</sup>            | windowRect    | 否   | 表示模态弹框的位置属性。          |
225
226**示例:**
227
228```ts
229   import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility';
230   import Want from '@ohos.app.ability.Want';
231   import { BusinessError } from '@ohos.base';
232   import rpc from '@ohos.rpc';
233   import dialogRequest from '@ohos.app.ability.dialogRequest';
234   import window from '@ohos.window';
235
236   let TAG = "RequestInfoTest";
237
238   const REQUEST_VALUE = 1;
239
240    class StubTest extends rpc.RemoteObject {
241      constructor(des: string) {
242        super(des);
243      }
244
245      onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption) {
246        if (code === REQUEST_VALUE) {
247          let optFir = data.readInt();
248          let optSec = data.readInt();
249          reply.writeInt(optFir + optSec);
250        }
251        return true;
252      }
253
254      getInterfaceDescriptor() {
255        return "";
256      }
257
258      getCallingPid() {
259        return REQUEST_VALUE;
260      }
261
262      getCallingUid() {
263        return REQUEST_VALUE;
264      }
265
266      attachLocalInterface(localInterface: rpc.IRemoteBroker, descriptor: string) {
267      }
268    }
269
270   export default class ServiceExtAbility extends ServiceExtensionAbility {
271     onCreate(want: Want) {
272       console.info(TAG, `onCreate, want: ${want.abilityName}`);
273     }
274
275     onRequest(want: Want, startId: number) {
276       console.info(TAG, `onRequest, want: ${want.abilityName}`);
277       let windowClass: window.Window | undefined = undefined;
278       let config: window.Configuration = {name: "dialogWindow", windowType: window.WindowType.TYPE_DIALOG, ctx: this.context};
279       try {
280            let requestInfo = dialogRequest.getRequestInfo(want);
281            window.createWindow(config, (err, data) => {
282              if (err.code) {
283                  console.error('Failed to create the window. Cause: ' + JSON.stringify(err));
284                  return;
285              }
286              windowClass = data;
287              windowClass.bindDialogTarget(requestInfo, () => {
288                console.info('Dialog Window Need Destroy.');
289              }, (err: BusinessError) => {
290                  if (err.code) {
291                      console.error(`Failed to bind dialog target. Cause: ${JSON.stringify(err)}`);
292                      return;
293                  }
294                  console.info('Succeeded in binding dialog target.');
295              });
296            });
297        } catch(err) {
298            console.error(`getRequestInfo err= ${JSON.stringify(err)}`);
299        }
300     }
301
302     onConnect(want: Want) {
303       console.info(TAG, `onConnect, want: ${want.abilityName}`);
304       return new StubTest("test");
305     }
306
307     onDisconnect(want: Want) {
308       console.info(TAG, `onDisconnect, want: ${want.abilityName}`);
309     }
310
311     onDestroy() {
312       console.info(TAG, `onDestroy`);
313     }
314   }
315   ```
316
317## ResultCode
318
319模态弹框请求结果码。
320
321**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
322
323| 名称      | 值          | 说明     |
324| ------------ | ------------------ | ---------------------- |
325| RESULT_OK            | 0          | 表示成功。          |
326| RESULT_CANCEL        | 1          | 表示失败。          |
327
328## RequestResult
329模态弹框请求结果,当前只包含结果码,即RequestResult只当前只有ResultCode这一个成员。
330
331## 属性
332
333**模型约束**:此接口仅可在Stage模型下使用。
334
335**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
336
337| 名称 | 类型 | 可读 | 可写 | 说明 |
338| -------- | -------- | -------- | -------- | -------- |
339| result | [ResultCode](#resultcode) | 是 | 是 | 表示结果码。 |
340| want<sup>10+</sup> | [ResultWant](js-apis-app-ability-want.md)  | 是 | 是 | 表示Want类型信息,如ability名称,包名等。 |
341
342## RequestCallback
343
344用于设置模态弹框请求结果的callback接口。
345
346**模型约束**:此接口仅可在Stage模型下使用。
347
348### RequestCallback.setRequestResult
349
350setRequestResult(result: RequestResult): void;
351
352设置请求结果
353
354**模型约束**:此接口仅可在Stage模型下使用。
355
356**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore
357
358**参数:**
359
360| 参数名 | 类型 | 必填 | 说明 |
361| -------- | -------- | -------- | -------- |
362| result | [RequestResult](#requestresult) | 是 | 模态弹框请求结果信息。 |
363
364**错误码:**
365
366| 错误码ID | 错误信息 |
367| ------- | -------------------------------- |
368| 401 | If the input parameter is not valid parameter. |
369
370以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)。
371
372**示例:**
373
374```ts
375   import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility';
376   import Want from '@ohos.app.ability.Want';
377   import rpc from '@ohos.rpc';
378   import dialogRequest from '@ohos.app.ability.dialogRequest';
379
380   let TAG = "setRequestResultTest";
381
382      const REQUEST_VALUE = 1;
383
384    class StubTest extends rpc.RemoteObject {
385      constructor(des: string) {
386        super(des);
387      }
388
389      onRemoteRequest(code: number, data: rpc.MessageParcel, reply: rpc.MessageParcel, option: rpc.MessageOption) {
390        if (code === REQUEST_VALUE) {
391          let optFir = data.readInt();
392          let optSec = data.readInt();
393          reply.writeInt(optFir + optSec);
394        }
395        return true;
396      }
397
398      getInterfaceDescriptor() {
399        return "";
400      }
401
402      getCallingPid() {
403        return REQUEST_VALUE;
404      }
405
406      getCallingUid() {
407        return REQUEST_VALUE;
408      }
409
410      attachLocalInterface(localInterface: rpc.IRemoteBroker, descriptor: string) {
411      }
412    }
413
414   export default class ServiceExtAbility extends ServiceExtensionAbility {
415     onCreate(want: Want) {
416       console.info(TAG, `onCreate, want: ${want.abilityName}`);
417     }
418
419     onRequest(want: Want, startId: number) {
420       console.info(TAG, `onRequest, want: ${want.abilityName}`);
421       try {
422            let requestCallback = dialogRequest.getRequestCallback(want);
423            let myResult: dialogRequest.RequestResult = {
424                result : dialogRequest.ResultCode.RESULT_CANCEL,
425            };
426            requestCallback.setRequestResult(myResult);
427        } catch(err) {
428            console.error(`getRequestInfo err= ${JSON.stringify(err)}`);
429        }
430     }
431
432     onConnect(want: Want) {
433       console.info(TAG, `onConnect, want: ${want.abilityName}`);
434       return new StubTest("test");
435     }
436
437     onDisconnect(want: Want) {
438       console.info(TAG, `onDisconnect, want: ${want.abilityName}`);
439     }
440
441     onDestroy() {
442       console.info(TAG, `onDestroy`);
443     }
444   }
445  ```