0

Now I need to use webview component to load a HTML page and deal with component function. The content in HTML page is ,

you can see there is a multi-object (webkit.messageHandlers.adClicked), how could I to use webView.addJavascriptInterface() function to deal with it? I know how to deal with single object, but I don't know how to deal with multi-object?

I use following code, but it does not work:

enter code here
    webView.addJavascriptInterface(this, "webkit");
    webView.addJavascriptInterface(this, "messageHandlers");
    webView.addJavascriptInterface(this, "adClicked");
    webView.addJavascriptInterface(this, "webkit.messageHandlers.thepaperNewsClicked");

    @android.webkit.JavascriptInterface
    public void postMessage(final String str) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getContext(), "param:" + str, Toast.LENGTH_SHORT).show();
            }
    });

1

1 Answer 1

-1

From android API document :

void addJavascriptInterface (Object object, String name)

The parameter "name" is used to let javascript to call from android, so you can just deal with single object; you can create one class and put in functions which can been called for javascript, and give one name for javascript to call.

you can see what they do in this post: https://stackoverflow.com/a/24060790/8355528

Reference site: https://developer.android.com/reference/android/webkit/WebView.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.