经过了一段时间的学习,初步了解了该如何使用jQuery Mobile和 Phone Gap来开发一个Android应用程序,也想把这些东西介绍给大家。
1、 软件准备
要进行android app的开发,当然需要准备Java, eclipse和安装Android SDK,这个部分网络上面很多方法,搜索“安装Android SDK”即可找到很多答案,所以就不再这里浪费口水。

2、 知识准备
(1)了解jQuery Mobile这个js框架,知道怎么组织一个简单的页面。
官方网站:http://jquerymobile.com/(记得下载一个js库文件)
(2)了解Phone Gap,怎么利用Phone Gap在后面的内容也有介绍。
官方网站:http://phonegap.com/(同样记得下载相关文件)
(3)能够使用jQuery进行开发。

3、 组织工程目录
(1)打开Eclipse,建立一个android应用工程,见下图



(2)解压phonegap的压缩包,可以看到它针对不懂的应用类型进行了不同的分类,有android、IOS、Windows Phone等移动终端系统,打开其中的android文件夹。
(3)在刚才新建的工程的根目录下新建一个名为libs的文件夹,找到(1)中android文件夹中的jar包粘贴到刚才的libs文件夹下。
(4)将(1)中android文件夹下的xml文件夹整个粘贴到工程更目录下的res文件夹下。
(5)在工程的assets文件夹下新建文件夹www,这个文件夹其实可以看作是phonegap的工程目录,用来放js或者html文件。
(6)在文件夹www下面新建一个js文件夹,用来放置js和css文件;新建文件夹pages用来放置html文件。(新建html和引入js库可以参照图操作)
工程目录如下图:



4 Conding
(1)首先打开src下的Java类,修改继承类为DroidGap(如果找不到这个类,估计是忘记将PhoneGap的jar包加入工程的Libraries),并且修改代码,如下图



(2)打开index.html文件,进行编辑,记得开头要用html5的doctype声明。我在里面加入两个简单的jQuery Mobile的页面,并且调用了简单的Phone Gap的API:
http://docs.phonegap.com/en/1.3.0/phonegap_notification_notification.md.html#notification.vibrate
代码如下:
  1. <!Doctypehtml>
  2. <html>
  3. <head>
  4. <title>PhoneGapIntroduce</title>
  5. <metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
  6. <linkrel="stylesheet"type="text/css"href="../JS/jquery.mobile-1.0rc1.min.css"/>
  7. <scripttype="text/javascript"src="../JS/jquery_1_6_4.js"></script>
  8. <scripttype="text/javascript"src="../JS/phonegap-1.2.0.js"></script>
  9. <scripttype="text/javascript"src="../JS/jquery.mobile-1.0rc1.js"></script>
  10. <scripttype="text/javascript">
  11. $('#PageOne').live('pageinit',function(event){
  12. varshowTip=function(){
  13. navigator.notification.alert("thisisamessagefrompageone!",null,"Message","Close");
  14. $(this).die("click");
  15. };
  16. varconfirm=function(){
  17. navigator.notification.confirm(
  18. 'Youarethewinner!',//message
  19. null,//callbacktoinvokewithindexofbuttonpressed
  20. 'GameOver',//title
  21. 'Restart,Exit'//buttonLabels
  22. );
  23. $(this).die("click");
  24. };
  25. varredirectPage=function(){
  26. $.mobile.changePage("#PageTwo");
  27. $(this).die("click");
  28. };
  29. $(event.target).find('#alert').bind('click',showTip);
  30. $(event.target).find('#confirm').bind('click',confirm);
  31. $(event.target).find('#changePage').bind('click',redirectPage);
  32. });
  33. $('#PageTwo').live('pageshow',function(event){
  34. varshowTip=function(){
  35. navigator.notification.alert("thisisamessagefrompagetwo!",null,"Message","Close");
  36. $(this).die("click");
  37. };
  38. varconfirm=function(){
  39. navigator.notification.confirm(
  40. 'Youarethelosser!',//message
  41. null,//callbacktoinvokewithindexofbuttonpressed
  42. 'GameOver',//title
  43. 'Restart,Exit'//buttonLabels
  44. );
  45. $(this).die("click");
  46. };
  47. $(event.target).find('#alert').bind('click',showTip);
  48. $(event.target).find('#confirm').bind('click',confirm);
  49. });
  50. </script>
  51. </head>
  52. <body>
  53. <divid="PageOne"data-role="page">
  54. <divdata-role="header"data-backbtn="false">
  55. <h1>PhoneGapOne</h1>
  56. </div>
  57. <divdata-role="content">
  58. <div>
  59. <ahref="#"id="alert"data-role="button"data-theme="b">Alert</a>
  60. </div>
  61. <div>
  62. <ahref="#"id="confirm"data-role="button"data-theme="b">Confirm</a>
  63. </div>
  64. <div>
  65. <ahref="#"id="changePage"data-role="button"data-theme="b">ChangePage</a>
  66. </div>
  67. </div>
  68. <divdata-role="footer">
  69. <divdata-role="navbar">
  70. <ul>
  71. <li><ahref="#PageOne">PageOne</a></li>
  72. <li><ahref="#PageTwo">PageTwo</a></li>
  73. </ul>
  74. </div>
  75. </div>
  76. </div>
  77. <divid="PageTwo"data-role="page">
  78. <divdata-role="header"data-backbtn="true">
  79. <h1>PhoneGapTwo</h1>
  80. <adata-role="button"data-rel="back">Previous</a>
  81. </div>
  82. <divdata-role="content">
  83. <div>
  84. <ahref="#"id="alert"data-role="button"data-theme="b">Alert</a>
  85. </div>
  86. <div>
  87. <ahref="#"id="confirm"data-role="button"data-theme="b">Confirm</a>
  88. </div>
  89. <div>
  90. <ahref="file:///android_asset/www/Pages/pageThree.html"data-role="button"data-theme="b">PageThree</a>
  91. </div>
  92. </div>
  93. <divdata-role="footer">
  94. <divdata-role="navbar">
  95. <ul>
  96. <li><ahref="#PageOne">PageOne</a></li>
  97. <li><ahref="#PageTwo">PageTwo</a></li>
  98. </ul>
  99. </div>
  100. </div>
  101. </div>
  102. </body>
  103. </html>

