[android-developers] Re: Usage of WebView.addJavascriptInterface()
Anton Pirker wrote:
> 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.
Dump the "window." -- just reference FileUtil directly.
> Then I displayed all members of the window object and my "FileUtil" is
> missing.
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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home