1. Your execute menthod is not quite right. When you do:
  2. return new PluginResult(PluginResult.Status.OK,resultFunction);
  3.  
  4. that effectively returns nothing as a result. Instead you need to do:
  5.  
  6. PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT);
  7. r.setKeepCallback(true);
  8. return r;
  9.  
  10. which will tell the JS layer that there is info coming soon. Then in
  11. your onActivityResult method you need to call:
  12.  
  13. this.success(new PluginResult(PluginResult.Status.OK, msg), this.callbackId);
  14.  
  15. and you should get your result correctly.
  16.  
  17. Check out the:

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

  1. Hi,
  2. i'm trying to pass a value from my Android java plugin to javascript in index.html.
  3. This is my code :
  4.  
  5. PluginWrapper.js
  6. var PluginWrapper = function() {
  7.  
  8. };
  9.  
  10. PluginWrapper.prototype.crop = function (name, win, fail){
  11.  
  12. console.log("Prima di execute!");
  13.  
  14. return PhoneGap.exec(win,fail,"PluginWrapper","crop",[name]);
  15.  
  16. };
  17.  
  18. PhoneGap.addConstructor(function() {
  19.  
  20. PhoneGap.addPlugin('PluginWrapper',new PluginWrapper());
  21.  
  22. });
  23.  
  24. PluginWrapper.java
  25.  
  26. package it.Prova;
  27.  
  28. import org.json.JSONArray;
  29.  
  30. import com.phonegap.api.Plugin;
  31.  
  32. import com.phonegap.api.PluginResult;
  33.  
  34. import android.content.Intent;
  35.  
  36. import android.util.Log;
  37.  
  38. public class PluginWrapper extends Plugin{
  39.  
  40. private String resultFunction = null;
  41.  
  42. @Override
  43.  
  44. public PluginResult execute(String arg0, JSONArray arg1, String arg2) {
  45.  
  46. Log.v("PLUGIN","PLUGIN EXECUTE");
  47.  
  48. Start("");
  49.  
  50. return new PluginResult(PluginResult.Status.OK,resultFunction);
  51.  
  52. }
  53.  
  54. public String Start(String name){
  55.  
  56. Log.v("START","START");
  57.  
  58. Intent intent = new Intent(this.ctx,PluginActivity.class);
  59.  
  60. this.ctx.startActivityForResult((Plugin) this, intent, );
  61.  
  62. return "OK";
  63.  
  64. }
  65.  
  66. public void onActivityResult(int requestCode, int resultCode, Intent intent) {
  67.  
  68. super.onActivityResult(requestCode, resultCode, intent);
  69.  
  70. if (requestCode == ){
  71.  
  72. String msg = intent.getStringExtra("returnedData");
  73.  
  74. Log.v("FLAG","IN WRAPPER " + msg);
  75.  
  76. resultFunction = msg;
  77.  
  78. }
  79.  
  80. }
  81.  
  82. }
  83.  
  84. PluginActivity.java
  85.  
  86. package it.Prova;
  87.  
  88. import android.app.Activity;
  89.  
  90. import android.content.Context;
  91.  
  92. import android.content.Intent;
  93.  
  94. import android.os.Bundle;
  95.  
  96. import android.widget.Button;
  97.  
  98. import android.util.Log;
  99.  
  100. import android.view.View;
  101.  
  102. import android.view.View.OnClickListener;
  103.  
  104. public class PluginActivity extends Activity{
  105.  
  106. private Button btn;
  107.  
  108. private int flag = ;
  109.  
  110. private Intent intentNew = null;
  111.  
  112. private Context context = this;
  113.  
  114. @Override
  115.  
  116. public void onCreate(Bundle savedInstanceState) {
  117.  
  118. super.onCreate(savedInstanceState);
  119.  
  120. setContentView(R.layout.main);
  121.  
  122. intentNew = this.getIntent();
  123.  
  124. btn = (Button) findViewById(R.id.button1);
  125.  
  126. btn.setOnClickListener(new OnClickListener(){
  127.  
  128. public void onClick(View v) {
  129.  
  130. flag = ;
  131.  
  132. Log.v("FLAG","1 IN PLUGIN");
  133.  
  134. intentNew.putExtra("returnedData", Integer.toString(flag));
  135.  
  136. if (getParent() == null) {
  137. setResult(RESULT_OK, intentNew);
  138. }
  139. else {
  140. getParent().setResult(RESULT_OK, intentNew);
  141. }
  142. finish();
  143. }
  144. });
  145.  
  146. }
  147.  
  148. }
  149.  
  150. index.html
  151.  
  152. <!DOCTYPE HTML>
  153.  
  154. <html>
  155.  
  156. <head>
  157.  
  158. <title>PhoneGap</title>
  159.  
  160. <script type="text/javascript" charset="utf-8" src="phonegap-1.3.0.js"></script>
  161.  
  162. <script type="text/javascript" charset="utf-8" src="PluginWrapper.js"></script>
  163.  
  164. <script type="text/javascript" charset="utf-8">
  165.  
  166. function onLoad(){
  167.  
  168. document.addEventListener("deviceready", onDeviceReady, true);
  169.  
  170. }
  171.  
  172. function onDeviceReady() {
  173.  
  174. //alert("OK");
  175.  
  176. }
  177.  
  178. function getStart() {
  179.  
  180. //window.plugins.PluginWrapper.crop("",function(r){console.log("Value in javascript " + r);},
  181.  
  182. // function(e){console.log("NO");}
  183.  
  184. // );
  185.  
  186. var a = "vuoto";
  187.  
  188. a = window.plugins.PluginWrapper.crop("");
  189.  
  190. console.log("a = " + a);
  191.  
  192. }
  193.  
  194. </script>
  195.  
  196. </head>
  197.  
  198. <body onload="onLoad()">
  199.  
  200. <h1>Hello World</h1>
  201.  
  202. <button onclick="getStart();">Start</button> <br>
  203.  
  204. </body>
  205.  
  206. </html>
  207.  
  208. Attach the source code!
  209.  
  210. Best regards,
  211.  
  212. akus85

