开发环境: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>完!
相关推荐
■ 用PhoneGap和它的NFC插件写Android上的NFC应用程序用例 ■ 深入了解NDEF结构:测试已有的写标签程序,然后写自己的标签程序 ■ 用PhoneGap事件监听器来监听和过滤NDEF消息 ■ 写一个完整的Android应用程序来控制...
android与phonegap混合开发之相互跳转。一个小例子很好示范怎么相互跳转,之间可相互传参数。代码精简易懂,容易上手,下载下来,导入即可运行。
资源名称:NFC Arduino、Android与PhoneGap近场通信内容简介:《NFC:Arduino、Android与PhoneGap近场通信》将带你走入近场通信(NFC)的世界,这是一种快速发展的技术,它能够让设备近距离交换数据或使用无线电信号...
通过自定义plugin插件实现了javascript与Android的交互。
android+Phonegap开发环境搭建android+Phonegap开发环境搭建android+Phonegap开发环境搭建android+Phonegap开发环境搭建
NFC Arduino Android与PhoneGap近场通信
android之PhoneGap入门实例http://blog.csdn.net/beifengdelei/article/details/7702461
该源码是使用phonegap加jquery mobile开发基于html5 css3的应用的项目例子。可以很好的来学校jquery mobile。
websocket-android-phonegap
phonegap开发中JS与Android之间的交互
Android和PhoneGap使用举例,包括相机、打包本地网站和在线网站等。
websocket-android-phonegap-master
oPath的Android 2.1 PhoneGap 2.0.0 移植版本。 移植过程中遇到错误: TypeError: Result of expression "navigator.camera" is not an object 答案: 有可能是 PhoneGap 没有被正确装载或者完全装载你就调用了。 ...
一个Android原生activity与phonegap交互的例子, java调用js,js调用java 工程中是一个嵌套了phonegap的activity ,外层是android的activity,内层framelayout中是phonegap,完成了交互的简单例子
android上使用phonegap的hellworld例子,简单
PhoneGap Android is an Android application library that allows for PhoneGap-based projects to be built for the Android Platform. PhoneGap-based applications are, at the core, an application written in...
android_phonegap-0.9.5.1jar 包, Android以后的发展趋势
Android for PhoneGap 程式配置 不是想要币的, 没办法,我下载也需要,被逼无奈,文档挺好的,请放心下载。我已经看过了。
混合开发模式-利用HTML5开发无线应用_Android-PhoneGap安装及调 phonegap 1.5 eclipse 3.4+
phonegap android在线更新插件,兼容android7.0下载后安装闪退的问题;详细使用见README.md文件。