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(R.id.appView);
    appView.getSettings().setJavaScriptEnabled(true);
    appView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    appView.addJavascriptInterface(fu, "FileUtil");
    appView.loadUrl(someUrl);
}

// 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 window.FileUtil.read() but nothing happens.
Then I displayed all members of the window object and my "FileUtil" is 
missing.

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


Thanks in advance!
Anton, Freelance Android Developer

[1] 
 http://developer.android.com/reference/android/webkit/WebView.html #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
"FileUtil.read()", and the Javascript interpreter will interpret it, but
there is no FileUtil object.

-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com  |  http://twitter.com/commonsguy 

Android App Developer Books:  http://commonsware.com/books.html 

--~--~---------~--~----~------------~-------~--~----~


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!
Anton


--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. Query About Android gadget

Hi ,

I am working on kernel 2.6.27. I am working on USB android gadget
driver. I am getting the same problem as i found in the following link

2. All apps are created equal; how to install/uninstall.

Since the judging app isn't part of the firmware/system group, was
just wondering, how come the ADC2 judging app manages to install and
uninstall .. oh well, probably through market but can other apps do
the same? How?

Or is it possible only by apps that are from the same UID as the
market app?
Thought apps installed externally can't get into system group or
install permissions.
--~--~---------~--~----~------------~-------~--~----~

3. How to delete file when file is created by another app

4. Java heap outofmemory during dex

5. Suggest a board

6. Search in Maps !

7. Something like onEmailReceived?