-
*
-
* DirectoryListing.list("/sdcard",
-
* successCallback
-
* failureCallback);
-
*
-
* }
-
* </pre>
-
*
-
* @author Rohit Ghatol
-
*
-
*/
-
public class DirectoryListPlugin extends Plugin {
-
/** List Action */
-
public static final String ACTION = “list”;
-
/*
-
* (non-Javadoc)
-
*
-
* @see com.phonegap.api.Plugin#execute(java.lang.String,
-
* org.json.JSONArray, java.lang.String)
-
*/
-
@Override
-
public PluginResult execute(String action, JSONArray data, String callbackId) {
-
Log.d(“DirectoryListPlugin”, “Plugin Called”);
-
PluginResult result = null;
-
if (ACTION.equals(action)) {
-
try {
-
String fileName = data.getString(0);
-
JSONObject fileInfo = getDirectoryListing(new File(fileName));
-
Log
-
.d(“DirectoryListPlugin”, "Returning "
-
+ fileInfo.toString());
-
result = new PluginResult(Status.OK, fileInfo);
-
} catch (JSONException jsonEx) {
-
Log.d(“DirectoryListPlugin”, "Got JSON Exception "
-
+ jsonEx.getMessage());
-
result = new PluginResult(Status.JSON_EXCEPTION);
-
}
-
} else {
-
result = new PluginResult(Status.INVALID_ACTION);
-
Log.d(“DirectoryListPlugin”, "Invalid action : " + action
-
+ " passed");
-
}
-
return result;
-
}
-
/**
-
* Gets the Directory listing for file, in JSON format
-
*
-
* @param file
-
* The file for which we want to do directory listing
-
* @return JSONObject representation of directory list. e.g
-
* {“filename”:"/sdcard"
-
* ,“isdir”:true,“children”:[{“filename”:“a.txt”
-
* ,“isdir”:false},{…}]}
-
* @throws JSONException
-
*/
-
private JSONObject getDirectoryListing(File file) throws JSONException {
-
JSONObject fileInfo = new JSONObject();
-
fileInfo.put(“filename”, file.getName());
-
fileInfo.put(“isdir”, file.isDirectory());
-
if (file.isDirectory()) {
-
JSONArray children = new JSONArray();
-
fileInfo.put(“children”, children);
-
if (null != file.listFiles()) {
-
for (File child : file.listFiles()) {
-
children.put(getDirectoryListing(child));
-
}
-
}
-
}
-
return fileInfo;
-
}
-
}
5.将插件类导出成jar 包
Eclipse中如下操作:
-
在要生成jar的项目上右击,选择菜单上的Export(导出)
-
导出类型选择Jar File
-
选择或者输入生成路径
-
选择要导出的类
我们导出成directorylist.jar
6.实现JavaScript插件
-
创建一个名为DirectoryListing的类
-
创建一个成员函数list()
-
在成员函数中调用PhoneGap.exec(<>,<>,<>,<>,<>);
-
将js文件保存为directorylisting.js
代码:
[javascript]
/**
-
*
-
* @return Object literal singleton instance of DirectoryListing
-
*/
-
var DirectoryListing = {
-
/**
-
* @param directory The directory for which we want the listing
-
* @param successCallback The callback which will be called when directory listing is successful
-
* @param failureCallback The callback which will be called when directory listing encouters an error
-
*/
-
list: function(directory,successCallback, failureCallback) {
-
return PhoneGap.exec(successCallback, //Success callback from the plugin
-
failureCallback, //Error callback from the plugin
-
‘DirectoryListPlugin’, //Tell PhoneGap to run “DirectoryListingPlugin” Plugin
-
‘list’, //Tell plugin, which action we want to perform
-
[directory]); //Passing list of args to the plugin
-
}
-
};
三、测试
====
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” />
5.在index.html中调用DirectoryListing.list
代码:
[html]
总结
Android架构学习进阶是一条漫长而艰苦的道路,不能靠一时激情,更不是熬几天几夜就能学好的,必须养成平时努力学习的习惯。所以:贵在坚持!
上面分享的字节跳动公司2021年的面试真题解析大全,笔者还把一线互联网企业主流面试技术要点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节。
【Android学习PDF+学习视频+面试文档+知识点笔记】
【Android高级架构视频学习资源】
(img-TFQMza8Y-1645109704149)]
【Android学习PDF+学习视频+面试文档+知识点笔记】
【Android高级架构视频学习资源】
Android部分精讲视频领取学习后更加是如虎添翼!进军BATJ大厂等(备战)!现在都说互联网寒冬,其实无非就是你上错了车,且穿的少(技能),要是你上对车,自身技术能力够强,公司换掉的代价大,怎么可能会被裁掉,都是淘汰末端的业务Curd而已!现如今市场上初级程序员泛滥,这套教程针对Android开发工程师1-6年的人员、正处于瓶颈期,想要年后突破自己涨薪的,进阶Android中高级、架构师对你更是如鱼得水,赶快领取吧!