phonegap android插件,启动activity并返回值
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并返回值的更多相关文章
- 【Android】12.3 在当前Activity中获取另一个Activity的返回值
分类:C#.Android.VS2015: 创建日期:2016-02-23 一.简介 在上一节的示例中,通过StartActivity(Intent)方法启动另一个Activity后,这两个Activ ...
- Android app启动activity并调用onCreate()方法时都默默地干了什么?
Android app启动activity并调用onCreate() 方法时都默默地干了什么? 在AndroidManifest.xml文件中的<intent-filter>元素中有这 ...
- Android开机启动Activity或者Service方法
本文出自 “Bill_Hoo专栏” 博客,请务必保留此出处http://billhoo.blog.51cto.com/2337751/761230 这段时间在做Android的基础开发,现在有一需求是 ...
- Android开机启动Activity或者Service方法(转载)
这段时间在做Android的基础开发,现在有一需求是开机启动,按照网上某些博文教程做了下,始终不成功,一开机总是提示所启动的应用程序意外终止,于是参考了Android SDK doc,终于解决问题,下 ...
- activity 接回返回值
activity 接回返回值 今天做订单列表显示 点击某一项显示订单详细信息,在详细activity中用户可以选择取消订单(未支付的状态下)当用户取消订单后订单列表也要改变状态,原来最初做法是所加载绑 ...
- Android - 和其他APP交互 - 获得activity的返回值
启用另一个activity不一定是单向的.也可以启用另一个activity并且获得返回值.要获得返回值的话,调用startActivityForResult()(而不是startActivity()) ...
- Android课程---Activity 带返回值的跳转
Activity2.java package com.hanqi.test4; import android.content.Intent; import android.os.Bundle; imp ...
- Activity详解三 启动activity并返回结果
首先看演示: 1 简介 .如果想在Activity中得到新打开Activity 关闭后返回的数据,需要使用系统提供的startActivityForResult(Intent intent, int ...
- Android 外部启动activity,自定义action,action常量大全
从任意app,启动另外一个app的activity: 1. Intent i = new Intent(); ComponentName cn = new ComponentN ...
随机推荐
- FireFox 火狐主页被劫持
火狐主页被劫持hao123,流氓 WIN7 ,firefox,任务栏,快速启动,右键 属性 target 应该是 "D:\Program Files (x86)\Mozilla Firefo ...
- J2SE 8的Lambda --- 语法
语法例子 LambdaGrammarTest lambdaTest = new LambdaGrammarTest(); // 1. 能够推导出类型的,可以不写类型 String[] planets ...
- DB2 to mysql
1.安装DB2 create database testdb connect to testdb 2. 将DB2 ixf格式into DB2 IMPORT FROM /tabxxx.IXF OF IX ...
- nginx HttpLuaModule
http://wiki.nginx.org/HttpLuaModule#Directives Name ngx_lua - Embed the power of Lua into Nginx This ...
- Haskell语言学习笔记(24)MonadWriter, Writer, WriterT
MonadWriter 类型类 class (Monoid w, Monad m) => MonadWriter w m | m -> w where writer :: (a,w) -& ...
- 代理URI和服务器URI的不同
[代理URI和服务器URI的不同] 1.向Web服务器直接发送请求时,路径为相对路径(不包含域名). 2.当向代理发送请求时,路径为绝对路径(包含域名). 参考<HTTP权威指南>6.5. ...
- win7卸载打印机驱动
无法删除的话停止Print Spooler服务 删除PRINTERS文件夹下面的文件 C:\Windows\System32\spool\PRINTERS目录下所有的文件,重新启动服务:print s ...
- ios 8 联系人ABPeoplePickerNavigationController
一. ios 联系人ABPeoplePickerNavigationControllerDelegate方法,新添加下面两个联系人选中方法,适配iOS8需要实现 // Called after a p ...
- 836. Rectangle Overlap 矩形重叠
[抄题]: A rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) are the coordinates of i ...
- 9-最短路径(dijkstra)
参考博客:http://www.wutianqi.com/?p=1890 #include <iostream>using namespace std;#define max 1< ...