Android WebViews
本指南介绍如何在更大的 Android 应用程序中嵌入支持 Cordova 的 WebView 组件。有关这些组件如何相互通信的详细信息,请参阅应用程序插件。
如果您不熟悉 Android,您应该首先熟悉 Android 平台指南 并在尝试嵌入 WebView 的更不寻常的开发选项之前安装最新的 Android SDK。从 Cordova 1.9 开始,Android 平台依赖于 CordovaWebView
组件,该组件建立在 1.9 版本之前的传统 CordovaActivity
组件之上。
-
要遵循这些说明,请确保您拥有最新的 Cordova 发行版。从 cordova.apache.org 下载并解压缩其 Android 包。
-
导航到 Android 包的
/framework
目录并运行ant jar
。它会创建 Cordova.jar
文件,格式为/framework/cordova-x.x.x.jar
。 -
将
.jar
文件复制到 Android 项目的/libs
目录中。 -
将以下内容添加到应用程序的
/res/xml/main.xml
文件中,修改layout_height
、layout_width
和id
以适合应用程序<org.apache.cordova.CordovaWebView android:id="@+id/tutorialView" android:layout_width="match_parent" android:layout_height="match_parent" />
-
修改活动,使其实现
CordovaInterface
。它应该实现包含的方法。您可能希望从/framework/src/org/apache/cordova/CordovaActivity.java
中复制它们,或者自己实现它们。以下代码片段显示了一个依赖于该接口的基本应用程序。请注意,引用的视图 ID 与上面 XML 片段中指定的id
属性匹配public class CordovaViewTestActivity extends Activity implements 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()); }
-
如果应用程序需要使用相机,请实现以下内容
@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); } }
-
最后,请记住添加线程池,否则插件将没有线程可以运行
@Override public ExecutorService getThreadPool() { return threadPool; }
-
将应用程序的 HTML 和 JavaScript 文件复制到 Android 项目的
/assets/www
目录中。 -
将
config.xml
文件从/framework/res/xml
复制到项目的/res/xml
目录中。