摘自:http://topmanopensource.iteye.com/blog/1577717
在项目采用Native+Web方式开发,Android团队开发一个模块的代码,WebApp phonegap开发几个模块,需要将Android中的模块潜入到Phonegap开发的几个模块中。需要通过Phonegap调用Android 的Activity。
demo如下:
Phonegap插件类:
package com.easyway.etrip.plugins; import org.json.JSONArray; import android.app.Activity; import android.content.Intent; import android.os.Handler; import android.os.Message; import android.util.Log; import com.phonegap.api.Plugin; import com.phonegap.api.PluginResult; /** * Phonegap 调用Android Activity的插件 * * @Title: * @Description: 实现TODO * @Copyright:Copyright (c) 2011 * @Company:易程科技股份有限公司 * @Date:2012-7-5 * @author longgangbai * @version 1.0 */ public class AndroidActivityPlugin extends Plugin{ private Object synObj = new Object(); @Override public PluginResult execute(String action, JSONArray data, String callbackID) { // TODO Auto-generated method stub if(action.equals("startActivity")){ PluginResult result = null; try { PluginResult.Status status = PluginResult.Status.OK; if(action.equals("startActivity")){ Log.e("test", "test plugin js -> java~~~~"+data.getString(0)); Log.e("test", "test plugin js -> java~~~~"); result = new PluginResult(status, data.getString(0)); Message msg=new Message(); msg.what=1; Log.e("参数信息:", data.toString()); msg.obj=data.getString(0); handler.sendMessage(msg); sleep(); } } catch (Exception e) { } return result; }else { return new PluginResult(PluginResult.Status.INVALID_ACTION); } } private Handler handler = new Handler() { public void handleMessage(Message msg) { if (msg == null) { return; } switch (msg.what) { case 1: String className=msg.obj.toString(); try { Class activityClass = Class.forName(className); Intent intent = new Intent(ctx.getBaseContext(), activityClass);//你想去的activity(exp:Temp) ctx.startActivityForResult(AndroidActivityPlugin.this, intent, 1); } catch (ClassNotFoundException e) { Log.e("Why", "错误原因:"+className); e.printStackTrace(); } break; } }; }; @Override public void onActivityResult(int requestCode, int resultCode, Intent intent) { // TODO Auto-generated method stub if(requestCode==1){ if(resultCode == Activity.RESULT_CANCELED){ //返回时调用 Log.e("test", "result plugin -> java~~~~"); Log.e("test", "result plugin -> java~~~~"); weakup(); } }else{ super.onActivityResult(requestCode, resultCode, intent); } } private void sleep() { try { synchronized(synObj) { synObj.wait(); } } catch (InterruptedException e) { e.printStackTrace(); } } private void weakup() { synchronized(synObj) { synObj.notify(); } } }
phonegap启动类:
package com.easyway.etrip; import android.os.Bundle; import com.phonegap.DroidGap; /** * phonegap启动类 * * @Title: * @Description: 实现TODO * @Copyright:Copyright (c) 2011 * @Company:易程科技股份有限公司 * @Date:2012-7-5 * @author longgangbai * @version 1.0 */ public class PhonegapAndroidActivityActivity extends DroidGap { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.loadUrl("file:///android_asset/www/index.html"); } }
Android的Activity类:
package com.easyway.etrip; import android.app.Activity; import android.os.Bundle; /** * * * @Title: * @Description: 实现TODO * @Copyright:Copyright (c) 2011 * @Company:易程科技股份有限公司 * @Date:2012-7-5 * @author longgangbai * @version 1.0 */ public class ThirdAndroidActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.main); } }
phonegap的插件配置类phonegap.xml:
<plugin name="AndroidActivity" value="com.easyway.etrip.plugins.AndroidActivityPlugin"/>
androidActivity.js
var AndroidActivity=function(){}; AndroidActivity.prototype.startActivity = function(success, fail,parmas) { return PhoneGap.exec( function(args){ success(args); }, function(args){ fail(args); }, 'AndroidActivity', //java类 'startActivity', //action [parmas] //params ); }; PhoneGap.addConstructor(function() { PhoneGap.addPlugin('androidActivity', new AndroidActivity()); });
调用的测试类: