[Android Advanced] Intermodulation method between Android js and Android

insert image description here

1. Create an HTML URL for testing

webView.html
1.<html>
     <head>
         <title>
         </title>
     </head>
    <body>
<input type="button" value="Say hello" onClick="showAndroidToast('Web传递参数到Android')" />

<script type="text/javascript">
    function test(card_mo ,price){
    Android.showtest(card_mo+price);
    }
    function showAndroidToast(toast) {
        Android.showToast(toast);
    }
</script>

   </body>
</html>

2. Create a JavaScriptInterface interface class

package com.ruidde.csndresourcedemo;

import android.content.Context;
import android.webkit.JavascriptInterface;
import android.widget.Toast;

/**
 * Created by Administrator on 2017/1/5.
 */

public class JavaScriptInterface {
    Context mContext;

    /** Instantiate the interface and set the context */
    JavaScriptInterface(Context c) {
        mContext = c;
    }
    @JavascriptInterface
    /** Show a toast from the web page */
    public void showToast(String toast) {
        Toast.makeText(mContext, toast+"0000000", Toast.LENGTH_SHORT).show();
    }
    @JavascriptInterface
    /** Show a toast from the web page */
    public void showtest(String toast) {
        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
    }
}

3. Use in Activity

package com.ruidde.csndresourcedemo;

import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    private String card_mo ,price ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        card_mo = "1234565";
        price = "32.00";

        WebView myWebView = (WebView) findViewById(R.id.webview);
        //设置js可用
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        //注解自定义对象 js用自定义的对象如:Android 来调用Android中方法
        myWebView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
        //网址本地自己写的
        myWebView.loadUrl("file:///android_asset/webView.html");
        //webView网页加载进度
        myWebView.setWebChromeClient(new WebChromeClient()
        {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if (newProgress == 100){ //网页加载完成
                    /**
                     *   //这是Android调用js的方法  如:test方法在js中要有(可以给js中传递参数)
                     *   方法在web加载完成后  会调用下面方法来给js传递参数
                     * */
                    view.loadUrl("javascript:test('" + card_mo+ "','" + price+ "')"); //aa是js的函数test()的参数
                }
            }
        }
        );
    }
  }

Scan the QR code to get more advanced knowledge of Android for free!

" Advanced Information on Eight Modules of Android "

Here I would like to share with you a set of "Advanced Information on Eight Modules of Android" written by senior architects of Ali, to help you organize the messy, scattered and fragmented knowledge systematically, so that you can systematically and efficiently master all aspects of Android development. knowledge points.

Content introduction : "Necessary skills for architects to build a foundation", "Analysis of the source code of the top 100 frameworks of Android", "Analysis of actual combat of Android performance optimization", "Advanced kotlin strengthening actual combat", "Advanced decryption of Android advanced UI open source framework", "NDK Module Development", "Flutter Technology Advancement", "WeChat Mini Program Development". A full set of video materials is attached, including interview collection, source code collection, and open source framework collection.

Due to the large content of the article and the limited space, the information has been organized into PDF documents. If you need the complete document of "Android Eight Modules Advanced Information", you can add WeChat to get it for free!

Table of contents

img

1. Essential Skills for Architects

1. In-depth understanding of Java generics 2. Annotations in simple terms 3. Concurrent programming 4. Data transmission and serialization 5. Java virtual machine principles 6. Efficient IO...img

2. Source Code Analysis of Top 100 Android Frameworks

1.Retrofit 2.0 source code analysis2.Okhttp3 source code analysis3.ButterKnife source code analysis4.MPAndroidChart source code analysis5.Glide source code analysis6.Leakcanary source code analysis7.Universal-lmage-Loader source code analysis8.EventBus 3.0 source code analysis9.zxing source code Analysis 10. Picasso source code analysis 11. LottieAndroid use detailed explanation and source code analysis 12. Fresco source code analysis - picture loading processimg

3. Actual analysis of Android performance optimization

1. Tencent Bugly: A Little Understanding of String Matching Algorithms

2. iQiyi: Android APP crash capture solution - xCrash

3. Bytedance: In-depth understanding of one of the Gradle frameworks: Plugin, Extension, buildSrc

4. Baidu APP technology: Android H5 first screen optimization practice

5. Analysis of Alipay client architecture: Android client startup speed optimization "garbage collection"

6. Ctrip: From the Zhixing Android project to see the practice of component architecture

7. Netease news construction optimization: how to make your construction speed "like lightning"?

img

4. Advanced kotlin strengthens actual combat

Guess you like

Origin blog.csdn.net/Misdirection_XG/article/details/130063726