【小编推荐】app开发之phonegap插件(安卓)

2014-06-25   |   发布者:梁国芳   |   查看:3320次

app开发
 在做APP的时候 ,假如使用phonegap 开发,那么一定会用到插件的,因为phonegap不知道你会使用哪些插件。来适应高速发展的互联网。
其实还是比较简单的,首先新建一个类   这个类专门是用来写插件的。本文插件式针对phonegap 2.9的。
比如类名为SimplePlugin  继承 CordovaPlugin 
这个类和  主类 在同一个包里面。
发上代码来瞅瞅:

public class SimplePlugin extends CordovaPlugin {
private String mClientId;
private ShareContent mShareContent;
    @Override
    public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
        if (action.equals("echo")) {
            String message = args.getString(0);
        //String message = "HI,this is java hello!";
            this.echo(message, callbackContext);
            return true;
        }
        else if(action.equals("showProcess")){
        //���������DEMO
        final String title=args.getString(0);
        final String content=args.getString(1);
        cordova.getActivity().runOnUiThread(new Runnable() {
                public void run() {
                ProgressDialog progressDialog = ProgressDialog.show(cordova.getActivity(), title, content);
            progressDialog.setCancelable(true);
                    callbackContext.success(); // Thread-safe.
                }
            });
        return true;
        }
  

    private void echo(String message, CallbackContext callbackContext) {
        if (message != null && message.length() > 0) {
            callbackContext.success(message);
        } else {
            callbackContext.error("Expected one non-empty string argument.");
        }
    }
}


在xml中增加插件    res/xml/config.xml 中
    <plugins>  
       <plugin name="SimplePlugin" value="com.longguang.longguang.SimplePlugin"/>   

    </plugins>  

然后就是js代用的了。   

从插件类中可以看出    else if(action.equals("showShare")){  有很多  elseif   每一个if  都是一个插件       

在html中js代码可以调用这些插件      

function  xx(){
        cordova.exec(successFunction2, failFunction, "SimplePlugin","tishi",[]);
}
第三个参数 是  SimplePlugin  这个即是类名   tishi  就是  if中的。 表示一个插件  []中是参数。

在   类插件中这样表示  



final String title=args.getString(0);
final String content=args.getString(1);
第一个 参数  第二个参数 

以上就完成了 Android  客户端   插件的开发。