android与phonegap的相互交互
开发环境:android SDK+android2.3或以上的真机
开发所需:cordova-2.1.0.js+sencha-touch-all-debug.js
首先把你的phonegap或sehcha项目放到assets文件夹下,然后在你的入口函数(onCreate)里添加如下:
super.init();
super.setBooleanProperty("loadInWebView", true);
super.setIntegerProperty("loadUrlTimeoutValue", 60000); //在模拟器测试上使用.不然会出错
//加splashScreen
super.setIntegerProperty("splashscreen", R.drawable.welcome);
this.appView.setBackgroundColor(0);
this.appView.setBackgroundResource(R.drawable.welcome);
super.loadUrl(<a href="file:///android_asset/PagoClient/index.html">file:///android_asset/PagoClient/index.html</a>);
这样就可以在你的机器上运行了.注意,我这里用的是2.1.0的cordova包,只支持android系统2.3/以上的真机!!!!!!!!!!!!!
如果要在phonegap或sencha端调用android端的代码就需要用插件的方式来做:
1,创建一个继承了Plugin的类
public class pluginClass extends Plugin{
public static final String ACTION = "codetest";
public static final String ACTION_INTENT_TEST = "com.terry.broadcast.test";
@Override
public PluginResult execute(String action, JSONArray data, String callbackId) {
// TODO Auto-generated method stub
if(ACTION.equals(action)){
Intent intent = new Intent(ctx.getContext(), CaptureActivity.class);//你想去的activity(exp:Temp)
ctx.startActivity(intent);
}
return null;
}
} 2,在项目的res/xml/config.xml文件加一个plugin
<plugin name="plugintest" value="com.example.senchapago.pluginClass"/>
name是你自字的名字,value是继承了plugin类的所在类的路径
3,在phonegap项目里的cordova-2.1.0.js文件最后添加以下代码:
//to
var testAndroid01API=function(){};
testAndroid01API.prototype.test = function(params, success, fail){
return PhoneGap.exec(
function(args){
success(args);
},
function(args){
fail(args);
},
'plugintest', //java类
'codetest', //action
[params] //params
);
};
PhoneGap.addConstructor(function() {
PhoneGap.addPlugin('testAndroid01API', new testAndroid01API());
});
if(!window.plugins) {
window.plugins = {};
}
if (!window.plugins.testAndroid01API) {
window.plugins.testAndroid01API = new testAndroid01API();
} 4,最后在你需要触发的地方加上:
window.plugins.testAndroid01API.test(null,function(r){},function(e){}); 这样就可以调用android的Activiy类了,
同理.如果你的在android端把数据传给phonegap端,流程大概与上面的步骤想似:
1,2步一样,第3步是:
PhoneGap.addConstructor(function() {
PhoneGap.addPlugin('testAndroid02API', new testAndroid02API());
});//result
var testAndroid02API=function(){};
testAndroid02API.prototype.test = function(success, error, qrcodeData){
return PhoneGap.exec( success, error,
'resulttest', //java类名,plugins.xml中注册的名字
'resultcode', //action,Java方法中用来匹配的字段
[qrcodeData] //params 传递的参数,Array形式
);
}; if(!window.plugins) {
window.plugins = {};
}if (!window.plugins.testAndroid02API) {
window.plugins.testAndroid02API = new testAndroid02API();
}4,是在你需要返回结果的地方添加:
var success = function(data){ //当Java方法返回成功时,通过data.key 获得Java中传来的JSONObject数据
//alert("1111111 : " + data.testData1 + ' and 2222222 : ' + data.testData2);
Ext.getCmp('txt_').setValue(data.qrcodeData);
};
var error = function(e){
//Ext.getCmp('txt_').setValue(e);
};
window.plugins.testAndroid02API.test(success, error, null); 这样就可以把数据传递了,当然是你继承了plugin的类中处理方式也是有不同的,例如:
<p>public class resultClass extends Plugin{</p><p> public static final String ACTION = "resultcode";</p><p> PluginResult result = null;
JSONObject jsonObj = new JSONObject();//可以返回给JS的JSON数据
private SharedPreferences mPref;
@Override
public PluginResult execute(String action, JSONArray data, String callbackId) {
mPref = PreferenceManager.getDefaultSharedPreferences(ctx.getContext());
if(ACTION.equals(action)){
String resultCode = mPref.getString("qr_code_result", null);
while (resultCode==null || resultCode.length()==0) {//循环获取qr_code_result,直到有值
resultCode = mPref.getString("qr_code_result", null);
}
resultCode = mPref.getString("qr_code_result", null);
if(resultCode.length() != 0 ){
try {
jsonObj.put("qrcodeData", resultCode);
mPref.edit().remove("qr_code_result").commit();
} catch (JSONException e) {
e.printStackTrace();
}
System.out.println("有值!");
result = new PluginResult(PluginResult.Status.OK, jsonObj);
//返回成功时,将Java代码处理过的JSON数据返回给JS
}else{
System.out.println("无值!");
result = null;
}
}
return result;
}
}</p>完! 相关推荐
quzishen 2013-07-17
故作坚强 2013-07-15
爱技术爱生活TAO 2013-07-06
临碣秘藏 2013-07-10
czpaex 2011-09-11
益之 2014-01-09
yxwang0 2014-01-09
朱莉的乔夫 2014-09-05
朱莉的乔夫 2015-03-26
朱莉的乔夫 2015-03-13
益之 2015-04-21
临碣秘藏 2015-05-12
quzishen 2016-01-20
爱技术爱生活TAO 2016-01-07
tenda 2012-09-08
临碣秘藏 2012-05-30
老菜鸟自习室 2012-04-07