Your execute menthod is not quite right. When you do:
return new PluginResult(PluginResult.Status.OK,resultFunction); that effectively returns nothing as a result. Instead you need to do: PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT);
r.setKeepCallback(true);
return r; which will tell the JS layer that there is info coming soon. Then in
your onActivityResult method you need to call: this.success(new PluginResult(PluginResult.Status.OK, msg), this.callbackId); and you should get your result correctly. Check out the:

有问题的代码,参考上面修改

Hi,
i'm trying to pass a value from my Android java plugin to javascript in index.html.
This is my code : PluginWrapper.js
var PluginWrapper = function() { }; PluginWrapper.prototype.crop = function (name, win, fail){ console.log("Prima di execute!"); return PhoneGap.exec(win,fail,"PluginWrapper","crop",[name]); }; PhoneGap.addConstructor(function() { PhoneGap.addPlugin('PluginWrapper',new PluginWrapper()); }); PluginWrapper.java package 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 class PluginWrapper extends Plugin{ private String resultFunction = null; @Override public PluginResult execute(String arg0, JSONArray arg1, String arg2) { Log.v("PLUGIN","PLUGIN EXECUTE"); Start(""); return new PluginResult(PluginResult.Status.OK,resultFunction); } public String Start(String name){ Log.v("START","START"); Intent intent = new Intent(this.ctx,PluginActivity.class); this.ctx.startActivityForResult((Plugin) this, intent, ); return "OK"; } public void onActivityResult(int requestCode, int resultCode, Intent intent) { super.onActivityResult(requestCode, resultCode, intent); if (requestCode == ){ String msg = intent.getStringExtra("returnedData"); Log.v("FLAG","IN WRAPPER " + msg); resultFunction = msg; } } } PluginActivity.java package 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 class PluginActivity extends Activity{ private Button btn; private int flag = ; private Intent intentNew = null; private Context context = this; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); intentNew = this.getIntent(); btn = (Button) findViewById(R.id.button1); btn.setOnClickListener(new OnClickListener(){ public void onClick(View v) { flag = ; 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> <html> <head> <title>PhoneGap</title> <script type="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");} // ); var a = "vuoto"; a = window.plugins.PluginWrapper.crop(""); console.log("a = " + a); } </script> </head> <body onload="onLoad()"> <h1>Hello World</h1> <button onclick="getStart();">Start</button> <br> </body> </html> Attach the source code! Best regards, akus85

继承CordovaPluging的代码,Pluging继承CordovaPluging

public class WXPayPlugin extends CordovaPlugin {

    public static final String ACTION = "call";
private CallbackContext _callbackContext=null; @Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
if (action.equals(ACTION)) {
try {
//下面两句最关键,利用intent启动新的Activity
Intent intent = new Intent().setClass(cordova.getActivity(), Class.forName(args.getString()));
this.cordova.startActivityForResult(this, intent, ); _callbackContext=callbackContext;
//下面三句为cordova插件回调页面的逻辑代码
PluginResult mPlugin = new PluginResult(PluginResult.Status.NO_RESULT);
mPlugin.setKeepCallback(true);
callbackContext.sendPluginResult(mPlugin); } catch (Exception e) {
e.printStackTrace();
return false;
}
} return true;
}
//onActivityResult为第二个Activity执行完后的回调接收方法
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent){
switch (resultCode) { //resultCode为回传的标记,我在第二个Activity中回传的是RESULT_OK
case Activity.RESULT_OK:
Bundle b=intent.getExtras(); //data为第二个Activity中回传的Intent
String str=b.getString("change01");//str即为回传的值
_callbackContext.success(str);
break;
default:
_callbackContext.error("fail");
break;
} } }

