Cordova Invoke Activity Plugin development under Android platform

Source: Internet
Author: User

First, set up a package with the name packages com. jiajiacy.callactivity;

 Packagecom. jajacy.callactivity;ImportOrg.apache.cordova.CallbackContext;ImportOrg.apache.cordova.CordovaPlugin;ImportOrg.apache.cordova.PluginResult;ImportOrg.json.JSONArray;Importorg.json.JSONException;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.os.Bundle; Public classCallactivitypluginextendsCordovaplugin { Public Static FinalString ACTION = "Call"; @Override Public BooleanExecute (String action, Jsonarray args, Callbackcontext callbackcontext)throwsjsonexception {if(Action.equals (action)) {Try {             //The following two sentences are the most critical, using intent to start a new activityIntent Intent =NewIntent (). SetClass (Cordova.getactivity (), Class.forName (args.getstring (0)));  This. Cordova.startactivityforresult ( This, Intent, 1); //The following three sentences are the logical code for the Cordova plug-in callback pagePluginresult Mplugin =NewPluginresult (PluginResult.Status.NO_RESULT); Mplugin.setkeepcallback (true);            Callbackcontext.sendpluginresult (Mplugin); Callbackcontext.success ("Success"); } Catch(Exception e) {e.printstacktrace (); return false; }        }        return true; }        /*** Onactivityresult for the second activity after the execution of the callback receive method **/@Override Public voidOnactivityresult (intRequestcode,intResultCode, Intent Intent) {         Switch(ResultCode) {//ResultCode is the sign of the callback, and I return it in the second activity is RESULT_OK             CaseActivity.RESULT_OK:Bundle b=intent.getextras ();//data is the intent of the second activityString str=b.getstring ("change01");//STR is the value of the callback             Break; default:             Break; }    }}

Add in Res\xml\config.xml:

<name= "callactivity">        <name = "Android-package" value = "com. JajaCy.CallActivity.CallActivityPlugin "/>    </feature> 

In assets\www\plugins\com. Build a callactivity.js in the jajacy.callactivity directory

Cordova.define (' com. JajaCy.CallActivity.CallActivityPlugin ',function(Require, exports, module) {varExec=require (' Cordova/exec '); Module.exports={        /** Param:data*/Activityplugin:function(data) {/** Altogether five parameters First: Success will drop the second: Failure callback third: The configuration name of the class to be invoked (configure in config. Configuration later explained below) Fourth: Called Method name (there may be more than one method in a class to differentiate by this parameter) Fifth: passed parameters in JSON format*/exec (Nativepluginresulthandler,nativepluginerrorhandler,"Callactivity", "call", [data])} }}); /** method to invoke when successful*/functionNativepluginresulthandler (Result) {alert ("SUCCESS: \ r \ n" +result); } /** methods to be called when failed*/  functionNativepluginerrorhandler (Error) {alert ("ERROR: \ r \ n" +error); }

In the Cordova_plugins.js file, configure the following:

{        "file": "Plugins/com". Jajacy.callactivity/callactivity.js ",        " id ":" com. JajaCy.CallActivity.CallActivityPlugin ",        " clobbers ": [            " Window.call "         ]    }
// TOP of METADATA {    "com. Jajacy.callactivity ":" 0.0.1 "}

To use when calling:

Window.call.activityPlugin ("com. JajaCy.CallActivity.DownloadActivity; ");

Finally, you need to configure the activity in Androidmanifest.xml:

<android:name= "downloadactivity"></activity  >

This allows you to complete the Cordova call activity

Cordova Invoke Activity Plugin development under Android platform

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.