Vignesh
Vignesh

Reputation: 731

Sharing an object(data) between java and javascript in android WebView

I have created a WebView activity like the following. I'm not able to make it working.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webview);

    JAsocket socket1=new JAsocket();

    webView = (WebView) findViewById(R.id.webView1);
    webView.getSettings().setJavaScriptEnabled(true);

        webView.addJavascriptInterface(socket1, "socket");

    webView.loadUrl("file:///android_asset/index.html");
     webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setUseWideViewPort(true);

}

My question is if I manipulate some member variables of "socket1" by calling its function in javascript via "socket",can I see those changes in this activity using socket1 as I pass the same object to the javascript.

Thanks.

Upvotes: 2

Views: 3361

Answers (1)

Kumar Bibek
Kumar Bibek

Reputation: 9117

The answer to your question is yes, sure. Have a look at a few examples.

http://android-developers.blogspot.in/2008/09/using-webviews.html

http://techdroid.kbeanie.com/2010/10/android-webview-javascript-and-css.html

You are half way through by the way.

Upvotes: 1

Related Questions