Android teaches you how to interact with WebView and JS in five minutes

Android teaches you how to interact with WebView and JS in five minutes

[[189497]]

Background: Android API provides WebView component to realize HTML rendering. Now HTML5, CSS3, JS related development technology, and data exchange format json/XML. Skills of Web development engineers. In order to reduce excessive dependence on Android, some HTML is usually embedded in native Android.

In this way, js data interaction is inevitable.

We create a simple layout with only one webview control

  1. <android:id= "@+id/webView"  
  2. android:layout_width= "fill_parent"  
  3. android:layout_height= "wrap_content"  
  4. />

We find this webView and load the local main.html, where main.html is stored in the assets root directory of the project file.

  1. WebView webView = (WebView) findViewById(R.id.webView);
  2. webView.addJavascriptInterface(newJSObject(context), "aikaifa" );
  3. WebSettings webSettings = webView.getSettings();
  4. webSettings.setJavaScriptEnabled( true );//Support js
  5. Handler handler = new Handler();
  6. handler.postDelayed(new Runnable() {
  7. public void run() {
  8. webView.loadUrl( "file:///android_asset/main.html" );
  9. }
  10. }, 500);

1.js calls Android method

If you are careful, you should notice that there is a JSObject class above. Yes, this is the entry point, so that the front-end js can call the methods written on our Android side. The "aikaifa" can be understood as a mark, which can be changed to whatever you like.

Let's take a look at the JSObject class:

  1. public class JSObject {
  2. private Context context;
  3. public JSObject(Context context){
  4. this.context=context;
  5. }
  6. @JavascriptInterface
  7. public void goBack({
  8. Activity activity = (Activity) context;
  9. activity.finish();
  10. }
  11. }

This class defines the goBack method, which can be called by injecting JS.

If you want to call the goBack method in js, you can call it like this: aikaifa.goBack().

In this way, the front end can easily call our Android methods.

2.Android calls JS method

Since js can call our methods, we also call js methods

For example, we want to call the getName method in js.

We can write it like this.

  1. public void getTestJS()
  2. {
  3. Timertimer = new Timer();
  4. final Handler handler = new Handler(){
  5. public void handleMessage(Message msg) {
  6. switch (msg.what) {
  7. case 1:
  8. webView.loadUrl( "javascript:getName()" );
  9. break;
  10. }
  11. super.handleMessage(msg);
  12. }
  13. };
  14. timer.schedule(new TimerTask() {
  15. public void run() {
  16. Message msg = new Message();
  17. msg.what = 1;
  18. handler.sendMessage(msg);
  19. }
  20. }, 500, 500);
  21. }

The getName method is a method in js, and we can call the getTestJS method where needed.

In this way, the interaction between WebView and js is roughly completed.

[This article is an original article by 51CTO columnist "Hong Shengpeng". Please contact the original author for reprinting.]

Click here to read more articles by this author

<<:  iOS AFNetworking framework HTTPS request configuration

>>:  Aiti Tribe Story Collection (16): Best Practices for Technical People During Their Fatigue Period

Recommend

Inside iOS 9.3.5: A detailed look at the biggest vulnerability in iOS history

This morning, Apple suddenly released iOS 9.3.5. T...

You must know these 3 mainstream APP promotion methods!

In recent years, with the development and growth ...

What brand advantages are needed to use low-price strategies for marketing?

Many people believe that if the product has the s...

Advertising cases in the automotive service industry!

In October this year, Shanghai's auto beauty ...

5 tips for brand marketing positioning!

There are many ways to position yourself, but the...

How to use Baidu experience for online promotion?

We all know that there are many ways of online pr...

Advertising and Marketing Case Analysis Skills

Looking at external cases is a basic skill for ma...

Say Goodbye to Singleness: When Love Knocks on the Door Baidu Cloud Download

Say Goodbye to Singleness: When Love Knocks on th...

Information flow methodology helps you reduce costs by 40%!

There are a lot of form leads, but few transactio...