Skip to content Skip to sidebar Skip to footer

Android Webview: Execution Of Javascript From Java Method Called From Javascript

I have the following javascript code: function mine() { var i = 3; AndroidObject.call(); } where AndroidObject is javascript interface to java. It has

Solution 1:

Suppose i is an integer,

function mine()

 {

var i = 3;

AndroidObject.call(i);

}

and

WebView myWebView;

publicvoidcall(Integer i)
{
Integer temp = i;
    runOnUiThread(newRunnable()
    {
        @Overridepublicvoidrun()
        {
            myWebView.loadUrl('javascript:alert(' + temp + ');');
        }
    });
}

Solution 2:

I suggest you to create a function to get value of i if your i variable is global, like this:

var i = 20;

function getValueOfI() {
     return i;
}

and in your java code use something like this:

 myWebView.loadUrl('javascript:alert(getValueOfI());');

Updated If you want to achieve the same with local variables:

function mine()
    {
        var i = 3;
        AndroidObject.call(i);
    }

where AndroidObject is javascript interface to java. It has method call

WebView myWebView;

publicvoidcall(final Integer i)
{
    runOnUiThread(newRunnable()
    {
        @Overridepublicvoidrun()
        {
            myWebView.loadUrl("javascript:alert(" + i + ");");
        }
    });
}

publicvoidcall(final String i)
{
    runOnUiThread(newRunnable()
    {
        @Overridepublicvoidrun()
        {
            myWebView.loadUrl("javascript:alert(" + i + ");");
        }
    });
}

publicvoidcall(final Boolean i)
{
    runOnUiThread(newRunnable()
    {
        @Overridepublicvoidrun()
        {
            myWebView.loadUrl("javascript:alert(" + i + ");");
        }
    });
}

Post a Comment for "Android Webview: Execution Of Javascript From Java Method Called From Javascript"