Exist stringByEvaluatingJavaScriptFromString for Android
Asked Answered
F

2

3

Exist stringByEvaluatingJavaScriptFromString method in Android like Iphone?. Not with a simple code like this javascript:wave(). But with a complex Java Script function. Thanks

Fermium answered 23/6, 2011 at 15:52 Comment(1)
#10305180Slav
C
0

You can use loadUrl instead,eg:

webView.loadUrl("javascript:PerformSimpleCalculation()");

It's effect similar to stringByEvaluatingJavaScriptFromString.The API description is not clear.

Casals answered 6/1, 2012 at 9:55 Comment(0)
B
0

stringByEvaluatingJavaScriptFromString is a private method in Android API. But it is really useful.

You could retrive this API via Java reflection:

Method stringByEvaluatingJavaScriptFromString , sendMessageMethod;;
    Object webViewCore , browserFrame;
private boolean hasIntercepted = true;
    Object webViewObject = this;
    Class webViewClass = WebView.class;
    try {
        Field mp = webViewClass.getDeclaredField("mProvider");
        mp.setAccessible(true);
        webViewObject = mp.get(this);
        webViewClass = webViewObject.getClass();
        Field wc = webViewClass.getDeclaredField("mWebViewCore");
        wc.setAccessible(true);
        webViewCore = wc.get(webViewObject);
        if (webViewCore != null) {
            sendMessageMethod = webViewCore.getClass().getDeclaredMethod("sendMessage", Message.class);
            sendMessageMethod.setAccessible(true);

            Field bf= webViewCore.getClass().getDeclaredField("mBrowserFrame");
            bf.setAccessible(true);
            browserFrame = bf.get(webViewCore);
            stringByEvaluatingJavaScriptFromString = browserFrame.getClass().getDeclaredMethod("stringByEvaluatingJavaScriptFromString", String.class);
            stringByEvaluatingJavaScriptFromString.setAccessible(true);   

        }
        hasIntercepted = true;
    } catch (Throwable e) {
        hasIntercepted = false;
    }

Then to use this method:

Object argument =(Object) ((String)"window.hitTest("+event.getX()+ ","+event.getY()+")");
    try {
        Object _tagName = stringByEvaluatingJavaScriptFromString.invoke(browserFrame, argument);
        String tagName =(String)_tagName;
    } catch (Exception e) {
        e.printStackTrace();
    }
Boathouse answered 24/7, 2013 at 9:43 Comment(1)
This way will cause exception at this line: Field wc = webViewClass.getDeclaredField("mWebViewCore");Aldrin

© 2022 - 2024 — McMap. All rights reserved.