Android interacts with JS (pass by value and value callback)

At present, there is such a small function, that is, the Android side uses MD5 in JS for encryption. The specific requirement is that the Android side passes a value (password plaintext) to the JS method, and then calls the method in JS to encrypt and then calls back the ciphertext to Android side

The local loading of the html page here, first we have to write a local html page, and then add some of our java entity class code to call back, similar to the role of herbinate.
Below is the code of the html I wrote:

<html >
<head>
 <meta charset="UTF-8">
 <title>Title</title>
 <h1>android js md5 加密</h1>
 <script src="file:///android_asset/jquery.min.js"></script>
 <script type="text/javascript" src="file:///android_asset/md5.js"></script>
 <script type="text/javascript">
         function ok(pass)
         {
            var psd=$.md5(pass);
            android.toastMessage(psd);
         }
</script>
</head>
<body>
<button  value="点击" onclick="ok('')"></button>
</body>
</html>

Then our specific content on the Android side is as follows:

 {
 webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("file:///android_asset/test.html");
        webView.addJavascriptInterface(new MyJsMd5(), "android");
 webView.loadUrl("javascript:ok('"+mpassword+"')");

    class MyJsMd5{
        @JavascriptInterface
        public void toastMessage(String message) {
            Log.i("TAG" , "传递过来的值是: "+message);
        }
    }


In this way, we can pass the value to the JS method, and then return the result through our Class class

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325401439&siteId=291194637