---
title: "phonegap-plugin"
author: "fanfq"
date: "2012-09-24T21:56:05Z"
tags: ["HTML5", "Mobile", "PhoneGap"]
categories: ["移动开发"]
slug: "phonegap-plugin"
draft: false
lastmod: "2012-09-24T21:56:05Z"
id: 1684633
url: "https://fanfq.iteye.com/blog/1684633"
---

说白就是使用js调用java代码，蛮有意思的，不难看出phonegap的很多类库都是使用plugin的模式做的。 index.html \[code="html"] Storage Example

# Example

Plugin \[/code] res/xml/config.xml \[color=red]\*这部需要注意一下，不要忘记配置\[/color] \[code="xml"] \[/code] src/com/fanfq/phonegap/plugin/MyPlugin.java \[code="java"] package com.fanfq.phonegap.plugin; import org.apache.cordova.api.Plugin; import org.apache.cordova.api.PluginResult; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; public class MyPlugin extends Plugin { /\** * Executes the request and returns PluginResult. * * @param action The action to execute. * @param args JSONArry of arguments for the plugin. * @param callbackId The callback id used when calling back into JavaScript. * @return A PluginResult object with a status and message. \*/ public PluginResult execute(String action, JSONArray args, String callbackId) { try { if (action.equals("echo")) { String echo = args.getString(0); if (echo != null && echo.length() &gt; 0) { return new PluginResult(PluginResult.Status.OK, "hello fanfq!"); } else { return new PluginResult(PluginResult.Status.ERROR); } } else { return new PluginResult(PluginResult.Status.INVALID\_ACTION); } } catch (JSONException e) { return new PluginResult(PluginResult.Status.JSON\_EXCEPTION,"error"); } } } \[/code] 附件中含完整工程，问题请留言。