Android WebView
このガイドでは、より大規模なAndroidアプリケーション内にCordova対応のWebViewコンポーネントを埋め込む方法を示します。これらのコンポーネントがお互いに通信する方法の詳細については、「アプリケーションプラグイン」を参照してください。
Androidに慣れていない場合は、まずAndroidプラットフォームガイドをよく理解し、最新のAndroid SDKをインストールしてから、WebViewを埋め込むという、より特殊な開発オプションを試みるようにしてください。Cordova 1.9以降、AndroidプラットフォームはCordovaWebView
コンポーネントに依存しており、これは1.9リリースより前のレガシーCordovaActivity
コンポーネントを基盤として構築されています。
-
これらの手順に従うには、最新のCordova配布版をインストールしていることを確認してください。cordova.apache.orgからダウンロードし、そのAndroidパッケージを解凍してください。
-
Androidパッケージの
/framework
ディレクトリに移動し、ant jar
を実行します。これにより、/framework/cordova-x.x.x.jar
という形式のCordova.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
ディレクトリにコピーします。 -
/framework/res/xml
にあるconfig.xml
ファイルをプロジェクトの/res/xml
ディレクトリにコピーします。