Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SDK WebView call Activity

I'm trying to launch an Activity when clicking a link inside a WebView component.

My Webview is loaded inside Main.java and I would like to launch SubActivity.java when clicking a link inside the Website which is in Main.java?

Also, how can I pass parameters to this activity?

Example: inspection://Project/1

"Inspection" is the name of my application, inspection is the Activity I would like to launch and 1 is the ID I would like to have.

like image 684
NicolasBL Avatar asked Jan 30 '11 23:01

NicolasBL


1 Answers

You could use WebView's addJavaScriptInterface to allow JavaScript to control your application (in this case, to allow JavaScript to fire an Intent when a link is clicked).

To do this you need to pass a class instance to bind to JavaScript, this could be something like the following:

private final class JsInterface {
      public void launchIntent(final String payload) {
         Activity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
               // use the payload if you want, attach as an extra, switch destination, etc.
               Activity.this.startActivity(new Intent(Activity.this, SomeOtherActivity.class));
            }
         });
      }
   }

Then you add that to the WebView with something along these lines:

webView.addJavascriptInterface(js, "Android");

Then in JavaScript from the WebView you just use your new "Android" object's "launchIntent" method.

like image 127
Charlie Collins Avatar answered Sep 28 '22 10:09

Charlie Collins