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 ...
随机推荐
- Asp.Net MVC参考资料
Every day up!!!!!! 1.无废话MVC入门教程 2.MVC快速入门 3.MVC小牛之路 4.Web API强势入门指南 5.全网最全的mvc汇总 6.MVC5+EF6+Bootstra ...
- ubuntu 16.04 install wine
from: https://wiki.winehq.org/Ubuntu If your system is 64 bit, enable 32 bit architecture (if you ha ...
- 类定义,创建/销毁OC对象
类定义 1 OC类分为2个文件,一个是.h文件,一个是.m文件: 2 .h文件存放类.函数声明: 3 .m文件类的具体实现: 4 类声明使用关键字@interface.@end来声明: 5 类实现使用 ...
- cocos2d-x 3.0 学习笔记: 一个可以拖拽的Label及schedule的应用
#ifndef _DRAGLABEL_H_ #define _DRAGLABEL_H_ #include "cocos2d.h" USING_NS_CC; class DragLa ...
- 基元线程同步构造 AutoResetEvent和ManualResetEvent 线程同步
在.Net多线程编程中,AutoResetEvent和ManualResetEvent这两个类经常用到, 他们的用法很类似,但也有区别.ManualResetEvent和AutoResetEvent都 ...
- js中常用的内部函数的使用
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- 框架中spring有专门的类用于处理乱码
在三大框架spring中有专门的一个过滤器类用于处理乱码问题--->CharacterEncodingFilter 我们只需在web.xml中添加如下几行代码就可解决乱码问题 <filte ...
- hibernate中多对多的注解配置
hibernate多对多的注解配置中的自动生成中间表的配置: @Entity@Table(name="test_student")public class Students { @ ...
- 关于sql 增删改
1.更改数据库的名称 --更改数据库的名称,逗号前面是之前的,后面是改成的名 sp_renamedb student,xuesheng 2.表中有数据的情况下再添加列.删除列 --后来添加列,只能默认 ...
- SpringDataRedis事务 专题
5.10.1. @Transactional SupportTransaction Support is disabled by default and has to be explicitly en ...