要特别注意的是引入js库的顺序,参照下图:


即:自己的包和phonegap的js包要放在中间,不然会出一些错误,我开发的时候是遇见过这种状况的,而且jQuery Mobile的官方也是这么要求的。

再打开pageThree.html,加入如下代码:
  1. <divid="PageThree"data-role="page">
  2. <divdata-role="header"data-backbtn="true">
  3. <h1>PhoneGapThree</h1>
  4. <adata-role="button"data-rel="back">Previous</a>
  5. </div>
  6. <divdata-role="content">
  7. <div>
  8. <ahref="#"id="alert"data-role="button"data-theme="b">Alert</a>
  9. </div>
  10. <div>
  11. <ahref="#"id="confirm"data-role="button"data-theme="b">Confirm</a>
  12. </div>
  13. </div>
  14. <divdata-role="footer">
  15. <divdata-role="navbar">
  16. <ul>
  17. <li><ahref="#PageOne">PageOne</a></li>
  18. <li><ahref="#PageTwo">PageTwo</a></li>
  19. </ul>
  20. </div>
  21. </div>
  22. </div>


选择工程,右键run as > android application,你应该能够看到下图:



到这里工程的开发已经完了,希望有兴趣的可以具体操作一遍,然后可以修修改改其中的一些东西,这样才能体会到这个开发过程是怎么一回事,光看和复制粘贴是很容易忘记怎么去开发的。


在我进行了一段时间的开发之后,我认为phonegap的好处在于:
(1)一个应用能够很容易就移植到其他的平台,不需要同样的逻辑写多种语言版本;
(2)容易上手,学习了html5和js既可以进行开发;
(3)如果学会了如何开发phonegap插件,那么android能够做的事情,phonegap都能够帮你完成,其他平台开发也如此。(如何开发插件已经不是这篇blog的内容了)
同时我感觉phonegap让我最不爽的一点就是调试太麻烦了,要在模拟器上才能看到效果到底对不对。

同时附上开发简易顺序:
(1)把phonegap的jar包和xml文件放到工程下的正确目录;
(2)修改src下的android默认类,参照4 (1);
(3)在aseets下面建立工程的根目录www,并在其中放入js、html、image、css等普通的web文件;
(4)只需要在index页面加入js包和css文件,其他页面只需要组织成一个简单的jQuery Mobile页面。
(5)调用一些特殊的api时,需要注意申请android许可!(百度一下就可以轻松解决)

最后一个压缩包是工程压缩包。
  • Introduce.7z(595.5 KB)
  • 描述: 工程压缩包

更多相关文章

  1. android动态加载(二)
  2. Android(安卓)Studio(4)---开发人员工作流程基础
  3. 利用HTML5开发Android笔记(中篇)
  4. Android高级开发第三讲--应用程序基础
  5. Android(安卓)4.0原生SDK发布,引入新的流式音频API
  6. 【安卓开发】Facebook工程师是如何改进他们Android客户端的
  7. Android开发---从简单的TextView看Android的控件显示方式
  8. android 工程中重新生成gen文件夹或R.java 文件
  9. Android(安卓)开发简介-系统架构

随机推荐

  1. android 问题分析解析,有你想要的吗
  2. Android手势源码浅析----手势识别
  3. View那些事儿(1) -- View绘制的整体流程
  4. 自定义View基础:Android中的颜色和颜色混
  5. Android中遍历View的几种方式
  6. Android的文字渲染
  7. Android(安卓)不得不说的VideoView的一些
  8. 彻底搞清楚Android中的 Attr
  9. UE高级性能剖析技术(三)-- Android内存分布
  10. Android(安卓)合并AAR踩坑之旅