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
目錄中。