Android Tutorial

(avery) #1

By : Ketan Bhimani


388 

You can define the JavaScriptExtensions class as a subclass within
the activity as a subclass with a single method that can trigger
Android Toast messages:

class JavaScriptExtensions {
public static final int TOAST_LONG = Toast.LENGTH_LONG;
public static final int TOAST_SHORT = Toast.LENGTH_SHORT;
public void toast(String message, int length) {
Toast.makeText(SimpleWebExtension.this, message, length).show();
}
}


The JavaScript code has access to everything in the
JavaScriptExtensions class, including the member variables as well
as the methods. Return values work as expected from the
methods, too. Now switch your attention to defining the web page
to load in the WebView control. For this example, simply create a
file called sample.html in the /assets directory of the application.

The contents of the sample.html file are shown here:






This is a test.




Free download pdf