在项目采用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;
-
-
-
-
-
-
-
-
-
-
-
- public class AndroidActivityPlugin extends Plugin{
-
- private Object synObj = new Object();
- @Override
- public PluginResult execute(String action, JSONArray data, String callbackID) {
-
- 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);
- 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) {
-
- 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;
-
-
-
-
-
-
-
-
-
-
-
- public class PhonegapAndroidActivityActivity extends DroidGap {
-
- @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;
-
-
-
-
-
-
-
-
-
-
-
- public class ThirdAndroidActivity extends Activity {
-
- @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',
- 'startActivity',
- [parmas]
- );
- };
- PhoneGap.addConstructor(function() {
- PhoneGap.addPlugin('androidActivity', new AndroidActivity());
- });
调用的测试类:
- >
- <html>
- <head>
- <meta name="viewport" content="width=320; user-scalable=no" />
- <meta http-equiv="Content-type" content="text/html; charset=utf-8">
- <title>Minimal AppLaud Apptitle>
-
- <script type="text/javascript" charset="utf-8" src="phonegap-1.4.1.js">script>
- <script type="text/javascript" charset="utf-8" src="androidActivity.js">script>
- <script type="text/javascript" charset="utf-8">
- function openAndroidActivity(){
- window.plugins.androidActivity.startActivity(
- function(r){//alert(r);
- },
- function(e){//alert(e);
- },
- "com.easyway.etrip.ThirdAndroidActivity"
- );
- }
- script>
- head>
- <body>
- <h1>Hello Worldh1>
- <input type="button" value="打开Android Activity" onclick="javascript:openAndroidActivity();">
- body>
- html>