写点什么

PhoneGap 插件编写

用户头像
Android架构
关注
发布于: 59 分钟前

3.包含 PhoneGap 依赖

  • 下载 PhoneGap 并解压

  • 在工程根目录新建目录/libs

  • 拷贝?phonegap.jar?到?/libs


注:由于是写插件,所以只有 phonegap.jar 就够了。要建立完整的 PhoneGap 应用,可参考http://www.phonegap.com/start/#android?

4.实现插件类


代码:


[html]


/**??


  1. ?*?Example?of?Android?PhoneGap?Plugin??

  2. ?*/??

  3. package?com.trial.phonegap.plugin.directorylisting;??

  4. import?java.io.File;??

  5. import?org.json.JSONArray;??

  6. import?org.json.JSONException;??

  7. import?org.json.JSONObject;??

  8. import?android.util.Log;??

  9. import?com.phonegap.api.Plugin;??

  10. import?com.phonegap.api.PluginResult;??

  11. import?com.phonegap.api.PluginResult.Status;??

  12. /**??

  13. ?*?PhoneGap?plugin?which?can?be?involved?in?following?manner?from?javascript??

  14. ?*?<p>??

  15. ?*?result?example?-??

  16. ?*?{"filename":"/sdcard","isdir":true,"children":[{"filename":"a.txt"??

  17. ?*?,"isdir":false},{..}]}??

  18. ?*?</p>??

  19. ?*???

  20. ?*?<pre>??

  21. ?*?{@code??

  22. ?*?successCallback?=?function(result){??

  23. ?*?????//result?is?a?json??

  24. ?*????

  25. ?*?}??

  26. ?*?failureCallback?=?function(error){??

  27. ?*?????//error?is?error?message??

  28. ?*?}??

  29. ?*???

  30. ?*?DirectoryListing.list("/sdcard",??

  31. ?*????????successCallback??

  32. ?*????????failureCallback);??

  33. ?*???????????????????????????????????

  34. ?*?}??

  35. ?*?</pre>??

  36. ?*???

  37. ?*?@author?Rohit?Ghatol??

  38. ?*???

  39. ?*/??

  40. public?class?DirectoryListPlugin?extends?Plugin?{??

  41. ????/**?List?Action?*/??

  42. ????public?static?final?String?ACTION?=?"list";??

  43. ????/*??

  44. ?????*?(non-Javadoc)??

  45. ?????*???

  46. ?????*?@see?com.phonegap.api.Plugin#execute(java.lang.String,??

  47. ?????*?org.json.JSONArray,?java.lang.String)??

  48. ?????*/??

  49. ????@Override??

  50. ????public?PluginResult?execute(String?action,?JSONArray?data,?String?callbackId)?{??

  51. ????????Log.d("DirectoryListPlugin",?"Plugin?Called");??

  52. ????????PluginResult?result?=?null;??

  53. ????????if?(ACTION.equals(action))?{??

  54. ????????????try?{??

  55. ????????????????String?fileName?=?data.getString(0);??

  56. ????????????????JSONObject?fileInfo?=?getDirectoryListing(new?File(fileName));??

  57. ????????????????Log??

  58. ????????????????????????.d("DirectoryListPlugin",?"Returning?"??

  59. ????????????????????????????????+?fileInfo.toString());??

  60. ????????????????result?=?new?PluginResult(Status.OK,?fileInfo);??

  61. ????????????}?catch?(JSONException?jsonEx)?{??

  62. ????????????????Log.d("DirectoryListPlugin",?"Got?JSON?Exception?"??

  63. ????????????????????????+?jsonEx.getMessage());??

  64. ????????????????result?=?new?PluginResult(Status.JSON_EXCEPTION);??

  65. ????????????}??

  66. ????????}?else?{??

  67. ????????????result?=?new?PluginResult(Status.INVALID_ACTION);??

  68. ????????????Log.d("DirectoryListPlugin",?"Invalid?action?:?"?+?action??

  69. ????????????????????+?"?passed");??

  70. ????????}??

  71. ????????return?result;??

  72. ????}??

  73. ????/**??

  74. ?????*?Gets?the?Directory?listing?for?file,?in?JSON?format??

  75. ?????*???

  76. ?????*?@param?file??

  77. ?????*????????????The?file?for?which?we?want?to?do?directory?listing??

  78. ?????*?@return?JSONObject?representation?of?directory?list.?e.g??

  79. ?????*?????????{"filename":"/sdcard"??

  80. ?????*?????????,"isdir":true,"children":[{"filename":"a.txt"??

  81. ?????*?????????,"isdir":false},{..}]}??

  82. ?????*?@throws?JSONException??

  83. ?????*/??

  84. ????private?JSONObject?getDirectoryListing(File?file)?throws?JSONException?{??

  85. ????????JSONObject?fileInfo?=?new?JSONObject();??

  86. ????????fileInfo.put("filename",?file.getName());??

  87. ????????fileInfo.put("isdir",?file.isDirectory());??

  88. ????????if?(file.isDirectory())?{??

  89. ????????????JSONArray?children?=?new?JSONArray();??

  90. ????????????fileInfo.put("children",?children);??

  91. ????????????if?(null?!=?file.listFiles())?{??

  92. ????????????????for?(File?child?:?file.listFiles())?{??

  93. ????????????????????children.put(getDirectoryListing(child));??

  94. ????????????????}??

  95. ????????????}??

  96. ????????}??

  97. ????????return?fileInfo;??

  98. ????}??

  99. }??