继承CordovaPluging的代码,Pluging继承CordovaPluging

  1. public class WXPayPlugin extends CordovaPlugin {
  2.  
  3. public static final String ACTION = "call";
  4. private CallbackContext _callbackContext=null;
  5.  
  6. @Override
  7. public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
  8. if (action.equals(ACTION)) {
  9. try {
  10. //下面两句最关键,利用intent启动新的Activity
  11. Intent intent = new Intent().setClass(cordova.getActivity(), Class.forName(args.getString()));
  12. this.cordova.startActivityForResult(this, intent, );
  13.  
  14. _callbackContext=callbackContext;
  15. //下面三句为cordova插件回调页面的逻辑代码
  16. PluginResult mPlugin = new PluginResult(PluginResult.Status.NO_RESULT);
  17. mPlugin.setKeepCallback(true);
  18. callbackContext.sendPluginResult(mPlugin);
  19.  
  20. } catch (Exception e) {
  21. e.printStackTrace();
  22. return false;
  23. }
  24. }
  25.  
  26. return true;
  27. }
  28. //onActivityResult为第二个Activity执行完后的回调接收方法
  29. @Override
  30. public void onActivityResult(int requestCode, int resultCode, Intent intent){
  31. switch (resultCode) { //resultCode为回传的标记,我在第二个Activity中回传的是RESULT_OK
  32. case Activity.RESULT_OK:
  33. Bundle b=intent.getExtras(); //data为第二个Activity中回传的Intent
  34. String str=b.getString("change01");//str即为回传的值
  35. _callbackContext.success(str);
  36. break;
  37. default:
  38. _callbackContext.error("fail");
  39. break;
  40. }
  41.  
  42. }
  43.  
  44. }

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. VBA 编写类

    一.初识类 现在,请打开你的VBE,主菜单-插入-类模块. 插入了一个类模块,也就建立了一个类.类模块的名字就是类的名字.你现在看到的,她的名字叫“类1”,这是VBA按她姐妹排行给她取的的,是的,VB ...

  2. VBA 判断单元格是否为公式,可用于数组

    Function ISFORMULA(ByVal rg As Object) As Variant      Dim temp As Variant      Dim i As Integer, j ...

  3. ABAP-SET UPDATE TASK LOCAL

    SET UPDATE TASK LOCAL 影响 切换本地更新任务.这意味着当您指定  CALL FUNCTION ... IN UPDATE TASK时,更新数据不会存储在数据库中,而是存储在ABA ...

  4. 用R包来下载sra数据

    1)介绍 我们用SRAdb library来对SRA数据进行处理. SRAdb 可以更方便更快的接入  metadata associated with submission, 包括study, sa ...

  5. Java8 Stream语法详解 2

    1. Stream初体验 我们先来看看Java里面是怎么定义Stream的: A sequence of elements supporting sequential and parallel agg ...

  6. 用NBU无法还原数据库到ASM磁盘

    描述:用NBU无法还原数据库到ASM磁盘,却可以还原到数据库本地磁盘 错误提示: ORA-15025: could not open disk "/dev/mapper/DATA1" ...

  7. Mysql两个time类型计算时间相减

    round((UNIX_TIMESTAMP(finishtime)-UNIX_TIMESTAMP(starttime))/60) 得到的时间是分钟数

  8. sql批量修改字段内容的语句-SQL技巧

    --update '表名' set 要修改字段名 = replace (要修改字段名,'被替换的特定字符','替换成的字符')--update tRecord set columnName = rep ...

  9. iOS - OC - 打印信息 - xcode 中文打印

    #import <Foundation/Foundation.h> @implementation NSDictionary (Log) //重写系统的方法控制输出 -(NSString ...

  10. handler------post传送方式

    package com.qianfeng.gp08_day26_hanlder2; import android.os.Bundle; import android.os.Handler; impor ...