http://www.lampbrother.net
PhoneGap/Cordova是一个专业的移动应用开发框架,是一个全面的WEB APP开发的框架,提供了以WEB形式来访问终端设备的API的功能。这对于采用WEB APP进行开发者来说是个福音,这可以避免了原生开发的某些功能。Cordova 只是个原生外壳,app的内核是一个完整的webapp,需要调用的原生功能将以原生插件的形式实现,以暴露js接口的方式调用。
Cordova Android项目是Cordova Android原生部分的Java代码实现,提供了Android原生代码和上层Web页面的javascript通讯接口。本系列文章主要分析Cordova Android框架代码的实现。通过深入分析cordova Android源码的实现,我们在开发HybridApp时可以更从容,知道Cordova的插件的工作原理,开发cordova插件整合自己的功能模块,在实现web应用时避开js和webview的一些坑,开发出高质量的混合应用。
项目源码可以在https://git-wip-us.apache.org/repos/asf找到,上面展示了cordova全部子项目的git地址。当你创建cordova应用,并通过cordova platform add Android命令添加android平台后,在应用的platforms/android/CordovaLib/src/org/apache/cordova目录下面可以找到Cordova Android框架代码。
cordova3.5版本Android核心框架一共有27个Java文件,代码量不算大。从cordova3.0版本以后,所有的设备能力API都从cordova核心框架分离出去,变成了插件,各平台分别进行原生实现,例如访问设备信息的Device插件,访问网络状态的Network Information插件,目前官方网站一共收录了250个插件。下面是 Cordova Android的整体UML类图,从这张图上我们看出核心框架和插件之间的关系,插件需要实现CordovaPlugin接口。
http://www.lampbrother.net
Cordova框架类图
?
CordovaInterface接口分析
CordovaInterface是Cordova应用的底层接口,CordovaActivity需要实现这个接口。用来隔离Cordova插件开发,隔离插件对Cordova核心库的直接依赖。
主要方法有startActivityForResult,setActivityResultCallback,getActivity,onMessage,getThreadPool这几个接口方法。具体的实现在CordovaActivity中。
http://www.lampbrother.net
[java] view plain copy 1. 2. 3. 4. 5. 6. 7. 8.
/**
* The Activity interface that is implemented by CordovaActivity.
* It is used to isolate plugin development, and remove dependency on entire Cordova library. */
public interface CordovaInterface { /**
* Launch an activity for which you would like a result when it finished. When this activity exits,
9. * your onActivityResult() method will be called. 10. *
11. * @param command The command object 12. * @param intent The intent to start
13. * @param requestCode The request code that is passed to callback to i
dentify the activity
14. */
15. abstract public void startActivityForResult(CordovaPlugin command, Inten
t intent, int requestCode);
16.
17. /**
18. * Set the plugin to be called when a sub-activity exits. 19. *
20. * @param plugin The plugin on which onActivityResult is to be call
ed
21. */
22. abstract public void setActivityResultCallback(CordovaPlugin plugin); 23.
24. /**
25. * Get the Android activity. 26. *
27. * @return the Activity 28. */
29. public abstract Activity getActivity(); 30. 31.
32. /**
33. * Called when a message is sent to plugin. 34. *
35. * @param id The message id
http://www.lampbrother.net
36. * @param data The message data 37. * @return Object or null 38. */
39. public Object onMessage(String id, Object data); 40. 41. /**
42. * Returns a shared thread pool that can be used for background tasks. 43. */
44. public ExecutorService getThreadPool(); 45. }
?
CordovaActivity核心类分析
CordovaActivity是Cordova应用的入口类,用户用来加载html页面的Activity需要继承这个Activity。CordovaActivity会读取Cordova配置文件res/xml/config.xml中的配置。 CordovaActivity继承了Android Activity,实现了CordovaInterface接口。 比较重要的成员变量有CordovaWebView appView,CordovaWebViewClient webViewClient,用Executors.newCachedThreadPool()初始化了一个线程池threadPool,创建了Activity返回时的回掉插件activityResultCallback,还有就是启动画面splashscreen的一些变量等。CordovaActivity继承了Activity,因此的它的生命周期和Activity一样,我们可以按照Activity生命周期的顺序开始看代码。
首先看OnCreate方法,首先调用了Config.init(this)方法,读取config.xml文件初始化配置,看Config.java的源码可以知道,该方法主要初始化了url白名单,背景颜色,是否全屏,载入页面超时时间(默认20s),启动画面延时(默认3s)等。然后是读取Intent Extra中等一些参数,设置是否显示标题,是否全屏等。接下来是读取屏幕宽度和高度,创建一个LinearLayoutSoftKeyboardDetect。LinearLayoutSoftKeyboardDetect这个类是用来检测软键盘是否弹出的,主要是重写了onMeasure方法,当软键盘弹出,高度发生变化时发送 app.appView.sendJavascript(“cordova.fireDocumentEvent(‘hidekeyboard’);\事件。我们自己的cordova应用在重新onCreate方法时会依次调用下面的方法。
[java] view plain copy 1. 2.
super.onCreate(savedInstanceState); super.init();
http://www.lampbrother.net
3. 4.
// Set by