Android based on cordova3.3 plugin development

Source: Internet
Author: User

Recent work items, need to use Cordova plug-in development, detailed Cordova role, will not go, you can go to Baidu itself OK, direct start. For the detailed process, I have a small demo explaining in advance.

Still just contact, things too theoretical foundation, I am not too clear, or start and rise demo, go to the Power ~ everyone more generous enlighten ~

Step1. Preparatory work:

First I introduced the Helloworld-cordovalib in my demo instance package into the workspace, and I was using Eclipse. Then create the Projectmultiimagechooser, and at the same time introduce helloworld-cordovalib as the library into the Multiimagechooser:

Next, follow the folder structure in the demo instance package. Documents required to introduce Cordova. After the completion of the folder structure such as the following see:

In the Res directory, another XML directory, remember to copy the past Oh ~

As of now, the main preparatory work is finished.


Step2. Plug-in development

The plugin was written in order for JS to invoke my activity, which is actually relatively simple to write.

A. Create the package plugins under the SRC folder 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 the Cordova 3.3.0 version Number * * @author Xueqi * */public class Plugin_intent extends Cordovaplugin {private String infos; /** * Note that the construction method cannot be a * * Plugin_intent () {} * * can not be written or defined as the following * */public plugin_intent () {}callbackcontext callbackcontext; @Overridepublic 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")) {//Get the first parameter of the JS passed args infos = args.getstring (0); this.function (); return true;} return false;} Method run body private void function () {//cordova.getactivity () Get Current ActivityTHISLOG.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. After the method has been written. To register in Res/xml/config.xml, write in the Widget tab

<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 the second: Failure callback third: The configuration name of the class to be invoked (configured in CONFIG. below) Fourth: Called Method name (there may be multiple parties in a class The method is distinguished by this number fifth: the number of passed parameters in JSON format */demo:function (Mills) {EXEC (Winparam) {alert (Winpa RAM); <span style= "font-family:arial, Helvetica, Sans-serif;" >//run successfully.    Winparam is the Callbackcontext.success pass in the class </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: I'm just going to be able to pass a number of references here, so. My solution now is to stitch a string, for example: ' AAA,NNN,CCC ', to cut three parameters with a comma

Step3. Using plugins

As of now, the entire plugin is OK, able to create an HTML and Activiry. Here I just list lunch activity's authoring and HTML pages

Build index.html file under Assert/www, very easy

<! 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 joins in.


The Gaocheng! Attached to the demo, to 1 points, we do not spit Groove ~ http://download.csdn.net/detail/xq328220454/7620119


Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Android based on cordova3.3 plugin development

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.