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 ...
随机推荐
- Delphi RAD Berlin OutputDebugString 输出调试信息
Delphi RAD Berlin Event Log.OutputDebugString 输出调试信息,仅在win VCL下可以用.OutputDebugString(PChar('hellowor ...
- Swift 4 新特性
多行字符串 /// 多行字符串用三引号括起来 let quotation = """ The White Rabbit put on his spectacles. &q ...
- scikit Flow ,tensor flow 做ml模型
[https://github.com/ilblackdragon/tf_examples/blob/master/titanic.py] [keras 高层tensorflow] https://k ...
- 大型运输行业实战_day07_1_订单查看实现
1.业务分析 每个在窗口售票的售票员都应该可以随时查看自己的售票信息 简单的界面入口如图所示: 对应的html代码: <button onclick="orderDetail()&qu ...
- ORDER BY 子句在视 图、内联函数、派生表、子查询和公用表表达式中无效
SQL语句: select * from (select distinct t2.issue,cashmoney from (select distinct issue from lot_gamepa ...
- Honeycomb
Honeycomb http://codeforces.com/gym/102028/problem/F time limit per test 4.0 s memory limit per test ...
- Python错误:close failed in file object destructor
我遇到的情况: 二进制程序调shell再调Python后,shell退出,Python进程挂到init上(不是僵尸进程),但 此时二进制程序未退出,这时候中断而二进制程序出现此提示. 经查询: 应该是 ...
- windows系统如何真正隐藏文件夹[转载]
方法一(推荐)eg:现需隐藏e盘bak目录下的tools文件夹e:\bak\tools运行:cmd键入:attrib +s +a +h +r e:\bak\tools然后,你再进去看e盘bak目录下, ...
- discuz回贴通知插件实现-用户状态设置
1.获取用户提交数据 discuz通过$_GET来获取全部数据,包括($_GET,$_POST). else if($_GET['pluginop'] == 'set') { //获取用户提交数据 $ ...
- vue2.0 tab切换几种方式
第一种 比较灵活简单的方式(切换改变部分的内容在组件中比较方便操作) <template> <div id="app"> <ul> <li ...