[Cordova学习]3.Cordova自己插件的作成步骤

下面将以Android以及iOS做成各自的AlertDilaog插件作为例子。

插件文件夹构造

com.test.nativealert\plugin.xml

<?xml version="1.0" encoding="UTF-8"?>  
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"  
        id="com.test.nativealert"  
        version="0.0.1">  
        <name>NativeAlertPlugin</name>  
        <description>NativeAlertPlugin Description</description>  
        <author>laiqi</author>  
        <license>Apache 2.0 License</license>  
        <engines>  
            <engine name="cordova" version=">=3.0.0" />  
        </engines>  
        <js-module src="www/nativealert.js" name="nativealert">  
            <clobbers target="nativealert" />  
        </js-module>  
        <platform name="android">  
            <source-file src="src/android/NativeAlertPlugin.java" target-dir="src/com/test/nativealert/" />  
            <config-file target="res/xml/config.xml" parent="/*">  
                <feature name="nativealert">  
                    <param name="android-package" value="com.test.nativealert.NativeAlertPlugin"/>  
                </feature>  
            </config-file>  
        </platform>  
      <platform name="ios">  
             <config-file target="config.xml" parent="/*">
                   <feature name="nativealert">
	<param name="ios-package" value="CDVNativeAlert"/>
                   </feature> 
            </config-file>
         <header-file src="src/ios/CDVNativeAlert.h" />
         <source-file src="src/ios/CDVNativeAlert.m" />			
        </platform>  
</plugin>  

JS:                
com.test.nativealert\www\nativealert.js   
var nativealertExport = {};
               
nativealertExport.showAlert = function(a,b,c,d){
               var args = [a,b,c,d];
               var exec = require('cordova/exec');
    exec(null, null, "nativealert", "showAlert", args);
};
               
module.exports = nativealertExport;

exec(null, null, "nativealert", "showAlert", args);    
    第1参数:successCallback
    第2参数:errorCallback,
    第3参数:类名
    第4参数:方法名
    第5参数:传递到Native的桉树
            

public class NativeAlertPlugin extends CordovaPlugin implements OnClickListener{

public static final String TAG = "NativeAlertPlugin";  
public Activity context;
  
    public void initialize(CordovaInterface cordova, CordovaWebView webView) {  
        super.initialize(cordova, webView);  
        context = this.cordova.getActivity();  
    }  
  
    @Override  
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {  
       
    if(action.equals("showAlert")) {
    String title = args.getString(0);
    String message  =  args.getString(1);
    String bt1 = args.getString(2);
    String bt2 = args.getString(3);
    AlertDialog.Builder builder =  new AlertDialog.Builder(context);
    builder.setPositiveButton(bt1, this);
    builder.setNegativeButton(bt2, this);
    builder.setTitle(title).setMessage(message).show();
    }
        return true;  
    }

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}  
CordovaPlugin类继承有下面2个方法
public void initialize(CordovaInterface cordova, CordovaWebView webView) 初始化方法        
    第1参数:    cordova:Acitivity持有的类
    第2参数:    webview:正在运行的Webview实例
CordovaPlugin类继承有下面2个方法
public void initialize(CordovaInterface cordova, CordovaWebView webView) 初始化方法        
    第1参数:    cordova:Acitivity持有的类
    第2参数:    webview:正在运行的Webview实例

public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException  (必须复写)        
    第1参数:    action:nativealert.js的exec的第4参数方法名
    第2参数:    args:nativealert.js的exec的第5参数的传递参数
    第3参数:    callbackContext:回调函数結果设置的context    
        例:    
        正常系:    
            PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT);
            callbackContext.sendPluginResult(r);
        異常系    
                        callbackContext.error("Illegal Argument Exception");    
                        PluginResult r = new PluginResult(PluginResult.Status.ERROR);    
                        callbackContext.sendPluginResult(r);   



iOS的Native代码

com.test.nativealert\CDVNativeAlert.m

@implementation CDVNativeAlert
-(void)showAlert:(CDVInvokedUrlCommand*)command {          
  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:command.arguments[0]                                                         
                message:command.arguments[1]                                                  
                 delegate:self 
                                         cancelButtonTitle:command.arguments[2]           
                                  otherButtonTitles:command.arguments[3], nil];    
              [alert show];
}
com.test.nativealert\CDVNativeAlert.h
#import <Cordova/CDVPlugin.h>
@interface CDVNativeAlert : CDVPlugin
-(void)showAlert:(CDVInvokedUrlCommand*)command;
@end
iOS版,没有必须继承的类,

-(void)showAlert:(CDVInvokedUrlCommand*)command;                        
方法名:    showAlert: nativealert.js的exec的第4参数的方法名                    
参数:    CDVInvokedUrlCommand*  传递参数                    
     command.arguments:nativealert.js的第5个参数的传递参数            

插件API使用

1.Cordova平台文件夹下www/index.html、deviceready事件执行完后
  呼出nativeAlert的API。

document.addEventListener("deviceready",onDeviceReady,false);
function onDeviceReady(){
      showAlert();
}
function showAlert(){
    nativealert.showAlert("title","content","ok","cancel");
}


生成插件步骤

1.com.test.nativealert文件夹放在D:\    
2.生成Cordova的测试工程HelloWorld    
3.添加Cordova的Android/iOS平台支持。   
4.添加Cordova的nativealert插件。    
    Cordova plugin add D:\com.test.nativealert
5.编译工程HelloWorld 运行Android以及iOS。    

运行结果:




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值