Native application calling method, system and equipment and storage medium
By generating and injecting JavaScript code, function calls and callbacks between native applications and WebView are realized, which solves the problems of limited data interaction and insufficient security in the existing technology, and improves the flexibility and security of interaction.
Patent Information
- Application Number
- CN202410656807.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-24
- Publication Date
- 2024-09-06
AI Technical Summary
In the existing technology, the data interaction between native applications and WebView has insufficient flexibility and security. Native functions cannot be directly exposed, data interaction is limited, and JavaScript code execution may introduce security risks.
By creating NativeFunctionForJs objects, WebChromeClient objects, and JsCallNative objects, JavaScript code is generated and written into web pages to implement native application function calls and callbacks. The WebChromeClient object is used to respond to call requests and execute native functions to enhance interaction security.
It improves the flexibility and security of data interaction between native applications and WebView, avoids function expansion restrictions, and enhances the scalability and security of interaction.
Smart Images

Figure CN118606071A
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a native application calling method, system, device and storage medium. Background Art
[0002] In the field of contemporary mobile application development, WebView (web view) is a view component provided by mobile operating systems, which is used to embed web pages in applications, that is, to display web page content inside native applications. Native applications refer to applications designed and developed specifically for specific operating systems, thus combining the flexibility of the Internet with the performance advantages of native applications, greatly enriching the functionality and user experience of applications. Although WebView has brought convenience to hybrid application development, it still faces challenges in promoting the ability to deeply interact data between native applications and embedded web pages.
[0003] In related technologies, developers usually control the loading and interaction of WebView through Java code. However, this interaction method has some limitations, especially in the data interaction between native applications and web pages, such as: 1. Lack of native function exposure. Functions in native applications cannot be directly exposed to web pages for use, which limits the interactive capabilities of web pages and the functional expansion of native applications; 2. Data interaction restrictions. Data between native applications and web pages can usually only be passed through webView.addJavascriptInterface(), and interface callbacks cannot be implemented when calling native functions; 3. Interaction security issues. JavaScript code is executed in web pages, which may introduce security risks. Attackers can use the reflection mechanism to call any other unregistered Java classes, resulting in unlimited enhancement of JavaScript capabilities.
[0004] Therefore, how to improve the flexibility and security of data interaction between web pages and native applications in WebView while maintaining application performance has become a technical problem that needs to be solved urgently. Summary of the invention
[0005] In order to provide a basic understanding of some aspects of the disclosed embodiments, a brief summary is given below. The summary is not an extensive review, nor is it intended to identify key / critical components or delineate the scope of protection of these embodiments, but rather serves as a prelude to the detailed description that follows.
[0006] In view of the shortcomings of the prior art described above, the present application discloses a native application calling method, system, device and storage medium, which are used to solve the technical problem of lack of flexibility and security in data interaction between web pages and native applications in WebView in the prior art.
[0007] In the first aspect, the present application provides a native application calling method, the method comprising: pre-creating a NativeFunctionForJs object, a WebChromeClient object and a JsCallNative object, wherein the NativeFunctionForJs object contains function information of the native application; using the JsCallNative object, reading the function information in the NativeFunctionForJs object, and generating JavaScript code according to the function information; writing the JavaScript code into a web page through a network view, so that the web page generates a call request information according to the JavaScript code and the target native application to be called, wherein the call request information includes a target function name and a parameter value array corresponding to the target native application; using the WebChromeClient object, responding to the call request information, and executing the target function corresponding to the target function name according to the parameter value array, to complete the call of the target native application.
[0008] In one embodiment of the present application, the JsCallNative object is used to read the function information in the NativeFunctionForJs object, and JavaScript code is generated according to the function information, including: calling the generateJsCode method in the JsCallNative object, and parsing the function information in the NativeFunctionForJs object based on the generateJsCode method, wherein the function information includes the function name and parameter array of the native application; and the corresponding function name of each native application is associated with the parameter array to obtain the JavaScript code.
[0009] In one embodiment of the present application, the use of the WebChromeClient object to respond to the call request information and execute the target function corresponding to the target function name according to the parameter value array includes: pre-creating a WebChromeClient object and configuring the onJsPrompt method in the WebChromeClient object; activating the onJsPrompt method in response to the JavaScript code; using the onJsPrompt method to call the callNative method in the JsCallNative object, parsing the call request information, and obtaining the target function name and the parameter value array; reflectively calling the corresponding target function in the NativeFunctionForJs object based on the target function name, and executing the target function according to the parameter value array.
[0010] In one embodiment of the present application, the call request information also includes callback function information, and the callback function information includes a callback function name; after completing the call of the target native application, it also includes: creating a CallBackInJs object, and obtaining the callback function name and the call result of the target native application, wherein the CallBackInJs object is configured with a callback method; calling the callback method, encapsulating the callback function name and the call result, and generating a callback code; using the loadUrl method in the network view, executing the callback code, and calling the corresponding callback function in the JavaScript environment according to the callback function name, so that the web page calls back the call result.
[0011] In one embodiment of the present application, before writing the JavaScript code into the web page through the network view, it also includes: obtaining the web page address and loading the web page corresponding to the web page address; configuring the onProgressChanged method of the WebChromeClient object to monitor the loading progress of the web page; comparing the loading progress with a preset loading progress threshold; if the loading progress is greater than or equal to the loading progress threshold, calling the loadUrl method in the network view to perform the JavaScript code writing operation.
[0012] In one embodiment of the present application, the web page generates a call request message based on the JavaScript code and the target native application to be called, including: in response to the native application call instruction, calling the prompt method in the JavaScript environment, wherein the native application call instruction carries the target function name and parameter value corresponding to the target native application; based on the prompt method, matching the corresponding target parameter array from the JavaScript code according to the target function name; encapsulating the target function name, the target parameter array and the parameter value to generate the call request message.
[0013] In one embodiment of the present application, the target function name, the target parameter array and the parameter value are encapsulated to generate the call request information, including: assigning a value to each parameter in the target parameter array according to the parameter value to obtain the parameter value array; constructing a JSON object, the JSON object including a function name attribute and a parameter attribute; writing the target function name into the function name attribute, and writing the parameter value array into the parameter attribute to obtain a JSON string as the call request information.
[0014] In the second aspect, the present application provides a native application calling system, the system comprising: a creation module, used to pre-create a NativeFunctionForJs object, a WebChromeClient object and a JsCallNative object, wherein the NativeFunctionForJs object contains function information of the native application; an encoding module, used to use the JsCallNative object to read the function information in the NativeFunctionForJs object, and generate JavaScript code according to the function information; a writing module, used to write the JavaScript code into a web page through a network view, so that the web page generates a call request information according to the JavaScript code and the target native application to be called, wherein the call request information includes the target function name and parameter value array corresponding to the target native application; a calling module, used to use the WebChromeClient object to respond to the call request information, and execute the target function corresponding to the target function name according to the parameter value array to complete the call of the target native application.
[0015] In a third aspect, the present application provides an electronic device, comprising: one or more processors; a storage device for storing one or more programs, when the one or more programs are executed by one or more processors, the electronic device implements the native application calling method described in the first aspect.
[0016] In a fourth aspect, the present application provides a computer-readable storage medium having a computer program stored thereon. When the computer program is executed by a processor of a computer, the computer executes the native application calling method described in the first aspect.
[0017] As described above, the native application calling method, system, device and storage medium provided by the embodiments of the present application have the following beneficial effects:
[0018] First, in the NativeFunctionForJs object, the function information of the native application that needs to be exposed to the web page is defined, and then the JsCallNative object is used to generate JavaScript code according to the function information in the NativeFunctionForJs object. The JavaScript code is further written into the web page through the network view, so that the web page can generate call request information according to the JavaScript code and the target native application to be called. The call request information includes the target function name and the number of parameter values. Finally, the WebChromeClient object is used to execute the target function corresponding to the target function name according to the parameter value array to complete the native call. The JavaScript code is generated from the function information of the native application and injected into the web page, so that the web page can call the native application based on the JavaScript code, avoiding the restrictions on the interactive ability of the web page and the functional extension of the native application. In the process of realizing data interaction between the web page and the native application through WebView, both the flexibility of the native application call and the security of the native application call are improved.
[0019] It should be understood that the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the present application. BRIEF DESCRIPTION OF THE DRAWINGS
[0020] The drawings herein are incorporated into the specification and constitute a part of the specification, showing embodiments consistent with the present application, and together with the specification, are used to explain the principles of the present application. Obviously, the drawings described below are only some embodiments of the present application, and for those of ordinary skill in the art, other drawings can be obtained based on these drawings without creative work. In the drawings:
[0021] Figure 1 is a schematic diagram of an implementation environment of a native application calling system shown in an exemplary embodiment of the present application;
[0022] Figure 2 is a flowchart of a native application calling method shown in an exemplary embodiment of the present application;
[0023] Figure 3is a schematic diagram of a structure between objects shown in an exemplary embodiment of the present application;
[0024] Figure 4 is a flowchart of another native application calling method shown in an exemplary embodiment of the present application;
[0025] Figure 5 is a block diagram of a native application calling system shown in an exemplary embodiment of the present application;
[0026] Figure 6 It is a structural schematic diagram of an electronic device provided by an embodiment of the present application. DETAILED DESCRIPTION
[0027] The following will describe the implementation methods of the present application with reference to the accompanying drawings and preferred embodiments. Those skilled in the art can easily understand other advantages and effects of the present application from the contents disclosed in this specification. The present application can also be implemented or applied through other different specific implementation methods, and the details in this specification can also be modified or changed in various ways based on different viewpoints and applications without departing from the spirit of the present application. It should be understood that the preferred embodiments are only for illustrating the present application, not for limiting the scope of protection of the present application.
[0028] It should be noted that the illustrations provided in the following embodiments are only used to illustrate the basic concept of the present application in a schematic manner, and therefore the illustrations only show components related to the present application rather than being drawn according to the number, shape and size of components in actual implementation. In actual implementation, the type, quantity and proportion of each component may be changed arbitrarily, and the component layout may also be more complicated.
[0029] In the following description, a large number of details are discussed to provide a more thorough explanation of the embodiments of the present application. However, it is obvious to those skilled in the art that the embodiments of the present application can be implemented without these specific details. In other embodiments, well-known structures and devices are shown in the form of block diagrams rather than in detail to avoid making the embodiments of the present application difficult to understand.
[0030] Embedding web pages in WebView applications and controlling the loading and interaction of WebView through Java code combines the flexibility of the Internet with the performance advantages of native applications, enriching the functionality and user experience of applications. However, the inventors of this application have found that the above method still has defects in the data interaction between native applications and web pages, such as: 1. Lack of native function exposure. Functions in native applications cannot be directly exposed to web pages for use, which limits the interactive capabilities of web pages and the functional expansion of native applications; 2. Data interaction restrictions. Data between native applications and web pages can usually only be accessed through webView.addJavascriptInterface(), and interface callbacks cannot be implemented when calling native functions; 3. Interaction security issues. JavaScript code is executed in web pages, which may introduce security risks. Attackers can use the reflection mechanism to call any other unregistered Java classes, resulting in unlimited enhancement of JavaScript capabilities.
[0031] Therefore, see Figure 1 , Figure 1 FIG. 1 is a schematic diagram of an implementation environment of a native application calling system shown in an exemplary embodiment of the present application. Figure 1 As shown, the implementation environment includes a vehicle 110 and a native application calling system 120, wherein the native application calling system 120 can be embedded in the vehicle 110 to realize the interaction between each application in the client of the vehicle 110 and the web page, and the native application calling system 120 includes but is not limited to the vehicle system, the on-board computer, etc. The native application calling system 120 generates JavaScript code from the function information of the native application and injects it into the web page, so that the web page can call the native application based on the JavaScript code, avoiding the restriction on the interactive ability of the web page and the functional expansion of the native application. In realizing the data interaction between the web page and the native application through WebView, both the flexibility of the native application call and the security of the native application call are improved.
[0032] See also Figure 2 , Figure 2 is a flowchart of a native application calling method shown in an exemplary embodiment of the present application. The method can be applied to Figure 1 The implementation environment shown is, and it should be understood that the present application does not limit the implementation environment of the method.
[0033] like Figure 2 As shown, in an exemplary embodiment, the native application calling method includes at least steps S210 to S240, which are described in detail as follows:
[0034] Step S210 , pre-create a NativeFunctionForJs object, a WebChromeClient object, and a JsCallNative object, wherein the NativeFunctionForJs object includes function information of the native application.
[0035] It should be noted that the NativeFunctionForJs object defines the methods and implementations of the native application that need to be exposed to the web page JavaScript call, that is, the function information of the native application; the onProgressChanged method and onJsPrompt method are configured in the WebChromeClient object, which are used to monitor the web page loading progress and perceive the call of the prompt function in the JavaScript environment respectively; the JsCallNative object is used to generate JavaScript code and parse the call request information of the web page, execute the call of the native function, that is, parse the call parameters and execute the native method when the web page requests to call the native function. In addition, the function information contained in the NativeFunctionForJs object is the function information of the native application that can be called by the web page.
[0036] In an embodiment of the present application, the WebChromeClient object can intercept the call of the prompt method in the JavaScript environment and obtain the call parameters passed by the prompt method, thereby realizing the communication from the web page to the native application. WebView can inject JavaScript code into the web page and run JavaScript code by passing in JavaScript code through the loadUrl method, so as to achieve the purpose of native application to web page communication, thereby realizing the call of the web page to the native application. Among them, WebChromeClient is an auxiliary class of the WebView component, which provides methods related to the browser interface such as processing JavaScript dialog boxes, website icons, titles and loading progress, which is used to enhance the interactivity and user interface functions of WebView.
[0037] Step S220, using the JsCallNative object, read the function information in the NativeFunctionForJs object, and generate JavaScript code according to the function information.
[0038] It should be noted that the function information includes the function name and parameter array of the native application. For example, the function name is delayCallBackInJs, and the corresponding function is used to return the given string information to JavaScript through the callback function defined in JavaScript after a given delay time, and the parameter array includes the various parameters of the native function and their types. For example, if a calculator needs to be called for data calculation, the parameter array includes the number of calculated data and the type of each data. In addition, the JavaScript code contains the function information of the native application that can be called by the web page.
[0039] Specifically, the JsCallNative object is used to read the function information in the NativeFunctionForJs object, and JavaScript code is generated according to the function information, including: calling the generateJsCode method in the JsCallNative object, and parsing the function information in the NativeFunctionForJs object based on the reflection of the generateJsCode method; the corresponding function name of each native application is associated with the parameter array to obtain the JavaScript code.
[0040] In this embodiment, the generateJsCode method in the JsCallNative object has the function of automatically generating JavaScript code. By calling the generateJsCode method to reflectively parse all functions corresponding to NativeFunctionForJs, the automatic generation of JavaScript code containing function information of the native application that can be called by the web page is realized according to the functions in NativeFunctionForJs.
[0041] Step S230, writing the JavaScript code into the web page through the network view, so that the web page generates call request information according to the JavaScript code and the target native application to be called, wherein the call request information includes the target function name and parameter value array corresponding to the target native application.
[0042] It should be noted that the target function name is the function name used to call the target native application, and the parameter value array includes the values and types of each parameter of the target function. For example, if a calculator needs to be called to perform data calculations, the parameter value array includes the number of calculated data and the value and type of each data.
[0043] In this embodiment, JavaScript code is written to the web page through the loadUrl method of the network view, and the native application can flexibly expose functions and data to the web page for use, so that the web page can call the native application that exposes the native function, thereby improving the scalability and flexibility of the interaction between the two ends. In addition, when the JavaScript code is executed in the web page, the attacker cannot use the reflection mechanism to call any other unregistered Java class, thereby enhancing the security of the interaction.
[0044] In one embodiment, writing JavaScript code to a web page through a network view also includes: obtaining a web page address and loading a web page corresponding to the web page address; configuring the onProgressChanged method of the WebChromeClient object to monitor the loading progress of the web page; comparing the loading progress with a preset loading threshold; if the loading progress is greater than or equal to the loading progress threshold, calling the loadUrl method in the network view to perform the JavaScript code writing operation.
[0045] In this embodiment, WebView loads a web page with a given URL address, and monitors the loading progress of the web page through the onProgressChanged method of the WebChromeClient object, and determines whether to write the JavaScript code based on the loading progress, wherein the loading progress threshold can be set according to the specific situation. Preferably, the loading progress threshold is 20% to 30% of the total progress, so that the JavaScript code can be injected into the web page in an appropriate time period. In this way, the problem of global injection failure caused by too early injection, or the problem of affecting the execution efficiency of native calls due to too late injection can be avoided, thereby avoiding the native function calls made when some pages are initialized to wait too long.
[0046] In one embodiment, a web page generates a call request message based on the JavaScript code and the target native application to be called, including: in response to the native application call instruction, calling the prompt method in the JavaScript environment, wherein the native application call instruction carries the target function name and parameter value corresponding to the target native application; based on the prompt method, matching the corresponding target parameter array from the JavaScript code according to the target function name; encapsulating the target function name, target parameter array and parameter value to generate the call request message.
[0047] In this embodiment, the prompt method is a method used to interact with native applications in a JavaScript environment. After receiving a native application call instruction, the prompt method can match the corresponding target parameter array from the JavaScript code according to the target function name, and encapsulate the target function name, target parameter array and parameter value to obtain the call request information.
[0048] In one embodiment, the target function name, target parameter array and parameter value are encapsulated to generate call request information, including: assigning each parameter in the target parameter array according to the parameter value to obtain the parameter value array; constructing a JSON object, the JSON object including a function name attribute and a parameter attribute; writing the target function name into the function name attribute, and writing the parameter value array into the parameter attribute to obtain a JSON string as the call request information.
[0049] In this embodiment, the target parameter array is traversed according to the parameter value, each parameter is assigned a value to obtain a parameter value array, and then a JSON object is constructed, which includes at least two attributes: "functionName" and "arguments", namely the function name and parameters, which are respectively used to store the obtained target function name and the processed parameter value array, and finally the constructed JSON object is converted into a JSON string to obtain the call request information.
[0050] Step S240, using the WebChromeClient object, responding to the call request information, and executing the target function corresponding to the target function name according to the parameter value array to complete the call of the target native application.
[0051] After receiving the call request information from the web page, the native application side calls the corresponding target function based on the target function name and parameter value array in the call request information, thereby realizing the call of the target native application.
[0052] In one embodiment, a WebChromeClient object is used to respond to call request information, and a target function corresponding to the target function name is executed according to a parameter value array, including: pre-creating a WebChromeClient object and configuring the onJsPrompt method in the WebChromeClient object; activating the onJsPrompt method in response to JavaScript code; using the onJsPrompt method, calling the callNative method in the JsCallNative object, parsing the call request information, and obtaining the target function name and the parameter value array; reflectively calling the corresponding target function in the NativeFunctionForJs object based on the target function name, and executing the target function according to the parameter value array.
[0053] In this embodiment, the onJsPrompt method of the WebChromeClient object is activated when the JavaScript code is generated. The target function senses the call of the prompt method in the JavaScript environment in the onJsPrompt method, and calls the callNative function of the JsCallNative object through the onJsPrompt method to parse the JSON string passed by prompt, that is, to parse the target function name and parameter value array of the target native application called by the JavaScript request, and then calls the corresponding target native application in the NativeFunctionForJs object through reflection.
[0054] In one embodiment, the call request information also includes callback function information, and the callback function information includes the callback function name; after completing the call of the target native application, it also includes: creating a CallBackInJs object, and obtaining the callback function name and the call result of the target native application, wherein the CallBackInJs object is configured with a callback method; calling the callback method, encapsulating the callback function name and the call result, and generating a callback code; using the loadUrl method in the network view, executing the callback code, and calling the corresponding callback function in the JavaScript environment according to the callback function name, so that the web page calls back the call result.
[0055] In this embodiment, the CallBackInJs object is used to encapsulate the callback function name and call result defined in JavaScript, and after the native function is executed and the result is obtained, the result is returned to the web page in JSON format. If the call request information includes JavaScript callback function information, the corresponding JavaScript callback function will be executed through the CallBackInJs object, so that JavaScript can asynchronously call the native application to obtain the call result, and the result callback when calling the native application is realized.
[0056] In a possible embodiment, multiple native applications can be called simultaneously by the web page based on multiple call request information sent by the web page segment.
[0057] See also Figure 3 , Figure 3 is a schematic diagram of a structure between objects shown in an exemplary embodiment of the present application. Figure 3As shown in the figure, NativeFunctionForJs object, WebChromeClient object, JsCallNative object and CallBackInJs object are created on the native application side, as detailed below:
[0058] The WebChromeClient object, i.e., the web browser auxiliary client, is an auxiliary class of the WebView component on Android (Android), which can enhance the functionality of WebView by implementing InjectedChromeClient (i.e., the injected web browser auxiliary client). To implement InjectedChromeClient, you need to inherit the WebChromeClient object and rewrite the relevant methods to include the required functions. In an embodiment of the present application, the onProgressChanged method and the onJsPrompt method are rewritten, corresponding to the web page loading progress change method and the JavaScript prompt function method, i.e., onProgressChanged(WebView, int): void and onJsPrompt(WebView, String, String, String, JsPromptResult): Boolean, which are used to monitor the web page loading progress and intercept the prompt method (i.e., prompt function) execution of JavaScript (i.e., js). Among them, InjectedChromeClient has an inheritance relationship with the WebChromeClient object and an association relationship with the NativeFunctionForJs object.
[0059] The NativeFunctionForJs object is a native function provided for JavaScript, which defines the native methods and implementations that need to be exposed to the JavaScript environment, such as toast(String):void and delayCallBackInJs(long,String,CallBackInJs), which are toast and delayed callback JavaScript respectively. Toast is a lightweight feedback or prompt.
[0060] The JsCallNative object, that is, JavaScript calls native, is used to generate JavaScript injection code and parse JavaScript calls native requests, which correspond to generating JavaScript code and calling native functions, namely generateJsCode(Class):String and callNative(WebView,String,String):String.
[0061] The CallBackInJs object, i.e. callback in JavaScript, includes a callback function, i.e. callback(Object...):void, which is used to process the callback of js (i.e. JavaScript).
[0062] Please continue to see Figure 3 , the InjectedChromeClient and WebChromeClient objects are in an inheritance relationship, the InjectedChromeClient and NativeFunctionForJs objects and JsCallNative objects are in an association relationship, and the JsCallNative object and NativeFunctionForJs objects and CallBackInJs objects are in an association relationship.
[0063] See also Figure 4 , Figure 4 FIG. 1 is a flowchart of another native application calling method shown in an exemplary embodiment of the present application. Figure 4 As shown in the figure, the native application corresponding to the web page calling the native function delayCallBackInJs(long,String,CallBackInJs) is taken as an example to demonstrate how to implement JavaScript calling the native application and obtain the result in an asynchronous way. The specific steps are as follows:
[0064] 1. Set the "web browser auxiliary client" (i.e. WebChromeClient object) to the browser control (i.e. WebView);
[0065] 2. The browser control loads the web page corresponding to the given uniform resource locator (i.e., URL address);
[0066] 3. Monitor the progress of the web page through the "web browser auxiliary client", and when the progress is > 25%, call the "generate JavaScript code" method (i.e., generateJsCode method) of the "JavaScript call native" (i.e., JsCallNative object), read the function information in the "native function provided for JavaScript" (i.e., NativeFunctionForJs object), generate JavaScript code, and inject the JavaScript code into the web page through the loadUrl function of the browser control;
[0067] 4. After the webpage is loaded, the prompt method is called in the JavaScript environment to apply for calling the "delay callback in JavaScript" (i.e., delayCallBackInJs function) of the native function. The call request information is a Json format string, which contains: a. the name of the native function called "delayCallBackInJs", b. the parameter array of the native function called, and c. the parameter value. The callback function defined in JavaScript will be saved in the array defined in JavaScript, and the corresponding subscript of the callback function in the array will be passed to the native function as a parameter;
[0068] 5. The "JavaScript prompt function" (i.e., onJsPrompt method) of the "web browser auxiliary client" is called and activated. In the onJsPrompt function, the "call native function" method (i.e., callNative method) of the "JavaScript call native" is called to respond to the call request;
[0069] 6. The "Call Native Function" method parses the Json format call request information passed from the web page, parses the function name and parameters of the JavaScript request call, and calls the "delay callback JavaScript" function (i.e., delayCallBackInJs function) of the "native function provided for JavaScript" through reflection;
[0070] 7. The “native function provided for JavaScript” executes the “delayed callback JavaScript” function, and calls the callback function defined in JavaScript for receiving the result through the “load uniform resource locator” method (i.e., loadUrl method) of the browser control after a given delay time;
[0071] 8. The callback function defined in the JavaScript on the web page is executed, and the execution result information returned by the native function is successfully obtained.
[0072] The above-mentioned native application calling method first defines the function information of the native application that needs to be exposed to the web page in the NativeFunctionForJs object, and then uses the JsCallNative object to generate JavaScript code according to the function information in the NativeFunctionForJs object, and further writes the JavaScript code into the web page through the network view, so that the web page can generate call request information according to the JavaScript code and the target native application to be called, and the call request information includes the target function name and the number of parameter values, and finally uses the WebChromeClient object to execute the target function corresponding to the target function name according to the parameter value array to complete the native call, so that the function information of the native application is generated by JavaScript code and injected into the web page, so that the web page can call the native application based on JavaScript code, avoiding the restriction on the interactive ability of the web page and the functional extension of the native application. In realizing the data interaction between the web page and the native application through WebView, both the flexibility of the native application call and the security of the native application call are improved.
[0073] See also Figure 5 , Figure 5 is a block diagram of a native application calling system shown in an exemplary embodiment of the present application. The system can be applied to Figure 1 The implementation environment shown is, and it should be understood that the present application does not limit the implementation environment of the system.
[0074] like Figure 5 As shown, in an exemplary embodiment, the native application calling system 500 includes at least a creation module 510, an encoding module 520, a writing module 530 and a calling module 540, which are described in detail as follows:
[0075] A creation module 510 is used to pre-create a NativeFunctionForJs object and a JsCallNative object, wherein the NativeFunctionForJs object contains function information of the native application;
[0076] The encoding module 520 is used to use the JsCallNative object to read the function information in the NativeFunctionForJs object and generate JavaScript code according to the function information;
[0077] The writing module 530 is used to write the JavaScript code into the web page through the network view, so that the web page generates a call request information according to the JavaScript code and the target native application to be called, wherein the call request information includes the target function name and parameter value array corresponding to the target native application;
[0078] The calling module 540 is used to use the JsCallNative object to respond to the calling request information, and execute the target function corresponding to the target function name according to the parameter value array to complete the calling of the target native application.
[0079] It should be noted that the native application calling system provided in the above embodiment and the native application calling method provided in the above embodiment belong to the same concept, wherein the contents of the operations performed by each module have been described in detail in the method embodiment and will not be repeated here.
[0080] See also Figure 6 , Figure 6 It is a structural schematic diagram of an electronic device provided by an embodiment of the present application. Figure 6 The structure diagram of the computer system suitable for implementing the electronic device of the embodiment of the present application is shown. It should be noted that: Figure 6 The computer system 600 of the electronic device shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present application.
[0081] like Figure 6 As shown, the computer system 600 includes a central processing unit (CPU) 601, which can perform various appropriate actions and processes according to the program stored in the read-only memory (ROM) 602 or the program loaded from the storage part 608 to the random access memory (RAM) 603, such as executing the method in the above embodiment. In the RAM 603, various programs and data required for system operation are also stored. The CPU 601, ROM 602 and RAM 603 are connected to each other through a bus 604. An input / output (I / O) interface 605 is also connected to the bus 604.
[0082] The following components are connected to the I / O interface 605: an input section 606 including a keyboard, a mouse, etc.; an output section 607 including a cathode ray tube (CRT), a liquid crystal display (LCD), etc., and a speaker, etc.; a storage section 608 including a hard disk, etc.; and a communication section 609 including a network interface card such as a LAN (Local Area Network) card, a modem, etc. The communication section 609 performs communication processing via a network such as the Internet. A drive 610 is also connected to the I / O interface 605 as needed. A removable medium 611, such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, etc., is installed on the drive 610 as needed so that a computer program read therefrom is installed into the storage section 608 as needed.
[0083] In particular, according to an embodiment of the present application, the process described above with reference to the flowchart can be implemented as a computer software program. For example, an embodiment of the present application includes a computer program product, which includes a computer program carried on a computer-readable medium, and the computer program includes a computer program for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from a network through a communication section 609, and / or installed from a removable medium 611. When the computer program is executed by a central processing unit (CPU) 601, various functions defined in the system of the present application are executed.
[0084] The present application also provides a computer-readable storage medium on which a computer program is stored. When the computer program is executed by a processor of a computer, the computer executes the native application calling method as described above. The computer-readable storage medium may be included in the electronic device described in the above embodiment, or may exist independently without being assembled into the electronic device.
[0085] It should be noted that the computer-readable medium shown in the embodiment of the present application can be a computer-readable signal medium or a computer-readable storage medium or any combination of the above two. The computer-readable storage medium can be, for example, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples of computer-readable storage media can include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM), a flash memory, an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present application, a computer-readable signal medium may include a data signal propagated in a baseband or as part of a carrier wave, wherein a computer-readable computer program is carried. This propagated data signal can take a variety of forms, including but not limited to an electromagnetic signal, an optical signal, or any suitable combination of the above. A computer-readable signal medium may also be any computer-readable medium other than a computer-readable storage medium, which may send, propagate or transmit a program for use by or in conjunction with an instruction execution system, apparatus or device. A computer program contained on a computer-readable medium may be transmitted using any appropriate medium, including but not limited to: wireless, wired, etc., or any suitable combination of the above.
[0086] The flowchart and block diagram in the accompanying drawings illustrate the possible architecture, functions and operations of the system, method and computer program product according to various embodiments of the present application. Wherein, each box in the flowchart or block diagram can represent a module, a program segment, or a part of the code, and the above-mentioned module, program segment, or a part of the code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in a different order from the order marked in the accompanying drawings. For example, two boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram or flowchart, and the combination of boxes in the block diagram or flowchart can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.
[0087] The units involved in the embodiments described in this application may be implemented by software or hardware, and the units described may also be set in a processor. The names of these units do not constitute limitations on the units themselves in some cases.
[0088] The above embodiments are merely illustrative of the principles and effects of the present application and are not intended to limit the present application. Anyone familiar with the technology may modify or change the above embodiments without violating the spirit and scope of the present application. Therefore, all equivalent modifications or changes made by a person with ordinary knowledge in the technical field without departing from the spirit and technical ideas disclosed in the present application shall still be covered by the claims of the present application.
Claims
1. A native application calling method, It is characterized in that The method comprises: Pre-create a NativeFunctionForJs object, a WebChromeClient object, and a JsCallNative object, wherein the NativeFunctionForJs object contains function information of the native application; Using the JsCallNative object, read the function information in the NativeFunctionForJs object, and generate JavaScript code according to the function information; Writing the JavaScript code into a web page through a network view, so that the web page generates call request information according to the JavaScript code and the target native application to be called, wherein the call request information includes a target function name and a parameter value array corresponding to the target native application; The WebChromeClient object is used to respond to the call request information, and the target function corresponding to the target function name is executed according to the parameter value array to complete the call of the target native application.
2. The native application calling method according to claim 1, It is characterized in that The using the JsCallNative object to read the function information in the NativeFunctionForJs object and generating JavaScript code according to the function information includes: Calling the generateJsCode method in the JsCallNative object, and reflecting and parsing the function information in the NativeFunctionForJs object based on the generateJsCode method, wherein the function information includes the function name and parameter array of the native application; The function name corresponding to each of the native applications is associated with the parameter array to obtain the JavaScript code.
3. The native application calling method according to claim 2, It is characterized in that The WebChromeClient object responds to the call request information and executes the target function corresponding to the target function name according to the parameter value array, including: Pre-create a WebChromeClient object and configure the onJsPrompt method in the WebChromeClient object; In response to the JavaScript code, activating the onJsPrompt method; Using the onJsPrompt method, call the callNative method in the JsCallNative object, parse the call request information, and obtain the target function name and the parameter value array; The corresponding target function in the NativeFunctionForJs object is reflectively called based on the target function name, and the target function is executed according to the parameter value array.
4. The native application calling method according to claim 1, It is characterized in that The call request information also includes callback function information, and the callback function information includes the callback function name; After completing the calling of the target native application, the method further includes: Creating a CallBackInJs object, and obtaining the callback function name and the calling result of the target native application, wherein the CallBackInJs object is configured with a callback method; Calling the callback method, encapsulating the callback function name and the calling result, and generating a callback code; The loadUrl method in the network view is used to execute the callback code, and the corresponding callback function in the JavaScript environment is called according to the callback function name, so that the web page calls back the calling result.
5. The native application calling method according to any one of claims 1 to 4, It is characterized in that Writing the JavaScript code into the web page through the web view also includes: Obtaining a web page address and loading a web page corresponding to the web page address; Configure the onProgressChanged method of the WebChromeClient object to monitor the loading progress of the web page; Comparing the loading progress with a preset loading progress threshold; If the loading progress is greater than or equal to the loading progress threshold, the loadUrl method in the network view is called to perform a writing operation of the JavaScript code.
6. The native application calling method according to claim 5, It is characterized in that The webpage generates call request information according to the JavaScript code and the target native application to be called, including: In response to a native application calling instruction, calling a prompt method in a JavaScript environment, wherein the native application calling instruction carries the target function name and parameter value corresponding to the target native application; Based on the prompt method, matching the corresponding target parameter array from the JavaScript code according to the target function name; The target function name, the target parameter array and the parameter value are encapsulated to generate the call request information.
7. The native application calling method according to claim 6, It is characterized in that The encapsulating the target function name, the target parameter array and the parameter value to generate the call request information includes: Assign a value to each parameter in the target parameter array according to the parameter value to obtain the parameter value array; Construct a JSON object, wherein the JSON object includes a function name attribute and a parameter attribute; The target function name is written into the function name attribute, and the parameter value array is written into the parameter attribute to obtain a JSON string as the call request information.
8. A native application calling system, It is characterized in that The device comprises: A creation module is used to pre-create a NativeFunctionForJs object, a WebChromeClient object, and a JsCallNative object, wherein the NativeFunctionForJs object contains function information of the native application; An encoding module, used to use the JsCallNative object to read the function information in the NativeFunctionForJs object, and generate JavaScript code according to the function information; A writing module, used to write the JavaScript code into a web page through a network view, so that the web page generates a call request information according to the JavaScript code and the target native application to be called, wherein the call request information includes a target function name and a parameter value array corresponding to the target native application; The calling module is used to utilize the WebChromeClient object to respond to the calling request information, and execute the target function corresponding to the target function name according to the parameter value array to complete the calling of the target native application.
9. An electronic device, It is characterized in that include: one or more processors; A storage device for storing one or more programs, which, when executed by the one or more processors, enables the electronic device to implement the native application calling method as described in any one of claims 1 to 7.
10. A computer-readable storage medium, It is characterized in that Computer-readable instructions are stored thereon, and when the computer-readable instructions are executed by a processor of a computer, the computer is caused to execute the native application calling method described in any one of claims 1 to 7.
