Develop a phonegap plug-in that calls the activity in ten minutes

Source: Internet
Author: User

In the development of hybridapp, we cannot implement many services through HTML5 + Js. For example, to call third-party jar packages including activity, some functions that must be implemented using native code are required, for example, the complex UI effects, call communication-related protocol stacks, third-party payment sdks, and so on. In this case, we need to write the Cordova plug-in and wrap the android native code as the plug-in for upper-layer JavaScript calls. This article describes a Cordova plug-in sample that calls the activity and returns the result of the activity to help you understand the development of Cordova plugin.

The Cordova plug-in is added through Cordova plugin add (the plug-in package name must be officially added to the Cordova plugins or the GIT address where the plug-in code is stored) and deleted by the Cordova plugin RM plug-in package name. A main plug-in file contains the plug-in configuration file plugin. XML, plug-in description file package. JSON, platform-related resources and source code, which can contain images, XML, Java source code, Jar packages, and so static libraries. The following is the folder structure of my android pattern lock Cordova plug-in.

Plug-in folder structure

The plug-in supports iOS and Android platforms, including source code and image resources.

What we introduced today will not be so troublesome. It is just a simple call to the activity. Let's look at config. xml first.

Configuration file config. XML Code

<? XML version = "1.0" encoding = "UTF-8"?> <Plugin xmlns = "http://apache.org/cordova/ns/plugins/1.0" id = "com. qianmi. cordova. demoapp "version =" 0.0.3 "> <Name> demoplugin </Name> <description> qianmi <span style =" font-family: Arial, Helvetica, sans-serif; "> demoplugin </span> <span style =" font-family: Arial, Helvetica, sans-serif; "> plugin </description> </span> <license> Apache 2.0 </license> <keywords> qianmi, <span style =" font-family: Arial, Helvetica, Sans-serif; "> demoplugin </span> <span style =" font-family: Arial, Helvetica, sans-serif; "> </keywords> </span> <js-module src =" www/demo. JS "name =" Demo "> <clobbers target =" Cordova. plugins. demo "/> <! -- Name called in JS --> </JS-module> <! -- Android --> <platform name = "android"> <config-file target = "Res/XML/config. XML "parent ="/* "> <feature name =" demoapp "> <Param name =" Android-package "value =" com. qianmi. cordova. demoapp <span style = "font-family: Arial, Helvetica, sans-serif; ">"/> </span> </feature> </config-File> <source-file src = "src/Android/demoapp. java "target-Dir =" src/COM/qianmi/Cordova/demoapp/> </span> </platform> </plugin>

Code for the plug-in WWW/JS

var argscheck = require('cordova/argscheck'),    utils = require('cordova/utils'),    exec = require('cordova/exec');var DemoApp = function () {};//ExitApp<pre name="code" class="javascript">DemoApp<span style="font-family: Arial, Helvetica, sans-serif;">.exit = function () {</span>
Console. log ('---- exit'); Exec (null, null, "demoapp", "demoaction", [null, null, null]);}; module. exports = demoapp;
 


The key plug-in Java code is the plug-in Java code (only demo example, incomplete). It is here to pass values to the activity, accept the returned results, and return the results to Js.
public class LockPattern extends CordovaPlugin {private static final String TAG = "LockPattern";private static final String NICK_NAME = "nickName";private static final String MODE = "mode";public static final int REQUEST_CODE_SET_LOCK_PATTERN = 10001;public static final int REQUEST_CODE_VERIFY_LOCK_PATTERN = 10002; private CallbackContext mCallbackContext;@Overridepublic boolean execute(String action, JSONArray args,CallbackContext callbackContext) throws JSONException {Log.i(TAG, action + "   " + args);mCallbackContext = callbackContext;Intent intent = new Intent().setClass(cordova.getActivity(),LockPatternActivity.class);intent.putExtra(MODE, LockPatternActivity.MODE_STEP_1);intent.putExtra(NICK_NAME, nickName);this.cordova.startActivityForResult(this, intent,REQUEST_CODE_SET_LOCK_PATTERN);return false;}@Overridepublic void onActivityResult(int requestCode, int resultCode, Intent data) {Log.i(TAG, "---onActivityResult:" + requestCode + "   " + resultCode);switch (requestCode) {case REQUEST_CODE_SET_LOCK_PATTERN:if (Activity.RESULT_OK ==  resultCode && null != mCallbackContext) {mCallbackContext.success(LockPatternUtils.loadFromPreferences(cordova.getActivity()));}break;}}}

A Cordova plug-in is so simple that you can easily call activity from Js.

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.