|
前段时间研究了下webos 开发, 随便请教了几位前辈 , 大致了解了webos的前后台调用机制(即:javascript 调用 java service) .有几位朋友询问过相关问题,我就不一一回复了, 就在这里献丑了..." J3 l7 h9 l: z! P
一:java service 开发:, v+ u+ W/ j" f$ z0 T
) u; b+ ?8 p, L6 qjar :
4 z4 p5 W& H+ g, I M' `3 G serviceframework.jar , json.jar (在palm 模拟器里面有 抱歉 少了个 lunaservice.jar 这个是处理异常的)
/ b6 |+ r+ L; q! x$ q- code : MyService.java
4 z( ^8 N8 H; [+ E7 _" ?
/ `0 q, |0 p' J0 i2 A) U- package palm.greenwoods.demo
, O$ l: r" H3 h0 R - import com.palm.luna.LSException;* e6 K" M# j7 [: @& i' f3 G6 w
- import com.palm.luna.service.LunaServiceThread;
6 k! c4 O; A. y X4 S7 | - import com.palm.luna.service.ServiceMessage;7 V) M6 z1 @: S9 p! q& b7 ^2 T
- import org.json.JSONException;
3 [$ u5 U2 v9 v5 V - import org.json.JSONObject;6 h4 X' u' P, a+ r# F7 K; q
- " ]6 `8 t0 o+ o" p W
- public class MyService extends LunaServiceThread {0 X+ Z# D+ R2 q: U4 D2 t
- public MyService () throws IOException {
1 w: K y+ ^3 k! ?8 c, q) \+ Q* W - //code
' ~ _6 [3 Z6 D, t$ _) S1 ^' F% O - }
: c- F3 Y; w& `: ?; g9 o6 `4 L: J2 m - 0 R: e. o9 ~; C0 U' ^. m
- @LunaServiceThread.PublicMethod //新的service 接口1 g4 \/ w p4 g
- public void status(ServiceMessage msg) throws JSONException, LSException,+ |% O6 Z& k! r: l
- InterruptedException, IOException {, |4 [5 [: \8 m- |5 g8 W$ F
- JSONObject reply = new JSONObject();2 F! g0 m% ^7 [3 V% }( c" b5 M* `
- reply.put("action","status");9 G# q& n/ T* U( Y; O
- reply.put("status", status);
0 K# M# R2 u! b4 N - msg.respond(reply.toString()) ;//ServiceMessage msg 这个是前端的 请求对象 respond 方法可以返回数据到前端 js6 `* |, ^% @8 m! }& m' Y4 p9 o3 z
- }
4 r& O, Y1 V5 l+ Q, r/ p E - }
复制代码 Export => MyService.jar
0 h, E4 l/ J/ Q$ G; o' [# r) d+ ^. x+ g5 e% Q
+ Y7 O7 j8 h; d+ s5 m2 ` F
二:java service 打包:
2 c: H' [5 `+ x# @: I6 j J
+ C! @1 c% F1 S$ q$ h+ ?3 V) Q' {+ l目录结构- pack \" P4 _7 U0 p" G7 U q8 j
- CONTROL\
2 n1 n2 | M, @' X - #配置问题 palm.greenwoods.demo 为服务唯一ID
6 j2 M; b7 C8 O+ v6 w - control
. x. M/ H& j( }4 ?! q. } - Package: palm.greenwoods.demo
7 {4 `3 p. ~3 [* B5 D - Priority: optional
) J6 ~3 `/ q. ^. l, F" { - Section: Network9 x' `) x4 Z/ U, R+ @8 E
- Version: 1.0% y1 E2 j7 r' T7 P9 F2 v. z2 E
- Architecture: all# _) O/ p6 d( c) n! g |( E/ z" X
- Maintainer: GreenWoods [email]yulim.wang@gmail.com[/email]1 @1 m1 X3 O! } G# K" I# t( ?
- Depends:
; m. M7 d+ \5 j m - Description: MyServer Service
7 a2 w2 z, ^3 T& V1 F7 K$ u8 W - 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"}; F3 O$ N1 r7 A8 P5 Q
- #服务安装执行脚本5 Y; j; R' `8 k
- postinst; x, t9 j1 ~4 n- X/ y
- #!/bin/sh) i" y: i8 n3 e) }# Y0 \$ \
- start on started java-serviceboot
% {/ M" {: l$ |7 W5 D: {) j
" t _/ p8 R+ L# F# d7 t7 ~; N- mount -o remount,ro /% M0 X3 a8 j2 {- F; k8 ]7 m! y& k
$ m0 a! k2 O9 g. G* _4 G- APP_PATH=/media/cryptofs/apps/palm.greenwoods.demo/ t# x8 A! Y7 q1 E* n P) Y' W/ u
% N5 B3 D$ H8 {; V- /bin/cp -f $APP_PATH/usr/lib/luna/java/MyService.jar /usr/lib/luna/java/MyService.jar7 p4 o Z5 [5 R
- : _- }# |; X7 j2 h/ c7 o% \8 @+ h
- /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" m, g- ]: P5 G( y- M
- & [! R) r$ _, K2 W- V
- /bin/cp -f $APP_PATH/etc/event.d/palm.greenwoods.demo /etc/event.d/palm.greenwoods.demo: |) l: l' r7 ^0 h
5 I0 X2 F3 n. U% n! c8 v- if [ -z "$IPKG_OFFLINE_ROOT" ]; then, j7 W3 \8 f) E/ a
- killall -9 java/ n' @) \$ M! p5 j+ o4 X
- mount -o remount,ro /
0 `% e8 V+ @* F8 B) @! ` - fi
% z0 h6 s3 Y4 K/ k - 1 p* |4 ~" l$ i& M7 [. T; v
- stop java-serviceboot
; y$ m- \. O; p. _0 N$ w - start java-serviceboot' x7 G* n4 d: h& @3 m
- start palm.greenwoods.demo; Y; @( M+ a# u8 T5 q+ n
1 s* {( p: s+ n2 ]- exit 0( ^4 m% {: x5 P
- preinst
- I" k; F4 u! p1 ]; J" t: } - mount -o remount,rw /) ]* @7 H1 j+ s6 n0 t1 ?, j$ U
- #卸载服务脚本+ H" v0 i& B7 |, I# l2 M4 t
- prerm
4 C) K* S6 M9 h9 C4 A! M - #!/bin/sh2 A4 `. ]" g9 J1 X
- mount -o remount,rw /
8 U1 s5 w9 c: Y# W - ; t) l$ i$ d; X+ J) R# K: \5 p7 `
- stop java-serviceboot5 I6 L2 M% M. y( v! U' R8 F
- stop palm.greenwoods.demo- L" L, v$ Q+ x# w
- ( \ V# J- @9 `4 v+ J6 B6 ?
- /bin/rm -f /usr/lib/luna/java/MyService.jar
0 Z& v( `3 Y) D+ {% t0 u! _ - + N$ Y) k) \+ T* E
- /bin/rm -f /usr/share/dbus-1/system-services/palm.greenwoods.demo.service
0 m) R7 O5 O, B% i. B& a0 ^1 O
% W: b" R3 k9 q, [7 C& T- /bin/rm -f /etc/event.d/palm.greenwoods.demo4 Y/ L) ~9 G. [7 G, Q
- 1 {, x, I9 {- ~( X+ C9 G" s
- start java-serviceboot, {6 T' {* U8 F j: ^
- etc\event.d\
8 Y+ P5 `$ [& y2 o/ N - #启动服务器 文件名必须和 服务唯一ID 一致
. U4 X4 P6 D" { - palm.greenwoods.demo6 O0 S# l0 W- o7 h
- description "MyServer Service"
9 z* l: V9 u. a* v - 6 g |1 d$ ?. }0 { R- n: a5 V0 o! r1 q
- start on started java-serviceboot- ^: n0 y/ r2 C
- * x+ A& v# W2 |; {
- script
b) s1 h/ k! E: a - sleep 10
) d* }8 Z' e& d R$ N - returnTrue=0 z) x4 {$ k' Y* X/ }, P
- tries=0
0 r- q/ \: x/ p1 ?# ^3 R - while [ $returnTrue -eq 0 -a $tries -le 20 ]
$ q! v" i: R- h - do
# {, Y% w9 e" l8 f - returnVal=$(luna-send -n 1 luna://palm.greenwoods.demo/status '{}' 2>&1)
6 V3 y9 I8 ^( _8 c - tries=`expr $tries + 1`
6 S9 [: O1 t& L! z$ T' s2 x - if [ "$returnVal" = "** Message: serviceResponse Handling: 2, {"returnValue":true}" ]
# t0 n0 _7 w; J: p! w. a- X - then# S# t. H9 a, M8 Y1 N! v3 v
- returnTrue=1
% Q! W2 h H4 w - else
1 e0 @ A6 i' O! }, i - sleep 10
: s# D! |1 x; Y+ r$ i1 `: \* \ - fi
, I" f, p( J. F D5 V - done- v+ {- S/ R1 p- S) s
- end script/ O' s+ K' V- j9 w) M7 R* N
- usr\
2 q9 l2 Q. P5 i5 u - lib\luna\java\
: L" W; O7 ^% V9 W7 P' M! b0 g ? - MyService.jar
3 W& X! k" I' ^; y - share\dbus-1\system-services\
, ~8 @$ {( N* O3 Q - palm.greenwoods.demo.service' X7 | P8 W& l3 m* n
- [D-BUS Service]
/ h9 `" g' e8 } - Name=palm.greenwoods.demo# h: l, k g5 o* O" }& b9 i
- Exec=/usr/bin/luna-helper 'luna://com.palm.vm/launch' '{"serviceName":"palm.greenwoods.demo","className":"palm.greenwoods.demo.MyService"}'
复制代码 所需打包工具 Ipk Packager.jar 选择 pack 所在目录 入图所示0 A4 Z, b" w. W6 F
(不好意思 无法传图片 请看附件 )- P( D8 a( w% R
三 : 前台调用- new Mojo.Service.Request("palm://palm.greenwoods.demo/", {
9 B0 ?" `: M- l* Y - method: "status",
. |% W5 ]- W# V3 ~6 O* e: R; a) D - onSuccess: this.listenStatus.bind(this),' W+ }! n. e5 E- r% H) K! w
- onFailure: this.serverFail.bind(this)
+ t. C4 `- o3 e W' ~/ Z; F; L - });
复制代码 注:以上为个人见解 , 可能有不完善之处 ,还望高手指正 ! 献于 Plamer 谢谢. |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?加入我们
×
评分
-
查看全部评分
|