|
前段时间研究了下webos 开发, 随便请教了几位前辈 , 大致了解了webos的前后台调用机制(即:javascript 调用 java service) .有几位朋友询问过相关问题,我就不一一回复了, 就在这里献丑了...
: o6 e' w1 e2 B+ g一:java service 开发:$ |! Q$ P/ a$ u' m& }: s
; K4 a9 d! @4 }1 f; t7 o
jar :
; E+ y& H+ V' [4 ]$ O( F2 } serviceframework.jar , json.jar (在palm 模拟器里面有 抱歉 少了个 lunaservice.jar 这个是处理异常的)
% O+ }! U; J( Y( c- code : MyService.java
5 e' h) g! {: g L - ! a, A9 s* S- [5 s& S: a% t8 P9 F3 x
- package palm.greenwoods.demo- _; G0 }+ z2 c* H5 z' a
- import com.palm.luna.LSException;; O" U. z. b: @) N
- import com.palm.luna.service.LunaServiceThread;7 ?# _0 U8 h& t8 H
- import com.palm.luna.service.ServiceMessage;1 B" \2 S5 i; P% V1 T! \2 t, u) Z6 S
- import org.json.JSONException;
2 ]7 @( u$ @- [) ]5 X - import org.json.JSONObject;3 Z, O$ Q) \9 B6 x, [4 u8 f! q
- 7 g+ N: G% Z; F+ Z( C
- public class MyService extends LunaServiceThread {
" |% A8 S" T, ~' P* l, e* [ - public MyService () throws IOException {
- D; g8 C8 T5 E2 R - //code
& I, l9 l3 k! S - }
+ F; Q/ L6 w6 i0 j" N# N - : `0 t1 J {1 a% ]
- @LunaServiceThread.PublicMethod //新的service 接口
+ n( m- P2 s2 [- M9 I - public void status(ServiceMessage msg) throws JSONException, LSException,
: k; i, s3 W( ?! q - InterruptedException, IOException {
5 G8 L- d$ E) z+ P) N; s - JSONObject reply = new JSONObject();! H" J" P& U3 @* }
- reply.put("action","status");
7 K4 k( J) \. k) t" t - reply.put("status", status);) T4 ?/ Y$ {3 _
- msg.respond(reply.toString()) ;//ServiceMessage msg 这个是前端的 请求对象 respond 方法可以返回数据到前端 js, j5 r7 C) `( ]
- }5 P C: ~, X' e( C- R5 S8 ]* G+ a
- }
复制代码 Export => MyService.jar
7 {! S& M. _% B3 s9 d& e# `5 }! {* y% \; ~4 m/ @3 k2 N. @( q* j1 p
; d' x3 |. g9 K; n1 X& n二:java service 打包:8 K. z* n% l. [1 `( _: Q
5 V6 U1 p1 M2 t0 ~) O# J5 V目录结构- pack \9 E# m# {/ T4 i
- CONTROL\: V1 X4 P) y6 w" V
- #配置问题 palm.greenwoods.demo 为服务唯一ID j& Z5 U1 d, y
- control
W) C+ A d% \ - Package: palm.greenwoods.demo
7 }- j7 }' \: ^ - Priority: optional
* x# o2 ]) S6 y6 C8 e6 z/ e - Section: Network
' s9 O/ d5 X/ [% L6 Q- x - Version: 1.06 R) H! k+ A3 W, s+ `: E: H' d! i$ E' n
- Architecture: all
6 W! _4 ^: H. ?8 v+ g - Maintainer: GreenWoods [email]yulim.wang@gmail.com[/email]- g$ p1 Z( c/ D1 t# V
- Depends: ^% C/ E: P* @+ i
- Description: MyServer Service
% ^! V9 G1 u/ d0 }# d0 y- S+ O; g - Source: {"Source":"http://user.qzone.qq.com/86121652/","Homepage":"http://user.qzone.qq.com/86121652/","Category":"Services","License":"GNU GPLv2","Type":"Service","FullDescription":"MyService.","PostRemoveFlags":"RestartJava","PostUpdateFlags":"RestartJava","Title":"GeXinService","PostInstallFlags":"RestartJava"}
1 L" P9 G$ ^2 F# L - #服务安装执行脚本: w. J5 y( \9 o2 G
- postinst$ T5 `$ q. m7 ~* X+ @* `) y
- #!/bin/sh, ?/ c0 }5 X8 {
- start on started java-serviceboot
7 g1 ]/ Y) y& D6 K! I& ` - 3 W8 I9 O, G! y/ I: y9 x
- mount -o remount,ro /
3 }" i0 f" B# N" L$ p - 9 I. x& J! {+ }$ s: B
- APP_PATH=/media/cryptofs/apps/palm.greenwoods.demo
% X. ]7 r( V" ^' b. H. h! j
$ _! O) |& ]! I, p8 C" F- /bin/cp -f $APP_PATH/usr/lib/luna/java/MyService.jar /usr/lib/luna/java/MyService.jar6 d9 o9 d6 }& Q+ W+ t0 N
4 u. ^5 ^: s- W f! G- /bin/cp -f $APP_PATH/usr/share/dbus-1/system-services/palm.greenwoods.demo.service /usr/share/dbus-1/system-services/palm.greenwoods.demo.service
9 o8 K! r- T( s2 o8 q - N) U( O- k6 w/ `
- /bin/cp -f $APP_PATH/etc/event.d/palm.greenwoods.demo /etc/event.d/palm.greenwoods.demo8 |8 ^% u5 L* n6 r# `$ d" g
- ' l$ j# `* z: }+ B8 L
- if [ -z "$IPKG_OFFLINE_ROOT" ]; then T' Q, J& ~' ?
- killall -9 java
# e, Q8 w4 ~; M7 f - mount -o remount,ro /. {: |' [7 |7 n. o0 p
- fi8 U, C4 L% r1 \# J. G3 B% w7 _
- # k: E7 e7 `8 G& i8 b
- stop java-serviceboot
4 M7 W, k& V7 m' `) {1 e0 x - start java-serviceboot# w# a5 z$ T4 t
- start palm.greenwoods.demo
0 }. m$ E0 K$ D6 I2 S& r7 A4 w
- ]+ j+ B! A1 o. P6 `, K9 h1 h' g- exit 0
7 l" {# ~. ]. k# S+ p - preinst" M! W8 W, y$ h& j# p8 Z
- mount -o remount,rw /
}1 S6 R9 \( e* X& h5 u% [ - #卸载服务脚本
% \- L! v9 O% ~) I. L, m - prerm* ?6 j% q! b1 } G" n
- #!/bin/sh( d! w1 `! J& s0 c1 f- B- n8 d2 a
- mount -o remount,rw /* i! B8 h; k \
- I. G5 n8 P. o9 l5 S
- stop java-serviceboot% T9 d3 E: o% \$ x1 |7 _; a
- stop palm.greenwoods.demo3 v9 C- j3 W4 @3 G
/ Y0 W4 c3 V' q9 P3 b3 h+ D- /bin/rm -f /usr/lib/luna/java/MyService.jar
! j! \* N+ L: `3 X7 I - 7 w3 k" I! N/ w4 m8 W5 R2 f
- /bin/rm -f /usr/share/dbus-1/system-services/palm.greenwoods.demo.service) n6 B3 x: _; |& o& ]0 m7 D* B
- - D9 M5 w. ^+ W; U5 p
- /bin/rm -f /etc/event.d/palm.greenwoods.demo0 q' z& M$ B. P! K. @1 L
! `/ m; N" o3 X" g; E) h- start java-serviceboot
. I. v7 p* F3 U. g9 x; i, ? - etc\event.d\+ \2 I: d! K) A- p% R. Z/ Q1 U- _
- #启动服务器 文件名必须和 服务唯一ID 一致6 P" {7 A% Z/ f0 S$ E# z
- palm.greenwoods.demo, F0 a7 [7 }5 x3 a) ]8 `
- description "MyServer Service"# R% I% U+ j$ | M! J6 j6 f
- ) ^* a( A, s7 r6 S
- start on started java-serviceboot; z9 Q( d+ ~( u$ E) K
- / H: w# L7 u: A' V6 F/ h* R ]
- script
/ _7 w9 O! c5 y9 Z8 q - sleep 10& F7 @0 G. i/ E" ? t
- returnTrue=0: H1 u# i, z& U. `& M
- tries=0% d. Z6 I+ j$ s2 q
- while [ $returnTrue -eq 0 -a $tries -le 20 ]
7 j9 g( L3 }# F - do
/ T3 J9 M& Y* E" z. K3 h - returnVal=$(luna-send -n 1 luna://palm.greenwoods.demo/status '{}' 2>&1)
E% s& M$ R0 E3 ]; d - tries=`expr $tries + 1`6 N }0 }- }" [5 L* Q9 M9 W
- if [ "$returnVal" = "** Message: serviceResponse Handling: 2, {"returnValue":true}" ]" f5 P0 q o, M
- then/ y. i, x/ D1 k4 D
- returnTrue=1
% a9 c& H2 F) ^- ^% C - else2 v$ C" [6 n) `% d5 R+ {
- sleep 10
0 \. z; O3 q& X0 w1 M- N+ T1 t - fi' ?: q4 s% V( i) j0 h& ?% B
- done7 Y) h0 a6 i- u/ a& b" V
- end script
- D- F/ A4 k' x+ e$ w X4 ^ - usr\; j2 T' P; U6 z0 t& B% K P
- lib\luna\java\
# y: s* `% V& ]0 M2 B, @+ S - MyService.jar
9 y, w+ T& f+ s! F1 h- l/ [ - share\dbus-1\system-services\: k; _) x4 x& d7 X0 A$ Z" R
- palm.greenwoods.demo.service8 s" o* C* ]# Y# I
- [D-BUS Service]
7 F. r( c. _6 [ N, J: Y% P; P7 S - Name=palm.greenwoods.demo! p( P6 t: s! i+ M# Z
- Exec=/usr/bin/luna-helper 'luna://com.palm.vm/launch' '{"serviceName":"palm.greenwoods.demo","className":"palm.greenwoods.demo.MyService"}'
复制代码 所需打包工具 Ipk Packager.jar 选择 pack 所在目录 入图所示7 H6 m0 f" n/ l8 Z7 ^( m8 H
(不好意思 无法传图片 请看附件 )
+ f _$ v( V+ t7 F1 y& X三 : 前台调用- new Mojo.Service.Request("palm://palm.greenwoods.demo/", {/ | \; c! F2 L) _* t2 C H7 G
- method: "status",
4 }5 G {! ^" h' T - onSuccess: this.listenStatus.bind(this),
1 x6 r' v6 w8 [3 c5 t5 _* ^ - onFailure: this.serverFail.bind(this)% O* z+ o$ r0 u* T+ W4 o4 e
- });
复制代码 注:以上为个人见解 , 可能有不完善之处 ,还望高手指正 ! 献于 Plamer 谢谢. |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?加入我们
×
评分
-
查看全部评分
|