Recently work projects, need to use Cordova for plug-in development, specific Cordova role, will not repeat, we can go to Baidu on their own OK, direct start. Specific process, I will have a small demo to advance the explanation. is also just contact, too theory of the foundation of things I also say not good, or first run up a demo, only to continue to learn the power ~ everyone more advice ~
Step1. Preparatory work:
First I introduced the Helloworld-cordovalib in my demo instance package into the workspace, I was using eclipse, and then I created the project Multiimagechooser, The Helloworld-cordovalib is also introduced into the Multiimagechooser as a library:
Next, follow the directory structure in the demo instance package, introduce the files required by Cordova, and the following directory structure is as follows:
Where the Res folder also has an XML folder, remember to copy the past Oh ~
As of now, the basic preparatory work is finished.
Step2. Plug-in development
Plug-in writing, is to let JS can call my activity, in fact, it is relatively simple to write up.
A. Build the package plugins in the SRC directory and write the plug-in class Plugin_intent
Package Plugins;import Org.apache.cordova.callbackcontext;import Org.apache.cordova.cordovaplugin;import Android.content.intent;import Android.util.log;import Android.widget.toast;import Com.wenjoy.dojo.ResponseJSON; Import com.wenjoy.multiimagechooser.mainactivity;/** * JS Call java Method * * must inherit Cordovaplugin Cordovaplugin there is a way to implement cordovaactivity * provide Startactivityforresult (); * * I use Cordova 3.3.0 version * * @author Xueqi * */public class Plugin_intent extends Cordovaplugin {private String infos;/ * * Note that the construction method cannot be * * Plugin_intent () {} * * can not be written or defined as follows * */public plugin_intent () {}callbackcontext callbackcontext; @Ov Erridepublic Boolean execute (String action, Org.json.JSONArray Args,callbackcontext Callbackcontext) throws org.json.JSONException {this.callbackcontext = Callbackcontext; LOG.I ("123", action), if (Action.equals ("intent")) {//Gets the first parameter of the args passed by JS infos = args.getstring (0); this.function (); return true;} return false;} Method execution Body private void function () {//cordova.getactivity () Get Current activity's thISLOG.I ("123", cordova.getactivity (). toString ()); Intent Intent = new Intent (cordova.getactivity (), Mainactivity.class); Intent.putextra ("Infos", infos); Cordova.startactivityforresult ((Cordovaplugin) This, intent, 200);} @Overridepublic void Onactivityresult (int requestcode, int resultcode, Intent Intent) {Super.onactivityresult ( Requestcode, ResultCode, intent);//Pass the return value to the JS method Callbackcontext.success (com.alibaba.fastjson.JSONArray.toJSONString (Responsejson.getinstance (). Getjsonobjects ())); if (Responsejson.getinstance (). getjsonobjects ()! = null&& responsejson.getinstance (). GetJsonObjects (). Size () > 0) {toast.maketext (cordova.getactivity (), "Congratulations, upload complete", +). Show ();}}
B. Once the method has been written, register it under Res/xml/config.xml, and write it in the Widget tab to add
<feature name= "Demo" > <param name= "android-package" value= "plugins". Plugin_intent "/><!--Value: Package name. class Name-</feature>
The name of feature is very important, one will be used in JS.
C. write the plugin js file, under Assert/www/plugins, create the Intent.js file
Cordova.define ("Org.apache.cordova.intent", function (require, exports, module) {/* * * Licensed to the Apache software Fo Undation (ASF) under one * or more contributor license agreements. See the NOTICE file * Distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * under the Apache License, Version 2.0 (The * "License"); You are not a use of this file except in compliance * with the License. Obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * unless required by applicable Law or agreed into writing, * software distributed under the License is distributed on A * "As is" BASIS, without Warra Nties or CONDITIONS of any * KIND, either express OR implied. See the License for the * Specific language governing permissions and limitations * under the License. **/var exec = require (' cordova/exec ');/** * Provides access to the vibration mechanism on the device. */module.exports = {/** * Altogether 5 Parameters First: Success will drop second: Failure callback third: The configuration name of the class to be invoked (configured in config. later) Fourth: called Method name (a class may have multiple parties The method is distinguished by this parameter) Fifth: The parameters passed in JSON format */demo:function (Mills) {EXEC (Winparam) {alert (Winpa RAM); <span style= "font-family:arial, Helvetica, Sans-serif;" >//execution succeeds, Winparam is the parameter passed in the class callbackcontext.success </span>}, NULL, "Demo", "intent", [Mills]); },};});Demo: Define the method name that is called by JS
Demo: That's the name of the plug-in class we just configured in CONFIG.
Mills: Here I can always only pass one parameter, so, my solution now is to stitch a string, for example: ' AAA,NNN,CCC ', separated by a comma three parameters
Step3. Using plugins
As of now, the entire plugin is OK, can create an HTML and activiry, here I only list lunch activity writing and HTML page
It's easy to build index.html files under Assert/www.
<! DOCTYPE html>
Viewactivity:Package Com.wenjoy.multiimagechooser;import Org.apache.cordova.cordovaactivity;import android.content.Intent; Import android.os.bundle;/** * Loading HTML page activity * * @author Xueqi * */public class Viewactivity extends Cordovaa ctivity {@Overridepublic void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Super.init () ;//Set by <content src= "index.html"/> in Config.xmlsuper.loadUrl ("file:///android_asset/www/index.html");// Super.loadurl ("file:///android_asset/www/index.html")} @Overrideprotected void Onactivityresult (int requestcode, int resultcode,intent Intent) {super.onactivityresult (Requestcode, ResultCode, Intent);}}
Finally, as for the authority and so on, everyone added themselves.
The Gaocheng! Attached to the demo, to 1 points, we do not spit Groove ~ http://download.csdn.net/detail/xq328220454/7620119