5.将插件类导出成 jar 包


Eclipse 中如下操作:


  • 在要生成 jar 的项目上右击,选择菜单上的 Export(导出)

  • 导出类型选择 Jar File

  • 选择或者输入生成路径

  • 选择要导出的类


我们导出成 directorylist.jar

6.实现 JavaScript 插件

  • 创建一个名为 DirectoryListing 的类

  • 创建一个成员函数 list()

  • 在成员函数中调用 PhoneGap.exec(<<successCallback>>,<<failureCallback>>,<<Plugin Name>>,<<Action Name>>,<<Arguments Array>>);

  • 将 js 文件保存为 directorylisting.js


代码:


[javascript]


/**?


  1. ?*???

  2. ?*?@return?Object?literal?singleton?instance?of?DirectoryListing?

  3. ?*/??

  4. var?DirectoryListing?=?{???

  5. /**?

  6. ?*?@param?directory?The?directory?for?which?we?want?the?listing?

  7. ?*?@param?successCallback?The?callback?which?will?be?called?when?directory?listing?is?successful?

  8. ?*?@param?failureCallback?The?callback?which?will?be?called?when?directory?listing?encouters?an?error?

  9. ?*/??

  10. ????list:?function(directory,successCallback,?failureCallback)?{??

  11. ????????return?PhoneGap.exec(successCallback,????????//Success?callback?from?the?plugin ??

  12. ?????????????????????????????failureCallback,????????//Error?callback?from?the?plugin ??

  13. ?????????????????????????????'DirectoryListPlugin',??//Tell?PhoneGap?to?run?"DirectoryListingPlugin"?Plugin ??

  14. ?????????????????????????????'list',?????????????????//Tell?plugin,?which?action?we?want?to?perform ??


《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
浏览器打开:qq.cn.hn/FTe 免费领取
复制代码


?????????????????????????????[directory]);???????????//Passing?list?of?args?to?the?plugin ??


  1. ????}??

  2. };??


三、测试


====

1.创建一个 PhoneGap 应用?http://www.phonegap.com/start/#android

2.将?directorylisting.jar 加入工程依赖

3.将 directorylisting.js 放入到 /assets/www 目录下。

4.在?/res/xml/plugins.xml 文件中添加?

[html]


<plugin?name="DirectoryListPlugin"?value="com.trial.phonegap.plugin.directorylisting.DirectoryListPlugin"?/>??

用户头像

Android架构

关注

还未添加个人签名 2021.10.31 加入

还未添加个人简介

评论

发布
暂无评论
PhoneGap插件编写