0

Hi Im trying to get a certain variable value from a local HTML page. The function simply returns the value of a simple variable called x ( x is set to 5 ) so I want to call this function in android. Here is my code so far:

  @Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mWebView = (WebView) findViewById(R.id.webview);

    WebSettings webSettings = mWebView.getSettings();
    webSettings.setSaveFormData( false );
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.loadUrl( "file:///android_asset/tester.html" );

    getScript = (Button) findViewById( R.id.test );
    getScript.setOnClickListener( new View.OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {
            mWebView.loadUrl( "javascript:getX()" );
        }
    });
    Toast.makeText( this, "Hello World", duration );
}

So the idea is that when the getScript button is called it calls the function on the HTML page but how would i get that value displayed? Like in the toast message below it?

Cheers

1 Answer 1

1

Option #1: Add a WebChromeClient to your WebView, implement onJSAlert() in the WebChromeClient, and have your JavaScript code use alert()

Option #2: Add a custom JavaScript->Java bridge object to WebKit via addJavaScriptInterface(), and have your JavaScript code call something on that object

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks mate I'll have to do some research on it all - just seems to be bait complicated at this point
Hello all - Thought I'd leave a great link here that clarified my javascript/webview issues - code.google.com/p/openmobster/wiki/JavascriptBridge The only difference I did was to not load my stuff on the onResume() method. Cheers again CommonsWare

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.