• 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   export default class ServiceExtAbility extends ServiceExtensionAbility {
45     onCreate(want) {
46       console.info(TAG, `onCreate, want: ${want.abilityName}`);
47     }
48
49     onRequest(want, startId) {
50       console.info(TAG, `onRequest, want: ${want.abilityName}`);
51       try {
52            var requestInfo = dialogRequest.getRequestInfo(want);
53        } catch(err) {
54            console.error('getRequestInfo err= ${JSON.stringify(err)}');
55        }
56     }
57
58     onConnect(want) {
59       console.info(TAG, `onConnect, want: ${want.abilityName}`);
60     }
61
62     onDisconnect(want) {
63       console.info(TAG, `onDisconnect, want: ${want.abilityName}`);
64     }
65
66     onDestroy() {
67       console.info(TAG, `onDestroy`);
68     }
69   }
70   ```
71
72## dialogRequest.getRequestCallback
73
74getRequestCallback(want: Want): RequestCallback
75
76Obtains the request callback from Want.
77
78**System capability**: SystemCapability.Ability.AbilityRuntime.Core
79
80**Parameters**
81
82| Name| Type  | Mandatory| Description                       |
83| ---- | ------ | ---- | --------------------------- |
84| want  | [Want](js-apis-application-want.md) | Yes  | Want passed in the request for a modal dialog box.|
85
86**Return value**
87
88| Type  | Description                    |
89| ------ | ------------------------ |
90| [RequestCallback](#requestcallback) | **RequestCallback** object obtained, which is used to set the return result.|
91
92**Example**
93
94```ts
95   import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility';
96   import rpc from '@ohos.rpc';
97   import dialogRequest from '@ohos.app.ability.dialogRequest';
98
99   export default class ServiceExtAbility extends ServiceExtensionAbility {
100     onCreate(want) {
101       console.info(TAG, `onCreate, want: ${want.abilityName}`);
102     }
103
104     onRequest(want, startId) {
105       console.info(TAG, `onRequest, want: ${want.abilityName}`);
106       try {
107            var requestCallback = dialogRequest.getRequestCallback(want);
108        } catch(err) {
109            console.error('getRequestInfo err= ${JSON.stringify(err)}');
110        }
111     }
112
113     onConnect(want) {
114       console.info(TAG, `onConnect, want: ${want.abilityName}`);
115     }
116
117     onDisconnect(want) {
118       console.info(TAG, `onDisconnect, want: ${want.abilityName}`);
119     }
120
121     onDestroy() {
122       console.info(TAG, `onDestroy`);
123     }
124   }
125   ```
126
127## RequestInfo
128
129Defines the request information, which is used as an input parameter for binding the modal dialog box.
130
131**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore
132
133**Example**
134
135```ts
136   import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility';
137   import rpc from '@ohos.rpc';
138   import dialogRequest from '@ohos.app.ability.dialogRequest';
139   import window from '@ohos.window';
140
141   export default class ServiceExtAbility extends ServiceExtensionAbility {
142     onCreate(want) {
143       console.info(TAG, `onCreate, want: ${want.abilityName}`);
144     }
145
146     onRequest(want, startId) {
147       console.info(TAG, `onRequest, want: ${want.abilityName}`);
148       try {
149            var requestInfo = dialogRequest.getRequestInfo(want);
150            window.bindDialogTarget(requestInfo, () => {
151                console.info('Dialog Window Need Destroy.');
152            }, (err) => {
153                if (err.code) {
154                    console.error('Failed to bind dialog target. Cause: ${JSON.stringify(err)}');
155                    return;
156                }
157                console.info('Succeeded in binding dialog target.');
158            });
159        } catch(err) {
160            console.error('getRequestInfo err= ${JSON.stringify(err)}');
161        }
162     }
163
164     onConnect(want) {
165       console.info(TAG, `onConnect, want: ${want.abilityName}`);
166     }
167
168     onDisconnect(want) {
169       console.info(TAG, `onDisconnect, want: ${want.abilityName}`);
170     }
171
172     onDestroy() {
173       console.info(TAG, `onDestroy`);
174     }
175   }
176   ```
177
178## ResultCode
179
180Enumerates the result codes of the request for the modal dialog box.
181
182**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore
183
184| Name     | Value         | Description    |
185| ------------ | ------------------ | ---------------------- |
186| RESULT_OK            | 0          | The request succeeds.         |
187| RESULT_CANCEL        | 1          | The request fails.         |
188
189## RequestResult
190Defines the result of the request for the modal dialog box. Only the result code is included.
191
192## Attributes
193
194**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore
195
196| Name| Type| Readable| Writable| Description|
197| -------- | -------- | -------- | -------- | -------- |
198| result | [ResultCode](#resultcode) | Yes| Yes| Result code of the request.|
199
200## RequestCallback
201
202Provides a callback for setting the modal dialog box request result.
203
204### RequestCallback.setRequestResult
205
206setRequestResult(result: RequestResult): void;
207
208Sets the result of the request for the modal dialog box.
209
210**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore
211
212**Parameters**
213
214| Name| Type| Mandatory| Description|
215| -------- | -------- | -------- | -------- |
216| result | [RequestResult](#requestresult) | Yes| Request result to set.|
217
218**Error codes**
219
220| ID| Error Message|
221| ------- | -------------------------------- |
222| 401 | If the input parameter is not valid parameter. |
223
224For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
225
226**Example**
227
228```ts
229   import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility';
230   import rpc from '@ohos.rpc';
231   import dialogRequest from '@ohos.app.ability.dialogRequest';
232
233   export default class ServiceExtAbility extends ServiceExtensionAbility {
234     onCreate(want) {
235       console.info(TAG, `onCreate, want: ${want.abilityName}`);
236     }
237
238     onRequest(want, startId) {
239       console.info(TAG, `onRequest, want: ${want.abilityName}`);
240       try {
241            var requestCallback = dialogRequest.getRequestCallback(want);
242            let myResult = {
243                result : dialogRequest.ResultCode.RESULT_CANCEL,
244            };
245            requestCallback.setRequestResult(myResult);
246        } catch(err) {
247            console.error('getRequestInfo err= ${JSON.stringify(err)}');
248        }
249     }
250
251     onConnect(want) {
252       console.info(TAG, `onConnect, want: ${want.abilityName}`);
253     }
254
255     onDisconnect(want) {
256       console.info(TAG, `onDisconnect, want: ${want.abilityName}`);
257     }
258
259     onDestroy() {
260       console.info(TAG, `onDestroy`);
261     }
262   }
263  ```
264