-1

My application has a webview calling an url and basically all the stuff will be done by the website, but after the user finishes the proccess, it will send an information for the app, like success or invalid. I want to know how to catch this return value through the webview. Is there a way to do this?

thanks.

Luiz Alegria
  • 197
  • 3
  • 18

1 Answers1

0

You should use a JavascriptInterface to expose one of your Java methods to Javascript on your website.

First, create a class to act as your interface. Any methods you want to expose to Javascript need the @JavascriptInterface annotation:

class MyJavaScriptInterface{
    @JavascriptInterface
    public void myMethod(boolean success){
        //Do something with value
    }
}

Then initialize the interface on your WebView:

mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.addJavascriptInterface(new MyJavaScriptInterface(), "JSInterface");

Finally, create the Javascript function to call the Java method from your website:

<script>
  //Communicate with Javascript Interface
  var processComplete = function(success){
      //Only call if the interface exists
      if(window.JSInterface){
          window.JSInterface.myMethod(success);
      }
  };
</script>
JstnPwll
  • 8,585
  • 2
  • 33
  • 56