之前开发cordova 的插件都是区分Android 和iOS 的;
现在把这两个合并起来,其中文件的内容跟之前分开写的插件没有区别,只不过是把他们合起来的而已;

新建如下图片的文件夹和文件;


在各自的平台里面,当添加插件时,都会在cordova_plugins.js文件里面写入添加插件的信息;如这次添加的这个插件

上面对应的关系如下面的图片

现在写具体的文件内容:

1.在plugin.xml中添加一下的内容:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <plugin id="mtel.debbie.plugins" version="0.0.1" 
 3         xmlns="http://apache.org/cordova/ns/plugins/1.0"
 4         xmlns:android="http://schemas.android.com/apk/res/android">
 5     <name>plugintest</name>
 6     <description>Description</description>
 7     <js-module name="PluginTest" src="www/PluginTest.js">
 8         <clobbers target="PluginTest"/>
 9     </js-module>
10     
11     <platform name="android">
12         <config-file parent="/*" target="res/xml/config.xml">
13             <feature name="PluginTest">
14                 <param name="android-package" value="mtel.debbie.cordovaplugin.PluginTest"/>
15             </feature>
16         </config-file>
17         <source-file src="src/android/PluginTest.java" target-dir="src/mtel/debbie/cordovaplugin"/>
18     </platform>
19 
20         <platform name="ios">
21         <config-file target="config.xml" parent="/*">
22             <feature name="PluginTest">
23                 <param name="ios-package" value="PluginTest"/>
24             </feature>
25     </config-file>
26     <header-file src="src/ios/PluginTest.h"/>
27     <source-file src="src/ios/PluginTest.m" />
28     </platform>
29 </plugin>
View Code

