2010年8月19日星期四

Palm webos java service 写法调用等分析

webos java serice及相关



我们大概说一下怎么做一个基于java的后台服务,并让他在dbus系统下运行。
webos的服务框架很大上依赖dbus,palm的大多数后台服务都是用java来写的。他们用一个dbus的java库来和dbus交流,并且写了一些库让后台服务生成得更容易些。这些库其中重要的是serviceframework.jar,他放在/usr/lib/luna/java

过程就是:写java--->生成jar--->配置service和dbus--->前端调用(后台服务也行,nogui)
把youservice.jar拷贝到/usr/lib/luna/java
把service的dbus script拷贝到/usr/share/dbus-1/system-services
重启pre,就可以调用你的palm://com.xxx.xxx了

写法:http://gitorious.org/webos-internals/applications/trees/master/hello-service
分析:dist/README

用json来传递函数的输入和输出,方便调用。例如:
runCmd的JSON输入的结构 (js调用):
{method:"runCmd",prog:"cmd",param:"params"}

runCmd的JSON的输出的结构:
{output:"Command Output"}

luna-send 命令:

/usr/bin/luna-send -n 1 palm://com.shinali.services.hello/runCmd {\"prog\":\"echo\",\"param\":\"HELLO WORLD\"}

分析:HelloService.java

public class HelloService extends LunaServiceThread/*lunaservic扩展*/
JSONObject reply = new JSONObject();
reply.put("output", output);
msg.respond(reply.toString());

就以上面的服务com.shinali.services.hello.service为例

service 安装路径:
/usr/lib/luna/java/HelloService.jar

D-BUS配置文件
/usr/share/dbus-1/system-services/com.shinali.services.hello.service

D-BUS配置文件内容
[D-BUS Service]
Name=com.shinali.services.hello
Exec=/usr/bin/luna-helper 'luna://com.palm.vm/launch' '{"serviceName":"com.shinali.services.hello","className":"com.shinali.services.HelloService"}'

不搞自启动貌似也是可以用,readme里面说了,迟点我会测试一下,现在已经可以调用palm://com.shinali.services.hello

上面说了那么多,基本上都是后台,如果要有gui,要前段调用,就得用 this.controller.serviceRequest

调用 Mojo.Service.Request  前端 javascript 的 callback 函数

当我的 java service 有消息 到来的 时候 用 java 去调用  linux 下的   luna-send 命令

String[] commands = new String[]{"luna-send", "-n", "1","palm://com.palm.applicationManager/launch" ,"{\"id\":\"com.palm.app.myappid\"}"};
Process child = Runtime.getRuntime().exec(commands);
Mojo.Service.Request()
You can use this.controller.serviceRequest() within scenes where you would make
most service requests. However, if you need to make a service request within your
application assistant, you’ll need to create a service request object.
A common case is a call to the Alarm service to wake up the application after an interval:
this.alarm = new Mojo.Service.Request("palm://com.palm.power/timeout", {
method: "set",
parameters: {
key: "com.palm.app.news.update",
in: feedUpdateInterval,
uri: "palm://com.palm.applicationManager/open",
params: {
id: "com.palm.app.news",
params: {action: "updateFeed"}
}
},
onSuccess: this.onSuccessHandler,
onFailure: this.onFailureHandler
});
In this example, the new request object is created and a service request issued, with the
object stored as this.alarm.
Using The request references are managed by the scene when creating a service request using
this.controller.serviceRequest(), and are removed upon completion of the request,
unless the request has subscribe: true, in which case the requests are cleaned up when
the scene is popped.
All requests made with this.controller.serviceRequest() are cleaned up when the
scene is popped, meaning they are garbage collected and destroyed. If the subscription
request needs to be retained beyond the lifetime of the scene, you will also need to use
Mojo.Service.Request() to save the request object and manage the request yourself.
Remember that service requests are asynchronous, so they don’t complete when you
make the call; if there’s a chance they will not be completed by the time the scene is
popped, use Mojo.Service.Request().

参考

http://developer.palm.com/index.php?option=com_content&view=article&id=1649

http://www.weboshelp.net/webos-mojo-development-resources/api-reference/420-mojoservicerequest

http://www.weboshelp.net/getting-started-with-webos/419-introduction-to-mojo-services

没有评论:

发表评论