PhoneGap Android plugin, start activity and return value

Source: Internet
Author: User

Your Execute Menthod isNot quite right. When you Do:    return NewPluginresult (pluginresult.status.ok,resultfunction); That's effectively returns nothing asa result. Instead need to Do: Pluginresult R=NewPluginresult (PluginResult.Status.NO_RESULT); R.setkeepcallback (true); returnR;which'll tell the JS layer that there isInfo coming soon. Theninchyour Onactivityresult method you need to call: This. Success (NewPluginresult (PluginResult.Status.OK, msg), This. Callbackid); and you shouldGetyour result correctly. Check outThe:
View Code

Problematic code, refer to the above modification

Hi,i'm trying to pass a value from my Android Java plugin to JavaScript in index.html.This isMy Code:P luginwrapper.jsvarPluginwrapper =function () {}; PluginWrapper.prototype.crop=function (name, win, fail) {Console.log ("Prima di execute!"); returnPhonegap.exec (Win,fail,"Pluginwrapper","Crop", [name]); }; Phonegap.addconstructor (function () {Phonegap.addplugin ('Pluginwrapper',NewPluginwrapper ());}); Pluginwrapper.javapackage it. Prova;import Org.json.jsonarray;import Com.phonegap.api.plugin;import Com.phonegap.api.pluginresult;import Android.content.intent;import Android.util.Log; Public classPluginwrapper extends plugin{PrivateString resultfunction =NULL; @Override PublicPluginresult Execute (String arg0, Jsonarray arg1, string arg2) {LOG.V ("PLUGIN","PLUGIN EXECUTE"); Start (""); return NewPluginresult (pluginresult.status.ok,resultfunction); }     Publicstring Start (string name) {LOG.V ("START","START"); Intent Intent=NewIntent ( This. ctx,pluginactivity.class);  This. Ctx.startactivityforresult ((Plugin) This, Intent,0); return "OK"; }     Public voidOnactivityresult (intRequestcode,intResultCode, Intent Intent)        {Super.onactivityresult (Requestcode, ResultCode, intent); if(Requestcode = =0) {String msg= Intent.getstringextra ("Returneddata"); LOG.V ("FLAG","In WRAPPER"+msg); Resultfunction=msg; }}}pluginactivity.javapackage it. Prova;import Android.app.activity;import Android.content.context;import Android.content.intent;import Android.os.bundle;import Android.widget.button;import Android.util.log;import Android.view.View;import Android.view.View.OnClickListener; Public classPluginactivity extends activity{PrivateButton btn; Private intFlag =0; PrivateIntent intentnew =NULL; PrivateContext context = This; @Override Public voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);                    Setcontentview (R.layout.main); Intentnew= This. Getintent (); BTN=(Button) Findviewbyid (R.id.button1); Btn.setonclicklistener (NewOnclicklistener () { Public voidOnClick (View v) {flag=1; LOG.V ("FLAG","1 in PLUGIN"); Intentnew.putextra ("Returneddata", integer.tostring (flag)); if(getParent () = =NULL) {Setresult (RESULT_OK, intentnew); }           Else{getParent (). Setresult (RESULT_OK, intentnew);                       } finish ();     }                                    }); }}index.html<! DOCTYPE Html>"Text/javascript"charset="Utf-8"Src="Phonegap-1.3.0.js"></script><script type="Text/javascript"charset="Utf-8"Src="Pluginwrapper.js"></script><script type="Text/javascript"charset="Utf-8">function OnLoad () {Document.addeventlistener ("Deviceready", Ondeviceready,true); } function Ondeviceready () {//alert ("OK"); } function Getstart () {//Window.plugins.PluginWrapper.crop ("", function (r) {Console.log ("Value in JavaScript" + R);},//function (e) {console.log ("NO");}//                    );varA ="Vuoto"; a= Window.plugins.PluginWrapper.crop (""); Console.log ("A ="+a); }     </script>"onLoad ()">"Getstart ();">Start</button> <br></body>Attach The source code!Best Regards,akus85
View Code

Inherit cordovapluging code, pluging inherit cordovapluging

 Public classWxpayplugin extends Cordovaplugin { Public StaticFinal String ACTION ="Pager"; PrivateCallbackcontext _callbackcontext=NULL; @Override PublicBoolean execute (String action, Jsonarray args, Callbackcontext callbackcontext) throws Jsonexception {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); _callbackcontext=Callbackcontext; //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); } Catch(Exception e) {e.printstacktrace (); return false; }          }            return true; }      //Onactivityresult callback receive method after execution of the second activity@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_callbackcontext.success (str);  Break; default: _callbackcontext.error ("fail");  Break; }                 }}
View Code

PhoneGap Android plugin, start activity and return value

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.