Android WebView

このガイドでは、より大規模なAndroidアプリケーション内にCordova対応のWebViewコンポーネントを埋め込む方法を示します。これらのコンポーネントがお互いに通信する方法の詳細については、「アプリケーションプラグイン」を参照してください。

Androidに慣れていない場合は、まずAndroidプラットフォームガイドをよく理解し、最新のAndroid SDKをインストールしてから、WebViewを埋め込むという、より特殊な開発オプションを試みるようにしてください。Cordova 1.9以降、AndroidプラットフォームはCordovaWebViewコンポーネントに依存しており、これは1.9リリースより前のレガシーCordovaActivityコンポーネントを基盤として構築されています。

  1. これらの手順に従うには、最新のCordova配布版をインストールしていることを確認してください。cordova.apache.orgからダウンロードし、そのAndroidパッケージを解凍してください。

  2. Androidパッケージの/frameworkディレクトリに移動し、ant jarを実行します。これにより、/framework/cordova-x.x.x.jarという形式のCordova .jarファイルが作成されます。

  3. .jarファイルをAndroidプロジェクトの/libsディレクトリにコピーします。

  4. アプリケーションの/res/xml/main.xmlファイルに以下を追加します。layout_heightlayout_widthidはアプリケーションに合わせて変更してください。

     <org.apache.cordova.CordovaWebView
         android:id="@+id/tutorialView"
         android:layout_width="match_parent"
         android:layout_height="match_parent" />
    
  5. アクティビティが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());
         }
    
  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ファイルとJavaScriptファイルをAndroidプロジェクトの/assets/wwwディレクトリにコピーします。

  9. /framework/res/xmlにあるconfig.xmlファイルをプロジェクトの/res/xmlディレクトリにコピーします。