Cordova/Phonegap for Androidプロジェクトの作成
8810 ワード
Eclipse Android Developer ToolsでAndroid Application Projectを新規作成するこの投稿は、http://xuekaiyuan.com/forum.php?mod=viewthread&tid=8 Creates a new Android Applicationインタフェースでプログラム関連情報を入力コンフィギュレーションProjectインタフェースでCreate custom launcher iconを選択しますCreate activityを選択しますMark this project as a libraryを選択します.ct in Workspace Create ActivityインタフェースでCreate Activityを選択解除
Cordovaライブラリを参照Projectメニューをクリックし、Propertiesメニュー項目の左側をクリックしてAndroidを選択し、右側のLibraryでAddボタンをクリックしてCordovaを選択
作成HTMLファイルassetsフォルダにwwwフォルダを作成し、wwwフォルダにindexを作成する.htmlファイル、次の内容を入力します
アプリケーションのタイトルバー編集/res/values/stylesを非表示にします.xmlファイル、次のコードの場所にアプリケーションを表示しないタイトルバーを1行追加
新しいAndroid XML Layout Fileファイルファイルにmainを入力Root ElementでLinearLayoutを選択すると/res/layout/mainが生成されます.xmlファイル
編集/res/layout/main.xmlエディタでorgをドラッグ&ドロップapache.cordova.CordovaWebViewは設計領域にあり、ファイルには以下のコードが自動的に追加されます.
ファイルの完全なコードは以下の通りです.
Activityクラスを作成srcでパッケージtest 1を作成する.Actionsはパッケージtest 1にあります.Actionsで作成するクラスTest 1 Activity Superclass androidを選択します.app.Activity InterfacesのAdd org.apache.cordova.api.CordovaInterface Inherited abstract methodsを選択
Cordova要件によるgetActivity関数の実装
Cordovaの要件に従ってgetThreadPool関数を実装してメンバー変数を作成する
getThreadPool関数の作成
Cordovaの要件に従ってWebViewオブジェクトをバインドしてメンバー変数を作成
onCreate関数の作成
onDestroy関数の作成
完全なプログラムコードは以下の通りです.
AndroidManifestを編集します.xmlファイルAndroidManifestを開きます.xmlエディタ、「Application」タブを選択Application Nodesで「Add」ボタンをクリックし、「Activity」を選択して「Activity」を選択し、Name値を設定.actions.Test 1 ActivityはActivityを選択し、Addボタンをクリックし、Intent Filterを選択してIntent Filterを選択し、Addボタンをクリックし、Actionを選択してActionを選択し、Name値をandroidに設定.intent.action.MAIN Categoryを選択し、Name値をandroidに設定します.intent.category.LAUNCHERが完了すると、ファイルに次のコードが追加されます.
完全なファイルコードは次のとおりです.
仮想マシンで実行する効果は、次のです.
Cordovaライブラリを参照Projectメニューをクリックし、Propertiesメニュー項目の左側をクリックしてAndroidを選択し、右側のLibraryでAddボタンをクリックしてCordovaを選択
作成HTMLファイルassetsフォルダにwwwフォルダを作成し、wwwフォルダにindexを作成する.htmlファイル、次の内容を入力します
<html>
<body>
<p>Hello World!</p>
</body>
</html>
アプリケーションのタイトルバー編集/res/values/stylesを非表示にします.xmlファイル、次のコードの場所にアプリケーションを表示しないタイトルバーを1行追加
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="android:windowNoTitle">true</item>
</style>
新しいAndroid XML Layout Fileファイルファイルにmainを入力Root ElementでLinearLayoutを選択すると/res/layout/mainが生成されます.xmlファイル
編集/res/layout/main.xmlエディタでorgをドラッグ&ドロップapache.cordova.CordovaWebViewは設計領域にあり、ファイルには以下のコードが自動的に追加されます.
<org.apache.cordova.CordovaWebView
android:id="@+id/cordovaWebView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
ファイルの完全なコードは以下の通りです.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<org.apache.cordova.CordovaWebView
android:id="@+id/cordovaWebView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Activityクラスを作成srcでパッケージtest 1を作成する.Actionsはパッケージtest 1にあります.Actionsで作成するクラスTest 1 Activity Superclass androidを選択します.app.Activity InterfacesのAdd org.apache.cordova.api.CordovaInterface Inherited abstract methodsを選択
Cordova要件によるgetActivity関数の実装
@Override
public Activity getActivity() {
// TODO Auto-generated method stub
return this;
}
Cordovaの要件に従ってgetThreadPool関数を実装してメンバー変数を作成する
private final ExecutorService threadPool = Executors.newCachedThreadPool();
getThreadPool関数の作成
@Override
public ExecutorService getThreadPool() {
// TODO Auto-generated method stub
return threadPool;
}
Cordovaの要件に従ってWebViewオブジェクトをバインドしてメンバー変数を作成
private CordovaWebView cordovaWebView;
onCreate関数の作成
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
cordovaWebView = (CordovaWebView) findViewById(R.id.cordovaWebView1);
cordovaWebView.loadUrl("file:///android_asset/www/index.html");
}
onDestroy関数の作成
@Override
public void onDestroy() {
super.onDestroy();
if (cordovaWebView != null) {
cordovaWebView.handleDestroy();
}
}
完全なプログラムコードは以下の通りです.
package test1.actions;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.api.CordovaInterface;
import org.apache.cordova.api.CordovaPlugin;
import test1.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class Test1Activity extends Activity implements CordovaInterface {
private final ExecutorService threadPool = Executors.newCachedThreadPool();
private CordovaWebView cordovaWebView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
cordovaWebView = (CordovaWebView) findViewById(R.id.cordovaWebView1);
cordovaWebView.loadUrl("file:///android_asset/www/index.html");
}
@Override
public void onDestroy() {
super.onDestroy();
if (cordovaWebView != null) {
cordovaWebView.handleDestroy();
}
}
@Override
public void startActivityForResult(CordovaPlugin command, Intent intent,
int requestCode) {
// TODO Auto-generated method stub
}
@Override
public void setActivityResultCallback(CordovaPlugin plugin) {
// TODO Auto-generated method stub
}
@Override
public Activity getActivity() {
// TODO Auto-generated method stub
return this;
}
@Override
public Object onMessage(String id, Object data) {
// TODO Auto-generated method stub
return null;
}
@Override
public ExecutorService getThreadPool() {
// TODO Auto-generated method stub
return threadPool;
}
}
AndroidManifestを編集します.xmlファイルAndroidManifestを開きます.xmlエディタ、「Application」タブを選択Application Nodesで「Add」ボタンをクリックし、「Activity」を選択して「Activity」を選択し、Name値を設定.actions.Test 1 ActivityはActivityを選択し、Addボタンをクリックし、Intent Filterを選択してIntent Filterを選択し、Addボタンをクリックし、Actionを選択してActionを選択し、Name値をandroidに設定.intent.action.MAIN Categoryを選択し、Name値をandroidに設定します.intent.category.LAUNCHERが完了すると、ファイルに次のコードが追加されます.
<activity android:name=".actions.Test1Activity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
完全なファイルコードは次のとおりです.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.daonao.test1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="1"
android:targetSdkVersion="17" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name=".actions.Test1Activity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
仮想マシンで実行する効果は、次のです.