Usage of WebView.addJavascriptInterface()

by Anton Pirker » Mon, 12 Oct 2009 21:53:08 GMT

Sponsored Links
 Hey List!

I have an Application that displays a Website in an WebView.  This is 
working quite good.
Now i want give the Website access to a Class of my Android Project with
WebView.addJavascriptInterface() [1]

This is my Java Code (in an Activity)

public void onCreate(Bundle savedInstanceState) {
    FileUtil fu = new FileUtil();

    WebView appView = (WebView) findViewById(;
    appView.addJavascriptInterface(fu, "FileUtil");

// The FileUtils class is a very basic test-class:
public class FileUtil {
    public FileUtils() { }

    public int read() {
       return 99;

And now i have a index.html accessable via the url <someUrl>
In this html page i try to call but nothing happens.
Then I displayed all members of the window object and my "FileUtil" is 

Have i forgot anything? How can i make my Java methods accessable from 

Thanks in advance!
Anton, Freelance Android Developer

[1] #addJavascriptInterface(java.lang.Object,%20java.lang.String)


Usage of WebView.addJavascriptInterface()

by Mark Murphy » Mon, 12 Oct 2009 22:10:08 GMT


Dump the "window." -- just reference FileUtil directly.

It's not supposed to be there. You do not get an actual Javascript
object from addJavascriptInterface().

It is a bit more like you get a new Javascript keyword -- you can write
"", and the Javascript interpreter will interpret it, but
there is no FileUtil object.

Mark Murphy (a Commons Guy)  | 

Android App Developer Books: 


Sponsored Links

Usage of WebView.addJavascriptInterface()

by Anton Pirker » Mon, 12 Oct 2009 23:11:12 GMT

 Hey Mark!

Ah, i see! That's why it is called addJavascriptINTERFACE() and not 
addJavascriptObject ;)

I am trying to create a file on the sd card from the website and i was 
testing on my phone that was connected to the notebook with an usb cable 
and the usb sharing was activated. and it seems if all this is true i 
cannot write to the sd-card! took my quite a while to find out about that.

Thanks for your hints!


Other Threads

1. How to control a sound notification duration?


Could not find a thread of discussion nor information on the web on
how to control the duration of a sound playing for a notification. The
"Notification" class does not seem to offer that feature. Am I missing
something, please?

Thank you.

2. The code in android which draw a rectangle surrounding an anchor in Webkit

Correct me if I am wrong, but if you are working in webview do you not
mean the -webkit css commands for the box and an example see
my blue box in video of Xspot:

That is css and -webkit css commands not C++ or Java..


3. DisplayMetrics screen DPI seems to be wrong on G1?

4. GTalk IM on G1 seems to use Smack.

5. Does Android need a more compelling/smoother user interface? (consumer view)

6. charging for upgrades to free apps?

7. Manifest Editor - Application tab shows empty page