2.在DebbiePlugin-->src-->android-->PluginTest.java文件中添加一下的内容;

  1 package mtel.debbie.cordovaplugin;
  2 
  3 import org.apache.cordova.CordovaWebView;
  4 import org.apache.cordova.CallbackContext;
  5 import org.apache.cordova.CordovaPlugin;
  6 import org.apache.cordova.CordovaInterface;
  7 import org.apache.cordova.PluginResult;
  8 import org.json.JSONArray;
  9 import org.json.JSONException;
 10 import org.json.JSONObject;
 11 
 12 import android.annotation.SuppressLint;
 13 import android.app.Activity;
 14 import android.app.AlertDialog;
 15 import android.app.AlertDialog.Builder;
 16 import android.content.DialogInterface;
 17 import android.content.Intent;
 18 import android.util.Log;
 19 
 20 public class PluginTest extends CordovaPlugin {
 21 
 22     public boolean execute(String action, JSONArray args,
 23             CallbackContext callbackContext) throws JSONException {
 24         String back = "my ok";
 25 
 26         final String ACTIVITY_TAG = "MyAndroid";
 27         final String ACTIVITY_TAG1 = "myalert";
 28 
 29         Activity activity = this.cordova.getActivity();
 30         if (action.equals("setdatas")) {
 31             String mMassage = args.getString(0);
 32             String mTitle = args.getString(1);
 33             JSONArray mArray = args.getJSONArray(2);
 34             Log.i(ACTIVITY_TAG, mMassage);
 35             this.alertshow(mMassage, mTitle, mArray, callbackContext);
 36             // callbackContext.success(mMassage);
 37             return true;
 38         } else if (action.equals("getdatas")) {
 39             String mMassage = args.getString(0);
 40             Log.i(ACTIVITY_TAG1, mMassage);
 41             callbackContext.success(mMassage);
 42             return true;
 43         }
 44         return false;
 45     }
 46 
 47     /**
 48      * 显示对话框
 49      * 
 50      * @param _message
 51      * @param _title
 52      * @param _buttons
 53      * @param callbackContext
 54      */
 55     public void alertshow(final String _message, final String _title,
 56             final JSONArray _buttons, final CallbackContext callbackContext) {
 57         final CordovaInterface cordova = this.cordova;
 58         Runnable runnable = new Runnable() {
 59 
 60             @SuppressLint("NewApi")
 61             @Override
 62             public void run() {
 63                 // TODO Auto-generated method stub
 64                 AlertDialog.Builder alert = new AlertDialog.Builder(
 65                         cordova.getActivity(),
 66                         AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
 67                 alert.setTitle(_title);
 68                 alert.setMessage(_message);
 69                 if (_buttons.length() > 0) {
 70                     try {
 71                         alert.setNegativeButton(_buttons.getString(0),
 72                                 new AlertDialog.OnClickListener() {
 73 
 74                                     @Override
 75                                     public void onClick(DialogInterface dialog,
 76                                             int which) {
 77                                         // TODO Auto-generated method stub
 78                                         dialog.dismiss();
 79                                         callbackContext
 80                                                 .success("you click sure button ");
 81 
 82                                     }
 83 
 84                                 });
 85                     } catch (JSONException e) {
 86                         // TODO Auto-generated catch block
 87                         e.printStackTrace();
 88                     }
 89                 }
 90                 if (_buttons.length() > 1) {
 91                     try {
 92                         alert.setNeutralButton(_buttons.getString(1),
 93                                 new AlertDialog.OnClickListener() {
 94 
 95                                     @Override
 96                                     public void onClick(DialogInterface dialog,
 97                                             int which) {
 98                                         // TODO Auto-generated method stub
 99                                         dialog.dismiss();
100                                         callbackContext
101                                                 .success("you click cancel button");
102                                     }
103 
104                                 });
105                     } catch (JSONException e) {
106                         // TODO Auto-generated catch block
107                         e.printStackTrace();
108                     }
109 
110                 }
111 
112                 alert.create().show();
113             }
114         };
115         this.cordova.getActivity().runOnUiThread(runnable);
116     }
117 }
View Code

 


3.1 在DebbiePlugin-->src-->ios-->PluginTest.h文件中添加一下的内容;

//#import <Cordova/Cordova.h> //this is error import
#import <Cordova/CDVPlugin.h>
@interface PluginTest : CDVPlugin
-(void) setdatas:(CDVInvokedUrlCommand*)command;
-(void) getdatas:(CDVInvokedUrlCommand*)command;
@end

 


3.2 在DebbiePlugin-->src-->ios-->PluginTest.m文件中添加一下的内容;

 1 /**
 2  
 3  **/
 4 
 5 #import "PluginTest.h"
 6 
 7 @implementation PluginTest
 8 -(void) setdatas:(CDVInvokedUrlCommand *)command
 9 {
10     NSString *message = [command.arguments objectAtIndex:0];//这是我JavaScripte 传来的数据;
11     NSString *title=[command.arguments objectAtIndex:1];
12     NSArray  *buttons=[command.arguments objectAtIndex:2];
13     NSString *yesbutton=[buttons objectAtIndex:0];
14     NSString *nobutton=[buttons objectAtIndex:1];
15     
16     BOOL arg = YES;
17     CDVPluginResult* result;//
18     
19     if (arg)
20     {
21         // Success Callback
22         result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"我返回的数据"];
23         
24         UIAlertView *alert=[[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:nobutton otherButtonTitles:yesbutton, nil];
25         [alert show];
26         //[self writeJavascript:[result toSuccessCallbackString:command.callbackId]];//version=3.6
27         [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];//version =4.0
28     }
29     else
30     {
31         // Error Callback
32         result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"error"];
33         [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];//version =4.0
34         //[self writeJavascript:[result toErrorCallbackString:command.callbackId]];
35     }
36 }
37 -(void) getdatas:(CDVInvokedUrlCommand *)command{
38     CDVPluginResult* resultwrite;
39     UIAlertView *alertwrite=[[UIAlertView alloc] initWithTitle:@"this is write" message:@"this is data for write" delegate:self cancelButtonTitle:@"cancelBtn" otherButtonTitles:@"sureBtn", nil];
40     [alertwrite show];
41     resultwrite=[CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"this is data that i callback"];
42     [self.commandDelegate sendPluginResult:resultwrite callbackId:command.callbackId];
43 }
44 @end
View Code

 


4. 在DebbiePlugin-->www-->PluginTest.js文件中添加一下的内容;

 1 var exec = require('cordova/exec');
 2 
 3 module.exports={
 4     SetData:function(messege,_title,_buttons,success, error) {
 5     exec(success, error, "PluginTest", "setdatas", [messege,_title,_buttons]);
 6 },
 7    GetData:function(messege,_title,success, error) {
 8     exec(success, error, "PluginTest", "getdatas", [messege,_title]);
 9 }
10 }
View Code

 


5.最后的是这个插件的使用方法;DebbiePlugin-->test-->test.js文件中添加一下的内容;

PluginTest.SetData("mymessage", "mtitle",["yes","no"], function(_message) {
    console.log(_message);
}, function(_message) {
    console.log(_message);
});
View Code

注:不要忘记了添加此插件

posted on 2015-01-13 18:49  ทดสอบ  阅读(549)  评论(0编辑  收藏  举报