16

The following is my test code. My problem is that in the page two I can not reference AndroidFunction2. I'm testing this on Nexus 7 with Android 4.4. But it's OK on sumsang i9100 with Android 4.0. Am I doing something wrong, or there is an Android's bug?

MainActivity

public class MainActivity extends Activity {
    WebView mWebView1;
    WebView mWebView2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final FrameLayout mainFrame = (FrameLayout) this.findViewById(R.id.mainFrame);

        mWebView1 = new WebView(this);
        mWebView1.getSettings().setJavaScriptEnabled(true);
        mWebView1.getSettings().setSupportMultipleWindows(true);
        mWebView1.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }
        });
        mWebView1.setWebChromeClient(new WebChromeClient() {
            @Override
            public boolean onCreateWindow(WebView view, boolean isDialog,
                    boolean isUserGesture, Message resultMsg) {
                mWebView2 = new WebView(MainActivity.this);
                mWebView2.getSettings().setJavaScriptEnabled(true);
                mWebView2.getSettings().setSupportMultipleWindows(true);
                mWebView2.setWebChromeClient(new WebChromeClient() {
                    @Override
                    public void onConsoleMessage(String message, int lineNumber, String sourceID) {
                        Log.d("WebView", "Line: " + lineNumber + ", " + message);
                    }
                });
                mWebView2.addJavascriptInterface(new Object() {
                    @JavascriptInterface
                    public void hello2() {
                    }
                }, "AndroidFunction2");

                (( WebViewTransport )resultMsg.obj).setWebView(mWebView2);
                resultMsg.sendToTarget();
                mainFrame.addView(mWebView2);
                return true;
            }
        });
        mWebView1.addJavascriptInterface(new Object() {
            @JavascriptInterface
            public void hello1() {
            }
        }, "AndroidFunction1");
        mWebView1.loadUrl("file:///sdcard/test_1.html");

        mainFrame.addView(mWebView1);
    }
}

And the two web page,

test_1.html:

<html>
<body>
    <a href="test_2.html" target="_blank">goto test 2</a>
    <div><a href="javascript:alert(typeof AndroidFunction1);"> alert(typeof AndroidFunction1);</a> </div>
    <div><a href="javascript:alert(typeof window.AndroidFunction1);"> alert(typeof window.AndroidFunction1);</a> </div>
</body>
</html>

test_2.html

<html>
<body>
    <div><a href="javascript:alert(AndroidFunction2);"> alert(AndroidFunction2);</a> </div>
    <div><a href="javascript:alert(typeof window.AndroidFunction2);"> alert(typeof window.AndroidFunction2);</a> </div>
</body>
</html>
3
  • 1
    I'm having the same issue, did you ever figure it out? Commented Feb 27, 2014 at 5:05
  • @mntgoat sorry, I haven't. This problem still exists in my project. Commented Mar 18, 2014 at 11:53
  • @zhang Did you find out anyway to handle the internal 'AndroidFunction2' interface .? Commented Dec 4, 2018 at 11:35

1 Answer 1

52

Even I had the same problem and after going through the docs again I found that if you've set your targetSdkVersion to 17 or higher, you must add the @JavascriptInterface annotation to any method that you want available to your JavaScript (the method must also be public).

If you do not provide the annotation, the method is not accessible by your web page when running on Android 4.2 or higher.

The following example (taken from http://developer.android.com/guide/webapps/webview.html) shows that you can include the following class in your Android application:

public class WebAppInterface {
    Context mContext;

    /** Instantiate the interface and set the context */
    WebAppInterface(Context c) {
        mContext = c;
    }

    @JavascriptInterface   // must be added for API 17 or higher
    public void showToast(String toast) {
        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
    }
}

and bind as

WebView webView = (WebView) findViewById(R.id.webview);
webView.addJavascriptInterface(new WebAppInterface(this), "Android");

and then call from the HTML inside the WebView as

<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />

<script type="text/javascript">
    function showAndroidToast(toast) {
        Android.showToast(toast);
    }
</script>
Sign up to request clarification or add additional context in comments.

6 Comments

In future, do not copy content from elsewhere without clear attribution. It is seen as plagiarism. See stackoverflow.com/help/referencing
@Matt I have mentioned in the first line "going through the docs" which means that my answer is derived from the official documentation. I am not against your point here but I have no intentions of copying contents either.
@Ramswaroop: I accept some effort was made to reference the documentation, but in situations where the data is copied (as it was for the examples), we really need clearer attribution.
This work only on Main WebView, not on webview created from callback method onCreateWindow. Tried adding @JavascriptInterface also.
This solution doesnt seem to work for webview created from onCreateWindow method. I wonder how is this an accepted answer!!!!
|

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.