Android's WebView calls java code through JS

When working on a project, we will encounter a web that we use WebView to open, hoping that the web can call some of its own methods. For example, we are entering a web page, and then when we click a button on the web, we hope to be able to judge whether the current mobile terminal is not. Already logged in, if not logged in, it will jump to the login page (the login page is another Activity). At this time, a simple way is to call the java method on the js of the button action event, so as to judge whether to log in and decide whether to jump to another page.

Google's WebView provides us with the addJavascriptInterface(Object obj, String interfaceName) method. The first parameter of this method is the object passed to the Web, and the second parameter is the object name of the object.

public class WebActivity extends Activity{

    ProgressBar mProgressBar;
    WebView mWebView;
    String mUrl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView(R.layout.web);
        mWebView = (WebView) findViewById(R.id.web_view);
        mProgressBar = (ProgressBar) findViewById(R.id.loading_progress);
        doWebViewSetting();
// load the page
        loadUrl("http://172.10.1.2:8080/test");
    }

    private void doWebViewSetting(){
// Set WebClient (not required)
        mWebView.setWebViewClient (new MyWebViewClient ());
// support js (required)
        mWebView.getSettings().setJavaScriptEnabled(true);
// Add js object (necessary)
        mWebView.addJavascriptInterface(new JsOperation(this), "client");
    }

    private void loadUrl(String url) {
        mUrl = url;
        mProgressBar.setVisibility(View.VISIBLE);
        mWebView.loadUrl(url);
    }


    class MyWebViewClient extends WebViewClient{
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            mProgressBar.setVisibility(View.GONE);
        }
    }
    class JsOperation {

        Activity mActivity;

        public JsOperation(Activity activity) {
            mActivity = activity;
        }

// testing method
       @javascriptInterface
        public void test() {
            Toast.makeText(mActivity,"test",Toast.LENGTH_SHORT).show();
        }
    }

 The above is my WebActivity, I only simply implement the progress bar display and provide objects to js when loading. It can be seen that we provide a JsOperation object to the web, and then define the test() method in the JsOperation class. Next, let's see how the Web side calls this method.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Test</title>
    <script type="text/javascript">
        function test(){
            client.test()
        }

    </script>
</head>
<body>
<br />
<button onclick="test()">test</button>
</body>
</html>

 The implementation of the web is very simple. It directly calls the object we provide. It should be noted here that the object name is the String we passed in before, here is the client, and a Toast can be popped up by directly calling its test() method. Of course, you can also do other kinds of processing in this method, and you can also provide multiple methods. If you need to pass in parameters, after defining the formal parameters on java, js will call the method normally and pass in the parameters. , such as client.test(param), there is nothing special to pay attention to.

 

Special attention: Android's WebView has security vulnerabilities, because the js calling object can call Runtime through reflection and may even control the host in the form of a web hanging horse. Google also admits that the function of webView is too powerful, giving the web a lot of power. Android 4.2 (api17) has begun to adopt a new interface method, @JavascriptInterface instead of addjavascriptInterface, but it is unavoidable in lower versions.

Guess you like

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