Develop a PhoneGap plug-in called Activity in ten minutes, activityphonegap

Source: Internet
Author: User

Develop a PhoneGap plug-in called Activity in ten minutes, activityphonegap

In the development of HybridApp, we cannot implement many services through HTML5 + js. For example, to call a third-party jar package containing Activity, some functions can be implemented only by using native code, for example, complex UI effects, call communication-related protocol stacks, and third-party payment sdks. 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 an example of the cordova plug-in that calls the Activity and returns the Activity result 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 registered in 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 includes the plug-in configuration file plugin. xml, plug-in description file package. json, platform-related resources and source code, which can include images, xml, java source code, jar packages, and so static libraries. The following is the directory structure of my android pattern lock cordova plug-in.

Plug-in directory structure

It can be seen that this plug-in supports ios and android platforms, including source code and image resources, and is still quite complicated.

We will not be so troublesome today, but simply call 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. exitapp "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. d <span style = "font-family: Arial, Helvetica, sans-serif;"> emoapp </span> <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/d <span style = "font-family: Arial, Helvetica, sans-serif; "> emoapp </span> <span style =" font-family: Arial, Helvetica, sans-serif; ">"/> </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 () {};//ExitAppExitApp.exit = function () {    console.log('----exit');    exec(null, null, "DemoApp", "method", [null, null, null]);};module.exports = ExitApp;


The plug-in java code focuses on the plug-in java code (just an example, not complete) to pass values to the Activity, accept the returned results, and return to js here
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.


In the android phonegap custom plug-in, data is returned to the javascript client in the activity.

You can go to the Internet to find a normal link.
 
How does android phonegap call js on the activity page?

If you are talking about the activity in AndroidManifest. xml, I can't understand how the configuration file can call the js file...
If you need to call it in the background, a simple example is as follows:
Public class Main extends DroidGap
{
Private MyClass mc;

@ Override
Public void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );

Super. init ();

Mc = new MyClass (this, appView );
AppView. addJavascriptInterface (mc, "MyCls ");

Super. loadUrl (getString (R. string. url ));
}
}
 

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.