In the process of APP development, in order to achieve page flexibility and high efficiency of development, native and H5 intermodulation is a very common means. Here is a brief introduction to native JS intermodulation on Android:

JS to Android

Android WebView initialization:

webView = new WebView(this); WebSettings settings = webView.getSettings(); / / set support javaScript footsteps language Settings. SetJavaScriptEnabled (true); / / support double-clicking - premise is to support to display Settings page. SetUseWideViewPort (true); / / support zoom button - premise is to support to display Settings page. SetBuiltInZoomControls (true); Webview.setwebviewclient (new WebViewClient())); // Load the webview. loadUrl("");

Copy the code

Android register JavascriptInterface

* This method takes two arguments: 1. The class being called; 2. JS invoked in the instance objects of a class name * / webView addJavascriptInterface (new JsCallAndroid (),"Android");
Copy the code

Note: The object name must correspond to the name of onclick on the H5 page.

Android is called JsCallAndroid

Private class JsCallAndroid {/** * javascript method */ @javascriptInterface public voidhello() {
    Toast.makeText(JsCallAndroid.this, "JS call native", Toast.LENGTH_SHORT).show(); }}Copy the code

H5 code sample

<! Call native Android code :onclick ="Window.+ object name passed by Android.+ method in object"-->
<input type="button" value=JS calls "Android" onclick="window.Android.hello()"/>
Copy the code

2. Native JS

Android code Examples

/** * In this form: javascript:Js file method name (parameter) * AndroidCallJs() below; If the argument is a variable, pass it as a concatenated string */ webview.loadurl ("javascript:window.AndroidCallJs" + "("+hello+")");
Copy the code

H5 code sample

<script type="text/javascript">
    function AndroidCallJs(arg){
         console.log("Native JS");
    }
</script>
Copy the code


Summary: Android H5 intermodulation is basically similar to iOS H5 intermodulation. Here is only a simple android H5 intermodulation code, have a question of children’s shoes welcome to discuss, or you can also refer to this blog, may help you.

Next, let me make a note of a few minor issues I encountered during android development. I have encountered a lot of problems during Android development, but I didn’t record them and soon forgot them, so NOW I have to check them again every time I use them, so here is a brief note…


Android development process encountered problems

  • Because the simulator installed before is quite old, there is a problem when interacting with JS, so it needs to be updated. However, the problem of waiting for target deviceto come online is reported when the simulator is updated.
This problem is caused by virtual machine, so go to AVD Manager to resolve the first step: shut down the emulator; Step 2: Open the AVD Manager and find a new simulator with a Cold Boot Now option. Click, the simulator opens and a message is prompted. Step 3: Just re-run the app.Copy the code
  • Error in writing method: “Error: unreported Exception error Exception; It must be caught or declared to throw “, throws Exception after the method.
public void getUserInfo(String msg) throws Exception {
    
}

Copy the code
  • Encountered “error: accessing local variable onSuccess from inner class; Need to be declared final “, preceded by the final keyword.

  • Encountered “Error: unreported exception error JSONException; It must be caught or declared for throwing.”

Try {// String To JSONObject JSONObject result = new JSONObject(STR); Result.get ("command_type");
                
} catch (JSONException e) {
    e.printStackTrace();
}
Copy the code

Android JS intermodulation & some problems encountered in android development process

For more Articles, see Articles