phonegap android插件,启动activity并返回值的更多相关文章

  1. 【Android】12.3 在当前Activity中获取另一个Activity的返回值

    分类:C#.Android.VS2015: 创建日期:2016-02-23 一.简介 在上一节的示例中,通过StartActivity(Intent)方法启动另一个Activity后,这两个Activ ...

  2. Android app启动activity并调用onCreate()方法时都默默地干了什么?

    Android app启动activity并调用onCreate() 方法时都默默地干了什么?   在AndroidManifest.xml文件中的<intent-filter>元素中有这 ...

  3. Android开机启动Activity或者Service方法

    本文出自 “Bill_Hoo专栏” 博客,请务必保留此出处http://billhoo.blog.51cto.com/2337751/761230 这段时间在做Android的基础开发,现在有一需求是 ...

  4. Android开机启动Activity或者Service方法(转载)

    这段时间在做Android的基础开发,现在有一需求是开机启动,按照网上某些博文教程做了下,始终不成功,一开机总是提示所启动的应用程序意外终止,于是参考了Android SDK doc,终于解决问题,下 ...

  5. activity 接回返回值

    activity 接回返回值 今天做订单列表显示 点击某一项显示订单详细信息,在详细activity中用户可以选择取消订单(未支付的状态下)当用户取消订单后订单列表也要改变状态,原来最初做法是所加载绑 ...

  6. Android - 和其他APP交互 - 获得activity的返回值

    启用另一个activity不一定是单向的.也可以启用另一个activity并且获得返回值.要获得返回值的话,调用startActivityForResult()(而不是startActivity()) ...

  7. Android课程---Activity 带返回值的跳转

    Activity2.java package com.hanqi.test4; import android.content.Intent; import android.os.Bundle; imp ...

  8. Activity详解三 启动activity并返回结果

    首先看演示: 1 简介 .如果想在Activity中得到新打开Activity 关闭后返回的数据,需要使用系统提供的startActivityForResult(Intent intent, int ...

  9. Android 外部启动activity,自定义action,action常量大全

    从任意app,启动另外一个app的activity: 1.   Intent i = new Intent();           ComponentName cn = new ComponentN ...

随机推荐

  1. StringUtil字符串工具类

    package com.zjx.test03; /** * 字符串工具类 * @author * */ public class StringUtil { /** * 判断是否是空 * @param ...

  2. Spring Boot中启动HTTPS

    一,生成https 的证书 1,在相应的根目录下 keytool -genkey -alias tomcat -storetype PKCS12 -keyalg RSA -keysize -keyst ...

  3. 抽象类(abstract class)

    package com.bjsxt.oop.abstractClass; //抽象类 public abstract class Animal { //因为父类的方法总是被重写 那就没写的必要了 但是 ...

  4. javascript 函数对象

    http://hi.baidu.com/gdancer/blog/item/a59e2c12479b4e54f919b814.html jQuery的一些写法就是基于这篇文章的原理的..     函数 ...

  5. js中常见的内置对象

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  6. CTR点击率预估干货分享

    CTR点击率预估干货分享 http://blog.csdn.net/bitcarmanlee/article/details/52138713

  7. new operator

    [new operator] When the code new Foo(...) is executed, the following things happen: A new object is ...

  8. CentOS 6.3安装配置supervisor进程管理工具

    1. Supervisor是一个C/S系统,它可以在类unix操作系统让用户来监视和控制后台服务进程的数量,一个很重要的功能就是监控服务器的主要后台进程,并在出现问题是自动重启. 2. 根据服务器上的 ...

  9. 利用 AWK 的数值计算功能提升工作效率(转载)

    Awk 是一种优秀的文本样式扫描和处理工具.转文侧重介绍了 awk 在数值计算方面的运用,并通过几个实际工作中的例子,阐述了如何利用 awk 的计算功能来提高我们的工作效率.转文源自IBM Bluem ...

  10. forward(请求转发)和redirect(重定向)的区别

    原理: forward是服务器请求资源,服务器直接访问目标地址的URL,把那个URL的响应内容读取过来,然后把这些内容再发给浏览器,浏览器根本不知道服务器发送的内容是从哪儿来的,所以它的地址栏中还是原 ...