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 ...
随机推荐
- mavenLocal默认地址转移
maven的默认本地仓库为 USER_HOME/.m2/ windows开发我们大多不会讲本地仓库放在c盘下,而是重新指定了另一个存储位置. 在gradle中 使用 mavenLocal() 时的查找 ...
- Python library not found: libpython2.7mu.so.1.0
在使用pyinstaller生成python可执行文件的时候,包错误,提示有几个依赖的库找不到:Python library not found: libpython2.7mu.so.1.0 参考st ...
- spring cloud: eureka搭建
1. 添加pom 依赖: <parent> <groupId>org.springframework.boot</groupId> <artifactId&g ...
- caffe openpose/Realtime Multi-Person 2D Pose Estimation using Part Affinity Fields配置(转)
Realtime Multi-Person 2D Pose Estimation using Part Affinity Fields 是CVPR2017的一篇论文,作者称是世界上第一个基于深度学习的 ...
- webserive学习记录6-页面请求webservice
前面都是通过JAVA代码访问webservice服务,下面将介绍通过javascript,jquery访问webservice服务并介绍过过servlet解决跨域问题的方法. 服务端 编写服务代码,解 ...
- Nginx安装部署以及配置文件解析
Nginx 中的 Location 指令 是NginxHttpCoreModule中重要指令.Location 指令,是用来为匹配的 URI 进行配置,URI 即语法中的”/uri/”,可以是字符串或 ...
- iKcamp|基于Koa2搭建Node.js实战(含视频)☞ 处理静态资源
视频地址:https://www.cctalk.com/v/15114923882788 处理静态资源 无非花开花落,静静. 指定静态资源目录 这里我们使用第三方中间件: koa-static 安装并 ...
- javascript中this之说
this是在运行时基于函数的执行环境绑定的:在全局函数中,this等于window,而当函数被作为某个对象的方法调用时,this等于那个对象.不过,匿名函数的执行环境具有全局性,因此其this对象通常 ...
- MapReduce超时原因(Time out after 300 secs)
目前碰到过三种原因导致 Time out after 300 secs. 1. 死循环 这是最常见的原因.显式的死循环很容易定位,隐式的死循环就比较麻烦了,比如正则表达式.曾经用一个网上抄来的邮箱正则 ...
- EnumMap实现类
从名字上看出来,EnumMap是为枚举类服务的,它的key不能为null,在创建它的时候,必须要指定一个枚举类,如: EnumMap enumMap = new EnumMap(Season.clas ...