Develop your own plug-ins using CORDOVA

Source: Internet
Author: User

You still need to develop your own plug-ins...

My Cordova version is 4.0.0

1. Create a folder named myplugin.

1.1 create a new plugin. xml file in the myplugin folder.

1.2 create an android IOS folder in the SRC folder


2. the console. Java file in the android folder

 
1 package cn.debi.cordova;
 2 import org.apache.cordova.CordovaWebView;
 3 import org.apache.cordova.CallbackContext;
 4 import org.apache.cordova.CordovaPlugin;
 5 import org.apache.cordova.CordovaInterface;
 6 import org.json.JSONArray;
 7 import org.json.JSONException;
 8 import org.json.JSONObject;
12 import android.util.Log;
13 public class Console extends CordovaPlugin {
14 
15     public boolean execute(String action, JSONArray args, CallbackContext callbackContext) 
16             throws JSONException {
17          String databack=args.getString(0);
18         final String ACTIVITY_TAG="MyAndroid"; 20         if (action.equals("Consolelog")) {
21             Log.i(ACTIVITY_TAG,databack);
22             callbackContext.success(databack);
23             return true;
24         }
25         return false;
26     }
27 }

3. Create a console. js file in the WWW folder.

 

1 var exec = require (‘cordova / exec’);
2 
3 exports.setConsole = function (messege, success, error) {
4 exec (success, error, "Console", "Consolelog", [messege]); // console is the class name of java, consolelog is the string of action
5};

4. In plugin. xml

 
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <plugin id="cn.debi.cordova" 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>Console</name>
 6     <description>Description</description>
 7     <js-module name="Console" src="www/Console.js">
 8         <clobbers target="cordova.plugins.Console"/>
 9     </js-module>
10     <platform name="android">
11         <config-file parent="/*" target="res/xml/config.xml">
12             <feature name="Console">
13                 <param name="android-package" value="cn.debi.cordova.Console"/>
14             </feature>
15         </config-file>
16         <source-file src="src/android/Console.java" target-dir="src/cn/debi/cordova"/>
17     </platform>
18 </plugin>

Where

<Plugin id = "cn. Debi. Cordova" version = "0.0.1">

The ID in </plugin> can be written at will. It is the identifier of the plug-in. The version can be written at will.

  • Description: The description is written casually.
  • JS-module: corresponds to our Javascript file,srcAttribute pointingWWW/console. js name is used when we call JS
  • Platform: supported platform. Android is used only.
  • In <Param name = "Android-package" value = "cn. Debi. Cordova. Console"/>, CN. Debi. Cordova is the package name of console. java.

6. Call the plug-in by using the plug-in method

 var extraInfo = cordova.require(‘cn.debi.cordova.Console‘); 
2     extraInfo.setConsole(‘cole.log‘,function(message) {
3          alert(message);
4     }, function(message) {
5          alert(message);
6     });

Here, CN. Debi. Cordova. Console in Cordova. Require ('cn. Debi. Cordova. console') is the ID in plugin. xml; console is the name in JS-module.

That is, call console. js to export the Method

~ Extrainfo. setconsole () is the method for calling the export.

7. Forgot to add the plug-in O (plugin _ plugin) O ~~

1 Cordova plugin add myplugin # directory name, which can also be the GIT address
1 cordova plugin add  /your/plugin/address/myplugin 

 

Develop your own plug-ins using CORDOVA

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.