Android studio calls js library

To call the written JavaScript module in Android Studio, technologies such as WebView or js-engine can be used. The two methods are described below:

 

Method 1. Use WebView

 

1. Add the WebView control to the layout file

 

Add a WebView control to the layout file of the Activity that needs to call the JavaScript module, for example:

 

```xml

<WebView

    android:id="@+id/webview"

    android:layout_width="match_parent"

    android:layout_height="match_parent" />

```

 

2. Set WebViewClient and WebChromeClient of WebView

 

In the code of Activity, you need to set WebViewClient and WebChromeClient of WebView. WebViewClient is used to load web pages, and WebChromeClient is used to handle JavaScript pop-up boxes such as alert, confirm and prompt. For example:

 

```java

WebView webView = findViewById(R.id.webview);

webView.setWebViewClient(new WebViewClient());

webView.setWebChromeClient(new WebChromeClient());

```

 

3. Load the HTML file

 

Place the written JavaScript module in the assets directory of the project, and then load the HTML file in WebView to access the JavaScript module. For example:

 

```java

webView.loadUrl("file:///android_asset/my_module.html");

```

 

In the HTML file, JavaScript modules can be imported through the script tag, and the functions in the module can be exposed to the Android application through the window object. For example:

 

```html

<script src="my_module.js"></script>

<script>

    window.myFunction = my_module.myFunction;

</script>

```

 

Method 2: Use js-engine

 

js-engine is a library that embeds JavaScript on the Android platform, and can directly call JavaScript functions in Java code. Here is a simple example:

 

1. Add dependencies

 

In the project's build.gradle file, add the dependency of js-engine. For example:

 

```gradle

dependencies {

    implementation "com.eclipsesource.j2v8:j2v8:4.6.0"

}

```

 

2. Load the JavaScript module

 

In the Activity, load the JavaScript module and export the functions that need to be called from it. For example:

 

```java

String jsModule = MyUtils.readAssetFile(this, "my_module.js");

 

V8 v8 = new V8();

v8.executeScript(jsModule);

V8Object module = v8.getObject("my_module");

V8Function function = module.getFunction("myFunction");

 

// export function name

function.registerJavaMethod(new MyFunction(), "myRunnable");

```

 

In the above code, the MyUtils.readAssetFile method is used to read the file content in the assets directory. myRunnable is a Java interface used to perform an operation in V8, which can be modified according to the actual situation. For example:

 

```java

public interface MyFunction {

    void myRunnable();

}

```

 

3. Call the JavaScript function

 

In code that needs to call a JavaScript function, use V8 to execute the function exported above. For example:

 

```java

function.call(null, new V8Object[] {});

```

 

The above is the method of calling the written JavaScript module, I hope it can help you.

Guess you like

Origin blog.csdn.net/weixin_59246157/article/details/130773571