Tag:vue.jsandroidjavascript
Article From:https://segmentfault.com/q/1010000012145950
Question:


I need to click a button to send a parameter to Android. Android colleagues tell me that this method can be used, but the result, as shown in Figure two, is wrong, and how to solve it.

Answer 0:

Your colleague is probably already using JavascriptInterface in Android.wvc This object, but this object can only be effective in your application on Android. It is recommended that you communicate well.

Answer 1:

windowNo WVC is the object of the original, let alone the command. In collaboration with Android, the name of the method called and the parameters to be passed are needed.

Answer 2:

You can first customize a JavaScriptInterface

inner class JavaScriptInterface {

    @JavascriptInterface
    fun hello(string: String) {
        print("Hello Word")
    }
}

Then use the addJavascriptInterface method of WebView

addJavascriptInterface(JavaScriptInterface(), "interface")

Finally, in JavaScript, it can be direct

window.interface.hello();

Answer 3:

Use dsbrige to interact with Android.
http://www.jianshu.com/p/3946…
Download dsbrige through CDN or NPM, where you need to use VaR dsBrige=require (‘dsbrige’);
dsBrige.call(‘The way Android provides’, {
msg:’You pass a Android parameter ‘)

Answer 4:

Js Calling the Java code on the Android side requires the Android side to write the communication interface:

public class YourJavaScriptInterface {
    @JavascriptInterface        //The use of this annotation indicates that the method can be invoked by JS toPublic void command (string: String) {}}

Bind the communication interface to the WebView:

webView.addJavascriptInterface(new YourJavaScriptInterface(), xxx);  //The second parameter is the name of the exposure to JS

WebView To allow the execution of JS:

webView.getSettings().setJavaScriptEnabled(true);

Then the JS side can be invoked to the Android side in the following way:

window.xxx.command('hello');

The basic interaction between Android and Js can refer to a blog written by me: the interaction between Android and Javascript.

Leave a Reply

Your email address will not be published. Required fields are marked *