Android SL4A:如何链接到 HTML/javascript 应用程序中的相关脚本?

发布于 2024-12-15 21:31:45 字数 2330 浏览 2 评论 0原文

好的,我尝试了三种方法来将外部 javascript 和 CSS 文件添加到我的 SL4A 应用程序中。我在 3UK 的 ZTE Racer 上使用 2.3 Gingerbread(带有 google apps 的 cyanogenmod),并使用 SL4A r4 和 android python 2.6.2(如果适用)。

首先,我假设相对链接可以工作:

<!-- GUI.html - loaded directly within SL4A UI. -->
<script type="text/javascript" src="jquery.js"></script>

事实并非如此 - 相对脚本和样式未加载。

接下来,我尝试使用事件从 python 加载脚本传递绝对路径:

# 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)

以及 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));

但是,这失败了由于该事件没有“结果”属性,只有“错误”属性(一些 Java 异常:

org.json.JSONException: Value globalAppPath at 0 of type java.lang.String无法转换为 int

python 中的路径字符串不可转换为一个未装箱的 int?!)。

最后,我尝试根据这个问题document.URL > 像这样:

  // (inlined jquery to be able to append to a debug div easily.)
  $('#loading').append("<br/>" + document.URL);

但是,这只是返回路径:

file:///mnt/sdcard/sl4a/scripts/

而实际上正确的路径应该是

file:///mnt/sdcard/sl4a/scripts/run-app/GUI.html

可能正是这个不正确的路径首先导致了失败 - 如果相对脚本正在查找父目录会解释事情。然而,当以 APK 形式分发到不同制造商的 android 实现时,这显然是不可靠的 - 我不能依赖任何关于 SL4A 初始化 Web 视图 URL 路径的方式的假设 - 我更喜欢使用更动态和正确的 python sys.path[0],如果我能以某种方式将结果传达给 webview 就好了!

我会认为这些失败中的每一个本身都是一个错误,但是这三个失败加在一起真是令人沮丧!我更愿意使用这个框架,而不是回到 Titanium,因为当事情变得艰难并且你不希望你的 javascript UI 冻结时,它给了我进入 python 的能力,但是如果我无法构建我的代码放入不同的文件中,我将很难利用 SL4A 手机编辑功能以及每个代码文件中的内联 jquery...

有没有一种方法可以真正有效地做到这一点?有没有人使用 SL4A 在 Android 上启动并运行 HTML5 / javascript 应用程序?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心凉怎暖 2024-12-22 21:31:45

这个问题的答案很简单:RTFM -

droid.eventWait()

采用单个可选整数参数 - 等待任何事件的秒数。

droid.eventWaitFor()

另一方面,允许您使用(强制)第一个参数中的字符串来过滤事件类型。

所有详细信息都位于此处

我的错。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文