• 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```js
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-application-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 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
115从Want中获取请求方的RequestCallback。
116
117**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
118
119**参数:**
120
121| 参数名 | 类型   | 必填 | 说明                        |
122| ---- | ------ | ---- | --------------------------- |
123| want  | [Want](js-apis-application-want.md) | 是   | 表示发起方请求弹框时传入的want信息。 |
124
125**返回值:**
126
127| 类型   | 说明                     |
128| ------ | ------------------------ |
129| [RequestCallback](#requestcallback) | 请求方RequestCallback,用于设置返回结果。 |
130
131**示例:**
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
207表示发起方请求信息,作为窗口绑定模态弹框的入参。
208**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
209
210**示例:**
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
297模态弹框请求结果码。
298
299**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
300
301| 参数名      | 值          | 说明     |
302| ------------ | ------------------ | ---------------------- |
303| RESULT_OK            | 0          | 表示成功。          |
304| RESULT_CANCEL        | 1          | 表示失败。          |
305
306## RequestResult
307模态弹框请求结果,当前只包含结果码,即RequestResult只当前只有ResultCode这一个成员。
308
309## 属性
310
311**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
312
313| 名称 | 类型 | 可读 | 可写 | 说明 |
314| -------- | -------- | -------- | -------- | -------- |
315| result | [ResultCode](#resultcode) | 是 | 是 | 表示结果码。 |
316
317## RequestCallback
318
319用于设置模态弹框请求结果的callback接口。
320
321### RequestCallback.setRequestResult
322
323setRequestResult(result: RequestResult): void;
324
325设置请求结果
326
327**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
328
329**参数:**
330
331| 参数名 | 类型 | 必填 | 说明 |
332| -------- | -------- | -------- | -------- |
333| result | [RequestResult](#requestresult) | 是 | 模态弹框请求结果信息。 |
334
335**错误码:**
336
337| 错误码ID | 错误信息 |
338| ------- | -------------------------------- |
339| 401 | If the input parameter is not valid parameter. |
340
341以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)。
342
343**示例:**
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  ```