Webview question: Is it possible to execute Javascript from Java synchronously?

by j » Tue, 07 Apr 2009 02:35:13 GMT


Sponsored Links
 I am writing a hybrid web/native app and ran into issue.  My app
downloads a music file via Java code, and needs to update a Javascript
download progress bar in the WebView in real time.

My understanding is the only way to call Javascript from Java is via
the WebView's loadUrl method.

while ((byteCount = in.read(byteBuffer, 0, BUFFER_SIZE)) != -1) {
        out.write(byteBuffer, 0, byteCount);

        cumByteCount += byteCount;
        int percentDowloaded = (cumByteCount * 100) / contentLength;
        Log.i(LOG_TAG, "percent: " + percentDowloaded);
        webview.loadUrl("javascript:updateDownloadProgress('" +
percentDowloaded + "')");
}
out.flush();

If so, then while calling Java from Javascript is synchronous, calling
Javascript from Java is always asynchronous since loadUrl is
asynchronous.

I notice that all of my webview.loadUrl calls get queued up and don't
get executed until the whole file has been downloaded.  Is there any
trick to make the webview execute my javascript immediately instead of
queuing everything up?
--~--~---------~--~----~------------~-------~--~----~



Webview question: Is it possible to execute Javascript from Java synchronously?

by Mark Murphy » Tue, 07 Apr 2009 02:58:17 GMT


 


Sure! Use a thread.

the UI thread. Instead, do the download in a background thread, then use
Handler or runOnUiThread() or something to get the loadUrl() calls to
run on the UI thread.

Now, I haven't tried this specifically with WebView and loadUrl(), but
you get the results you are seeing from any regular widgets, so I am
guessing it is the same problem.

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

Android App Developer Training:  http://commonsware.com/training.html 

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


Sponsored Links


Webview question: Is it possible to execute Javascript from Java synchronously?

by j » Tue, 07 Apr 2009 03:38:56 GMT


 Mark, your suggestion makes sense.  I tried it.  I created a separate
thread for downloading and uses a Handler to loadUrl instead of
calling loadUrl directly.

But the result is the same.  The loadUrl executions get queued up.  It
appears the downloading thread seems to have priority over the Handler/
UI thread.

Thanks.





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



Webview question: Is it possible to execute Javascript from Java synchronously?

by Mark Murphy » Tue, 07 Apr 2009 03:43:08 GMT


 


Hmmmm...you could try calling setPriority() on the background Thread to
try to ensure the UI thread gets more love.

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

Warescription: Three Android Books, Plus Updates, $35/Year

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



Webview question: Is it possible to execute Javascript from Java synchronously?

by j » Tue, 07 Apr 2009 04:22:04 GMT


 Mark,

I have a nastier solution:
Put inside the while loop:
                try {
                        Thread.sleep(100);
                } catch (Exception e) {}

It works.





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



Other Threads

1. Interested in knowing about Chanukka games development in progress

Hi, I am highly interested in knowing of the existence of new chanukka
games, if any of you is developing games in this direction, please
notify me.

I am willing to write a post in my blog about Chanukka games in mobile
devices, and for the moment I just found one and only title in
Android, a small crazy title called "Chanukka on Mars", perhaps this
is the first game inspired in this jewish holliday on the history of
Android. But, is there anything new in progress?

Is not a piece of art, but is ok. But I am interested in finding new
titles.
Is anyone in this forum developing something in that direction?

Please let me know to my personal email.

Thank you!

-- 
===============
"Indonesian Android Community [id-android]"

PING'S mobile
Email: sa...@pings-mobile.com  Ph. (021) 96087100
---------------------
Yopie Ratjoen
Email: yo...@ratjoen.com  Ph. 08977726681
--------------------
Gila original
Email: i...@gilaoriginal.com  Ph. (031) 91555898
--------------------

Aturan Jual/Kloteran ID-Android  http://goo.gl/azW7

2. Unexplained Errors from Android MediaPlayer

Hi All,

I am getting the below errors from the Media Player
Error(1, -1002) or Error(1, -2147483648)

Has anyone been able to resolve these errors? is this because of the media
format?

Any kind of help is highly appreciated.

Thanks,
Prajakta

-- 

3. Custom checkable ImageView

4. CyanogenMod-6.1.0-RC1-Spica-alpha7.3

5. Short survey for a friend

6. Pre order desire Z dan desire HD bnib

7. Referencing a library adds <libraryProj>_src reference