PhoneGap Plugin 之 Hello World
文章目录
1、编写Java类并继承Plugin:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public class MyPlugin extends Plugin { @SuppressWarnings("deprecation") @Override public PluginResult execute(String action, JSONArray args, String callbackId) { PluginResult result = new PluginResult(Status.INVALID_ACTION); if (action.equals("hello")) { try { Toast.makeText(cordova.getContext(), "Hello World!", Toast.LENGTH_SHORT).show(); result = new PluginResult(Status.OK, "Hello World!"); } catch (NameNotFoundException nnfe) { result = new PluginResult(Status.ERROR, nnfe.getMessage()); } } return result; } } |
2、配置 res/xml/config.xml 文件:
1
|
<plugin name="MyPlugin" value="com.wenyi.hello.MyPlugin"/> |
3、新建 plugin.js 文件:
1
|
window.myplugin = { |
hello: function(callback) { cordova.exec(callback, function(err) { callback(err); }, “MyPlugin”, “hello”, []); } };
API: cordova.exec(success, error, service, action, args);
4、使用:
1
|
myplugin.hello(function(data) { |
console.log(data); //Hello World! });
文章作者 wenzhixin
上次更新 2012-08-01