OK, so I have tried three methods to be able to add external javascript and CSS files to my SL4A app. I am on 2.3 Gingerbread (cyanogenmod with google apps) on a ZTE Racer from 3UK, and using SL4A r4, with android python 2.6.2 (where appropriate).
Firstly, I assumed that relative links would work:
<!-- GUI.html - loaded directly within SL4A UI. -->
<script type="text/javascript" src="jquery.js"></script>
This is not the case - relative scripts and styles are not loaded.
Next, I tried to pass the absolute path in from a python loading script using an event:
# script.py - launched in order to load window.
import android, sys, time
droid = android.Android()
path = "file://" + sys.path[0]
droid.webViewShow(path + "/GUI.html")
droid.eventPost('globalAppPath', path + "/")
# sleep for a bit to allow webview to load the DOM and read the event.
time.sleep(10)
and the following code in an inline script at the end of the body tag in GUI.html
:
// (inlined jquery to be able to append to a debug div easily.)
var droid = new Android();
var event = droid.eventWait('globalAppPath');
$('#loading').append("<br/>" + JSON.stringify(event));
however, this fails since the event has no 'result' property, only an 'error' property (some Java exception:
org.json.JSONException: Value globalAppPath at 0 of type java.lang.String cannot be converted to int
the path string from python not being castable to an unboxed int
?!).
Finally, I tried using document.URL
as per this question like so:
// (inlined jquery to be able to append to a debug div easily.)
$('#loading').append("<br/>" + document.URL);
However, this just returns the path:
file:///mnt/sdcard/sl4a/scripts/
when in fact the correct path should have been
file:///mnt/sdcard/sl4a/scripts/run-app/GUI.html
It is possible that it is this incorrect path which causes the failures in the first place - if the relative scripts are looking in the parent directory that would explain things. However, this is clearly unreliable when distributed in APK form to different manufacturers' android implementations - I can't rely on any assumptions about the way SL4A initialises paths for web view URLs - I'd much prefer to use the more dynamic and correct python sys.path[0]
, if only I could communicate the result to the webview somehow!
I would consider each of these failures on its own a bug, but all three together is just depressing! I'd much rather use this framework than go back to Titanium, since it gives me the power of dropping down into python when the going gets tough and you don't want your javascript UI to freeze up, but if I can't structure my code out into different files I'm going to have a tough time taking advantage of the SL4A edit-on-the-phone capability with inline jquery in every single codefile...
Is there a way of doing this that actually works? Has anyone got an HTML5 / javascript app up and running on Android with SL4A?
The answer to this question is simply RTFM -
droid.eventWait()
takes a single optional integer parameter - the number of seconds to wait for any event.
droid.eventWaitFor()
on the other hand, lets you filter on the type of event with a string in the (compulsory) first argument.
All the details here.
my bad.