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. Some problems i have encountered and would like fixes for:

I just got the G1 about 3 days ago, and i must say I'm extremely
impressed but i have encounter some problems that I believe would
seriously improve Android .

My downloads in market
-I have downloaded lots of apps to try out and i find my download list
in the market is over populated with old apps i tried and never used.
An option to delete old downloads from ur list should be added.

Lag When Quitting Browser
-When I click the home key from the browser, im brought to the home
screen but icons lags and dont load for an avg of 10-20 seconds.
- From my memory I have only really encountered this with the web
browser

Text Times Wrong
-Im using this phone in Canada on Rogers Network and my text (sms)
messages do not thread properly. The times are totally out of whack.
-I believe we need to add an option for messages to either use time
stamp data or use the "time" the phone actually received the message.

Quitting an app (task manager)
-a task managed should be implemented
-Some apps i might not use for a while, and over that time I will open
6 other apps but i still want that specific app to continue to run.
-A Prime example is "NetCounter" i might not actively open the app but
i need it to run 24/7 so i can track my data usage.
-Not all apps have a QUIT option.

If there are fixes for the problems i have posted, it would be great
to receive as much information as possible but in general i hope
android develops fixes for the following issues. .

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

2. Possible bug in TableLayout

I think that there is possible bug in TableLayout SDK 1.0 rc2 when
constructing table from code. It seams that the weight  or height
attribute is not respected when rendering the layout.

The XML view creates a table with row at the top and row at the bottom
of the screen  which is what supposed to happen, so I created this
same table via code but the results are not what were expected. Maybe
I am missing something but it seams OK, all LayoutParams  are this
same.


Sample Code
[code]
private View buildTableViewFromSource(){
                TableRow.LayoutParams pTable=new TableRow.LayoutParams
(TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.FILL_PARENT);

                TableLayout table = new TableLayout(context);
                table.setBackgroundColor(Color.RED);
                table.setLayoutParams(pTable);


                TableRow rowTop = new TableRow(context);

                TableRow.LayoutParams pRowTop=new TableRow.LayoutParams
(TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.FILL_PARENT);
                pRowTop.height=TableRow.LayoutParams.FILL_PARENT;
                pRowTop.width=TableRow.LayoutParams.FILL_PARENT;
                pRowTop.weight=1;

                rowTop.setBackgroundColor(Color.BLUE);

                TextView txt=new TextView(context);
                txt.setText("Top Content");

                rowTop.addView(txt, pRowTop);

                TableRow rowBottom=new TableRow(context);
                rowBottom.setBackgroundColor(Color.GREEN);

                TextView txtBottom=new TextView(context);
                txtBottom.setText("Bottom Content");

                TableRow.LayoutParams pRowBottom=new TableRow.LayoutParams
(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT);
                pRowBottom.height=TableRow.LayoutParams.WRAP_CONTENT;
                pRowBottom.width=TableRow.LayoutParams.FILL_PARENT;
                pRowBottom.weight=0;

                rowBottom.addView(txtBottom);

                table.addView(rowTop, pRowTop);
                table.addView(rowBottom, pRowBottom);

                return table;
        }


        private View buildTableViewFromXML(){
                LayoutInflater inflater=(LayoutInflater) getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
                LinearLayout layout=(LinearLayout) 
inflater.inflate(R.layout.table,
null);
                TableLayout table=(TableLayout) layout.findViewById
(R.id.tableBottomLayout);
                layout.removeView(table);

                TableRow rowContent=(TableRow)table.findViewById(R.id.rowTop);

                TextView txt=new TextView(context);
                txt.setText("Top Content");
                rowContent.addView(txt);

                TableRow rowTabs=(TableRow)table.findViewById(R.id.rowBottom);


                TextView txtBottom=new TextView(context);
                txtBottom.setText("Bottom Content");
                rowTabs.addView(txtBottom);

                return table;
        }
[code]

[code]
<?xml version="1.0" encoding="utf-8"?>

<TableLayout xmlns:android="http://schemas.android.com/apk/res/
android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/tableBottomLayout">

        <TableRow android:id="@+id/rowTop"
                android:layout_width="wrap_content" android:layout_weight="1"
                android:layout_height="fill_parent">

        </TableRow>

        <TableRow android:id="@+id/rowBottom"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:layout_weight="0">
                <!--
                        <TextView android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="HEllo"></TextView>
                -->
        </TableRow>

</TableLayout>
[code]

Any suggestions , if anyone else can confirm this I will post this as
a bug.

Thanks
Greg
--~--~---------~--~----~------------~-------~--~----~

3. missing partner_setup in envsetup.sh

4. Can MediaPlayer play .wav fomat audio?

5. viewable area

6. Android Emulator, Working altogether with ARM and DSP ?

7. 'cc1plus': execvp: No such file or directory