• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.app.ability.dialogRequest (dialogRequest)
2
3The **dialogRequest** module provides APIs related to modal dialog box processing, including obtaining the request information (used to bind a modal dialog box) and request callback (used to set the request result).
4A modal dialog box is a system pop-up box that intercepts events (such as mouse, keyboard, and touchscreen events) triggered for the page displayed under it. The page can be operated only after the modal dialog box is destroyed.
5
6> **NOTE**
7>
8>  - The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
9>  - The APIs provided by this module are used in ServiceExtensionAbilities. For a ServiceExtensionAbility that implements modal dialog boxes, you can use the APIs to obtain the request information and request callback and return the request result.
10
11## Modules to Import
12
13```js
14import dialogRequest from '@ohos.app.ability.dialogRequest';
15```
16
17## dialogRequest.getRequestInfo
18
19getRequestInfo(want: Want): RequestInfo
20
21Obtains the request information from Want.
22
23**System capability**: SystemCapability.Ability.AbilityRuntime.Core
24
25**Parameters**
26
27| Name| Type  | Mandatory| Description                       |
28| ---- | ------ | ---- | --------------------------- |
29| want  | [Want](js-apis-application-want.md) | Yes  | Want passed in the request for a modal dialog box.|
30
31**Return value**
32
33| Type  | Description                    |
34| ------ | ------------------------ |
35| [RequestInfo](#requestinfo) | **RequestInfo** object obtained, which is used to bind a modal dialog box.|
36
37**Example**
38
39```ts
40   import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility';
41   import rpc from '@ohos.rpc';
42   import dialogRequest from '@ohos.app.ability.dialogRequest';
43
44    const REQUEST_VALUE = 1;
45
46    class StubTest extends rpc.RemoteObject {
47      constructor(des) {
48        super(des);
49      }
50
51      onRemoteRequest(code, data, reply, option) {
52        if (code === REQUEST_VALUE) {
53          let optFir = data.readInt();
54          let optSec = data.readInt();
55          reply.writeInt(optFir + optSec);
56        }
57        return true;
58      }
59
60      queryLocallInterface(descriptor) {
61        return null;
62      }
63
64      getInterfaceDescriptor() {
65        return "";
66      }
67
68      getCallingPid() {
69        return REQUEST_VALUE;
70      }
71
72      getCallingUid() {
73        return REQUEST_VALUE;
74      }
75
76      attachLocalInterface(localInterface, descriptor) {
77      }
78    }
79
80    let TAG = "getRequestInfoTest";
81
82    export default class ServiceExtAbility extends ServiceExtensionAbility {
83      onCreate(want) {
84        console.info(TAG, `onCreate, want: ${want.abilityName}`);
85      }
86
87      onRequest(want, startId) {
88        console.info(TAG, `onRequest, want: ${want.abilityName}`);
89        try {
90          var requestInfo = dialogRequest.getRequestInfo(want);
91        } catch (err) {
92          console.error('getRequestInfo err= ${JSON.stringify(err)}');
93        }
94      }
95
96      onConnect(want) {
97        console.info(TAG, `onConnect, want: ${want.abilityName}`);
98        return new StubTest("test");
99      }
100
101      onDisconnect(want) {
102        console.info(TAG, `onDisconnect, want: ${want.abilityName}`);
103      }
104
105      onDestroy() {
106        console.info(TAG, `onDestroy`);
107      }
108    }
109   ```
110
111## dialogRequest.getRequestCallback
112
113getRequestCallback(want: Want): RequestCallback
114
115Obtains the request callback from Want.
116
117**System capability**: SystemCapability.Ability.AbilityRuntime.Core
118
119**Parameters**
120
121| Name| Type  | Mandatory| Description                       |
122| ---- | ------ | ---- | --------------------------- |
123| want  | [Want](js-apis-application-want.md) | Yes  | Want passed in the request for a modal dialog box.|
124
125**Return value**
126
127| Type  | Description                    |
128| ------ | ------------------------ |
129| [RequestCallback](#requestcallback) | **RequestCallback** object obtained, which is used to set the return result.|
130
131**Example**
132
133```ts
134   import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility';
135   import rpc from '@ohos.rpc';
136   import dialogRequest from '@ohos.app.ability.dialogRequest';
137
138   let TAG = "getRequestCallbackTest";
139
140   const REQUEST_VALUE = 1;
141
142    class StubTest extends rpc.RemoteObject {
143      constructor(des) {
144        super(des);
145      }
146
147      onRemoteRequest(code, data, reply, option) {
148        if (code === REQUEST_VALUE) {
149          let optFir = data.readInt();
150          let optSec = data.readInt();
151          reply.writeInt(optFir + optSec);
152        }
153        return true;
154      }
155
156      queryLocallInterface(descriptor) {
157        return null;
158      }
159
160      getInterfaceDescriptor() {
161        return "";
162      }
163
164      getCallingPid() {
165        return REQUEST_VALUE;
166      }
167
168      getCallingUid() {
169        return REQUEST_VALUE;
170      }
171
172      attachLocalInterface(localInterface, descriptor) {
173      }
174    }
175
176   export default class ServiceExtAbility extends ServiceExtensionAbility {
177     onCreate(want) {
178       console.info(TAG, `onCreate, want: ${want.abilityName}`);
179     }
180
181     onRequest(want, startId) {
182       console.info(TAG, `onRequest, want: ${want.abilityName}`);
183       try {
184            var requestCallback = dialogRequest.getRequestCallback(want);
185        } catch(err) {
186            console.error('getRequestInfo err= ${JSON.stringify(err)}');
187        }
188     }
189
190     onConnect(want) {
191       console.info(TAG, `onConnect, want: ${want.abilityName}`);
192       return new StubTest("test");
193     }
194
195     onDisconnect(want) {
196       console.info(TAG, `onDisconnect, want: ${want.abilityName}`);
197     }
198
199     onDestroy() {
200       console.info(TAG, `onDestroy`);
201     }
202   }
203   ```
204
205## RequestInfo
206
207Defines the request information, which is used as an input parameter for binding the modal dialog box.
208**System capability**: SystemCapability.Ability.AbilityRuntime.Core
209
210**Example**
211
212```ts
213   import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility';
214   import rpc from '@ohos.rpc';
215   import dialogRequest from '@ohos.app.ability.dialogRequest';
216   import window from '@ohos.window';
217
218   let TAG = "RequestInfoTest";
219
220   const REQUEST_VALUE = 1;
221
222    class StubTest extends rpc.RemoteObject {
223      constructor(des) {
224        super(des);
225      }
226
227      onRemoteRequest(code, data, reply, option) {
228        if (code === REQUEST_VALUE) {
229          let optFir = data.readInt();
230          let optSec = data.readInt();
231          reply.writeInt(optFir + optSec);
232        }
233        return true;
234      }
235
236      queryLocallInterface(descriptor) {
237        return null;
238      }
239
240      getInterfaceDescriptor() {
241        return "";
242      }
243
244      getCallingPid() {
245        return REQUEST_VALUE;
246      }
247
248      getCallingUid() {
249        return REQUEST_VALUE;
250      }
251
252      attachLocalInterface(localInterface, descriptor) {
253      }
254    }
255
256   export default class ServiceExtAbility extends ServiceExtensionAbility {
257     onCreate(want) {
258       console.info(TAG, `onCreate, want: ${want.abilityName}`);
259     }
260
261     onRequest(want, startId) {
262       console.info(TAG, `onRequest, want: ${want.abilityName}`);
263       try {
264            var requestInfo = dialogRequest.getRequestInfo(want);
265            window.bindDialogTarget(requestInfo, () => {
266                console.info('Dialog Window Need Destroy.');
267            }, (err) => {
268                if (err.code) {
269                    console.error('Failed to bind dialog target. Cause: ${JSON.stringify(err)}');
270                    return;
271                }
272                console.info('Succeeded in binding dialog target.');
273            });
274        } catch(err) {
275            console.error('getRequestInfo err= ${JSON.stringify(err)}');
276        }
277     }
278
279     onConnect(want) {
280       console.info(TAG, `onConnect, want: ${want.abilityName}`);
281        return new StubTest("test");
282
283     }
284
285     onDisconnect(want) {
286       console.info(TAG, `onDisconnect, want: ${want.abilityName}`);
287     }
288
289     onDestroy() {
290       console.info(TAG, `onDestroy`);
291     }
292   }
293   ```
294
295## ResultCode
296
297Enumerates the result codes of the request for the modal dialog box.
298
299**System capability**: SystemCapability.Ability.AbilityRuntime.Core
300
301| Name     | Value         | Description    |
302| ------------ | ------------------ | ---------------------- |
303| RESULT_OK            | 0          | The request succeeds.         |
304| RESULT_CANCEL        | 1          | The request fails.         |
305
306## RequestResult
307Defines the result of the request for the modal dialog box. Only the result code is included.
308
309## Attributes
310
311**System capability**: SystemCapability.Ability.AbilityRuntime.Core
312
313| Name| Type| Readable| Writable| Description|
314| -------- | -------- | -------- | -------- | -------- |
315| result | [ResultCode](#resultcode) | Yes| Yes| Result code of the request.|
316
317## RequestCallback
318
319Provides a callback for setting the modal dialog box request result.
320
321### RequestCallback.setRequestResult
322
323setRequestResult(result: RequestResult): void;
324
325Sets the result of the request for the modal dialog box.
326
327**System capability**: SystemCapability.Ability.AbilityRuntime.Core
328
329**Parameters**
330
331| Name| Type| Mandatory| Description|
332| -------- | -------- | -------- | -------- |
333| result | [RequestResult](#requestresult) | Yes| Request result to set.|
334
335**Error codes**
336
337| ID| Error Message|
338| ------- | -------------------------------- |
339| 401 | If the input parameter is not valid parameter. |
340
341For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
342
343**Example**
344
345```ts
346   import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility';
347   import rpc from '@ohos.rpc';
348   import dialogRequest from '@ohos.app.ability.dialogRequest';
349
350   let TAG = "setRequestResultTest";
351
352      const REQUEST_VALUE = 1;
353
354    class StubTest extends rpc.RemoteObject {
355      constructor(des) {
356        super(des);
357      }
358
359      onRemoteRequest(code, data, reply, option) {
360        if (code === REQUEST_VALUE) {
361          let optFir = data.readInt();
362          let optSec = data.readInt();
363          reply.writeInt(optFir + optSec);
364        }
365        return true;
366      }
367
368      queryLocallInterface(descriptor) {
369        return null;
370      }
371
372      getInterfaceDescriptor() {
373        return "";
374      }
375
376      getCallingPid() {
377        return REQUEST_VALUE;
378      }
379
380      getCallingUid() {
381        return REQUEST_VALUE;
382      }
383
384      attachLocalInterface(localInterface, descriptor) {
385      }
386    }
387
388   export default class ServiceExtAbility extends ServiceExtensionAbility {
389     onCreate(want) {
390       console.info(TAG, `onCreate, want: ${want.abilityName}`);
391     }
392
393     onRequest(want, startId) {
394       console.info(TAG, `onRequest, want: ${want.abilityName}`);
395       try {
396            var requestCallback = dialogRequest.getRequestCallback(want);
397            let myResult = {
398                result : dialogRequest.ResultCode.RESULT_CANCEL,
399            };
400            requestCallback.setRequestResult(myResult);
401        } catch(err) {
402            console.error('getRequestInfo err= ${JSON.stringify(err)}');
403        }
404     }
405
406     onConnect(want) {
407       console.info(TAG, `onConnect, want: ${want.abilityName}`);
408        return new StubTest("test");
409     }
410
411     onDisconnect(want) {
412       console.info(TAG, `onDisconnect, want: ${want.abilityName}`);
413     }
414
415     onDestroy() {
416       console.info(TAG, `onDestroy`);
417     }
418   }
419  ```
420