0
点赞
收藏
分享

微信扫一扫

android与phonegap的相互交互


开发环境:android SDK+android2.3或以上的真机



开发所需:cordova-2.1.0.js+sencha-touch-all-debug.js

 

首先把你的phonegap或sehcha项目放到assets文件夹下,然后在你的入口函数(onCreate)里添加如下:


1. super.init();  
2. super.setBooleanProperty("loadInWebView", true);   
3. super.setIntegerProperty("loadUrlTimeoutValue", 60000); //在模拟器测试上使用.不然会出错  
4. //加splashScreen  
5. super.setIntegerProperty("splashscreen", R.drawable.welcome);  
6. this.appView.setBackgroundColor(0);  
7. this.appView.setBackgroundResource(R.drawable.welcome);  
8.   
9. 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的类




1. public class pluginClass extends Plugin{  
2.   
3.     public static final String ACTION = "codetest";  
4.     public static final String ACTION_INTENT_TEST = "com.terry.broadcast.test";  
5.   
6.     @Override  
7.     public PluginResult execute(String action, JSONArray data, String callbackId) {  
8.         // TODO Auto-generated method stub  
9.         if(ACTION.equals(action)){  
10.             Intent intent = new Intent(ctx.getContext(), CaptureActivity.class);//你想去的activity(exp:Temp)  
11.        ctx.startActivity(intent);  
12.         }  
13.         return null;  
14.     }  
15.       
16. }


2,在项目的res/xml/config.xml文件加一个plugin

          <plugin name="plugintest" value="com.example.senchapago.pluginClass"/>

name是你自字的名字,value是继承了plugin类的所在类的路径

 

3,在phonegap项目里的cordova-2.1.0.js文件最后添加以下代码:



 


1. //to  
2. var testAndroid01API=function(){};      
3.   
4. testAndroid01API.prototype.test = function(params, success, fail){  
5.     return PhoneGap.exec(  
6.                 function(args){  
7.                     success(args);  
8.                 },   
9.                 function(args){  
10.                     fail(args);  
11.                 },   
12.                 'plugintest', //java类  
13.                 'codetest',    //action  
14.                 [params]    //params  
15.     );  
16. };  
17. PhoneGap.addConstructor(function() {  
18.     PhoneGap.addPlugin('testAndroid01API', new testAndroid01API());   
19. });  
20.      
21.   
22. if(!window.plugins) {  
23.     window.plugins = {};  
24. }  
25. if (!window.plugins.testAndroid01API) {  
26.     window.plugins.testAndroid01API = new testAndroid01API();  
27. }



  1. 4,最后在你需要触发的地方加上:  





1. window.plugins.testAndroid01API.test(null,function(r){},function(e){});



 



  1. 这样就可以调用android的Activiy类了,  



 



  1. 同理.如果你的在android端把数据传给phonegap端,流程大概与上面的步骤想似:  



 



  1. 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();
}



 



  1. 4,是在你需要返回结果的地方添加:  


 


1.      var success = function(data){ //当Java方法返回成功时,通过data.key 获得Java中传来的JSONObject数据     
2.                   //alert("1111111 : " + data.testData1 + '   and 2222222 : ' + data.testData2);     
3.                   Ext.getCmp('txt_').setValue(data.qrcodeData);   
4.               };      
5.                   
6.      var error = function(e){      
7.   
8.                   //Ext.getCmp('txt_').setValue(e);      
9.               };      
10.     
11.      window.plugins.testAndroid02API.test(success, error, null);



  1. 这样就可以把数据传递了,当然是你继承了plugin的类中处理方式也是有不同的,例如:  




1. <p>public class resultClass extends Plugin{</p><p>    public static final String ACTION = "resultcode";</p><p>    PluginResult result = null;       
2.     JSONObject jsonObj = new JSONObject();//可以返回给JS的JSON数据   
3.       
4.     private SharedPreferences mPref;  
5.       
6.     @Override  
7.     public PluginResult execute(String action, JSONArray data, String callbackId) {  
8.         mPref = PreferenceManager.getDefaultSharedPreferences(ctx.getContext());  
9.       if(ACTION.equals(action)){  
10.   
11.          String resultCode = mPref.getString("qr_code_result", null);  
12.          while (resultCode==null || resultCode.length()==0) {//循环获取qr_code_result,直到有值  
13.             resultCode = mPref.getString("qr_code_result", null);  
14.      }  
15.      resultCode = mPref.getString("qr_code_result", null);  
16.      if(resultCode.length() != 0 ){  
17.         try {  
18.            jsonObj.put("qrcodeData", resultCode);  
19.            mPref.edit().remove("qr_code_result").commit();  
20.         } catch (JSONException e) {  
21.            e.printStackTrace();  
22.         }        
23.         System.out.println("有值!");  
24.         result = new PluginResult(PluginResult.Status.OK, jsonObj);     
25.              //返回成功时,将Java代码处理过的JSON数据返回给JS   
26.      }else{  
27.         System.out.println("无值!");  
28.         result = null;  
29.      }  
30.      
31.         }  
32.         return result;  
33.     }  
34.       
35. }</p>完!

举报

相关推荐

0 条评论