views:

149

answers:

3

Hi every one, I want to get a return value from javascript in android. With Iphone, i can do it, but Android, i can't. I use loadUrl but return void not object. Anybody help me ? Thanks

+1  A: 

Use addJavascriptInterface() to add a Java object to the Javascript environment. Have your Javascript call a method on that Java object to supply its "return value".

CommonsWare
A: 

Thanks for answer.

Tonny C
+1  A: 

Here's a hack on how you can accomplish it:

Add this Client to your WebView:

final class MyWebChromeClient extends WebChromeClient {
        @Override
        public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
            Log.d("LogTag", message);
            result.confirm();
            return true;
        }
    }

Now in your javascript call do:

webView.loadUrl("javascript:alert(functionThatReturnsSomething)");

Now in the onJsAlert call "message" will contain the returned value.

Felix Khazin