You could do both, if you abstract out the javascript-to-java calls in your
HTML5's javascript
- Create a javascript class in your HTML5 that implements a certain set of
methods that define
all the necessary javascript-to-java calls (abstraction). Let's call it
'CallToJava'.
- On Android, register a javascript-interface; let's call this javascript
object 'AndroidJS'.
- In your HTML5's javascript, test if AndroidJS is defined or not.
- If not, you're running on iOS.
Implement/instantiate a CallToJava instance that uses URL interception
for calling back into Java.
- If it is defined, you're running on Android.
Implement/instantiate a CallToJava instance that uses direct calls to
the 'AndroidJS' javascript-interface.
In the rest of your HTML5's javascript, use the instantiated CallToJava's
methods to call back into your native iOS or Android code.
As for in which threads javascript-to-java callbacks run:
My experience is that callbacks into the javascript-interface is done on a
WebViewCore thread, not the main UI thread. Use a Handler to post back to
the main UI-thread if that is necessary. If in doubt, try it in the
debugger and see which thread is being used.
About the Broadcaster's onReceive: To handle longer running operations,
look at Marks' *WakefullIntentService *(just google it) :-).
On Tuesday, August 28, 2012 12:45:13 PM UTC-4, Lieuwe wrote:
>
> Hi,
>
> I am working on a fairly complex bit of code. I would like your input to
> understand which (if any at all) of the above classes I should use.
>
> The short description is that I have an Activity that displays local HTML5
> within a WebView. The WebView utilises a WebViewClient which intercepts URL
> requests from the HTML5 implementation (I realise that on Android the
> HTML5/javascript implementation can directly call native code but
> unfortunately the HTML5 needs to be x-platform (i.e. run on iOS) so I am
> forced to communicate through intercepting URL's that are being loaded).
> The application also has a separate Thread that I use to communicate with
> an external server across the network.
>
> 1: When the native Android code wants to do a javascript call in the
> WebView object, does this need to be done via the main application thread
> or could for instance the external server Thread do a
> WebView.loadURL("javascript:......") directly?
> 2: Is the HTML5/javascript implementation run by the main thread? In other
> words, is the WebViewClient.shouldOverrideUrlLoading(....) called by the
> main application thread?
> 3: I implemented PUSH notifications within the application and I am
> currently using a BroadCastReceiver in my Activity to receive broadcast
> messages from the cloud messaging IntentService I added. As the Activity
> already contains a Broadcastreceiver - should the external server Thread
> (in case the answer to 1 is yes) and the WebViewClient (in case the answer
> to 2 is no) also send a broadcast message to report to the Activity or
> should I implement an additional message Handler?
>
> - on a sidenote .. I just read that I cannot perform timeconsuming actions
> from the BroadcastReceiver.onReceive(....) method. (apparently not even
> open pop-up dialogs although that is exactly what I do and works fine on
> both the simulator and a range of real devices) ... what is the solution
> here?
>
> Hope I explained the situation clear enough :)
>
> Cheers,
>
> Lieuwe
>
>
>
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en