本篇文章翻译自cordova官网:其地址为:
http://cordova.apache.org/docs/en/3.6.0/guide/platforms/android/webview.html
Android webview
这篇文章指导用户在Android原生软件里嵌入一个基于cordova的网页页面(webview)。
这些部件是怎样在互相之间交流数据请查看:Application Plugins
如果你不熟悉安卓开发,那你就需要先学习Android开发环境搭建,使用一下网址学习:
http://cordova.apache.org/docs/en/latest/guide/platforms/android/index.html
至少安装Android SDK,在你开发网页嵌入应用之前。自从Cordova 1.9开始。安卓平台依赖组件“CordovaWebView”。
1. 跟随这些说明,确保你安装了最新的cordova版本。,从cordova.apache.org下载并解压。
2. 切换到解压出来文件夹的/framework目录,然后运行ant jar。就会创建出一个叫Cordova.jar的文件。文件被放置在目录/framework/cordova-x.x.x.jar。
3. 复制这个.jar文件到Android项目的/libs目录
4. 然后编辑Android项目目录的/res/xml/main.xml文件。加入一下文件:
<org.apache.cordova.CordovaWebView
android:id="@+id/tutorialView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
5. 修改需要放置网页的activity的类为以下内容,即实现接口CordovaInterface。并实现包括的方法。当然一下的文件其实在这个目录找到:
/framework/src/org/apache/cordova/CordovaActivity.java
以下代码展示了一个依靠接口的基本应用。展示了怎样通过ID调用之前创建的界面文件里的控件。代码如下:
CordovaInterface {
CordovaWebView cwv;
/* Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
cwv = (CordovaWebView) findViewById(R.id.tutorialView);
Config.init(this);
cwv.loadUrl(Config.getStartUrl());
}
6.
如果当前网页页面需要调用摄像头,请实现一下部分。
@Override
public void setActivityResultCallback(CordovaPlugin plugin) {
this.activityResultCallback = plugin;
}
/**
* Launch an activity for which you would like a result when it finished. When this activity exits,
* your onActivityResult() method is called.
*
* @param command The command object
* @param intent The intent to start
* @param requestCode The request code that is passed to callback to identify the activity
*/
public void startActivityForResult(CordovaPlugin command, Intent intent, int requestCode) {
this.activityResultCallback = command;
this.activityResultKeepRunning = this.keepRunning;
// If multitasking turned on, then disable it for activities that return results
if (command != null) {
this.keepRunning = false;
}
// Start activity
super.startActivityForResult(intent, requestCode);
}
@Override
/**
* Called when an activity you launched exits, giving you the requestCode you started it with,
* the resultCode it returned, and any additional data from it.
*
* @param requestCode The request code originally supplied to startActivityForResult(),
* allowing you to identify who this result came from.
* @param resultCode The integer result code returned by the child activity through its setResult().
* @param data An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
*/
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
CordovaPlugin callback = this.activityResultCallback;
if (callback != null) {
callback.onActivityResult(requestCode, resultCode, intent);
}
}
7.
最终,要记得增加线程池。负责插件不知道该在那个线程里运行。
@Override
public ExecutorService getThreadPool() {
return threadPool;
}
8.
复制网页的html页面,js文件等到安卓项目的/assets/www文件夹下
9.
从下载并减压下来的文件夹里的/framework/res/xml里复制config.xml文件到安卓项目目录的/res/xml目录下。