phoneGap(cordova)插件开发学习

1、下载phonegap(cordova),http://pan.baidu.com/s/1c0379Iw

2、创建工程,进入解压文件夹~\cordova-android-master\bin,执行\cordova-android-master\bin>create<工程存储路径><包名><工程名>,如:create Test com.test AndrdTest

3、将创建的工程导入eclipse中。

4、创建js调用native插件,新建类继承CordovaPlugin,重写execute方法

 public class ToastPlugin extends CordovaPlugin {
@Override
public boolean execute(String action, JSONArray args,
CallbackContext callbackContext) throws JSONException {

Toast.makeText(MyApplication.getContext(), "success", 100).show();
Log.e("********传入的参数*******", args.getString(0));
if(action.equals("false")){
callbackContext.success("success");
return true;
}else{
callbackContext.error("faile");
return false;
}
}
}

5、在res/xml/config.xml文件中注册插件

<feature name="ToastPlugin">
      <param name="android-package" value="com.iraid.phonegap.ToastPlugin"/>
    </feature>

6、新建js文件,如:ToastPlugin.js

var toastPlugin =
    { createEvent: function(name, notes, successCallback, errorCallback)
        {
            cordova.exec(
                successCallback, // success callback function
                errorCallback, // error callback function
                'ToastPlugin', // 与config.xml中的name相同
                'true', // execute方法中的action参数
                [{      // and this array of custom arguments to create our entry
                    "name": name,
                    "description": notes
                }]
            );
        }
    };

7、在html中引用创建的js文件,并调用

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <meta name="format-detection" content="telephone=no" />
        <!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
        <title>Hello World</title>
    </head>
    <body>
        <input type="button" value="Toast" onClick="send()"/>
        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="js/ToastPlugin.js"></script>
        <script type="text/javascript" src="js/index.js"></script>
        <script type="text/javascript">
            app.initialize();
            
            function send(){
            var notes = "zhou";  
                var title = "PhoneGap Day";  
                var success = function(message) { alert( message); };  
                var error = function(message) { alert(message); };  
                toastPlugin.createEvent(title,notes, success, error);
            }            
        </script>
    </body>
</html>

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值