- 下载安装 Eclipse Classic
- 下载安装Android SDK
- 下载安装ADT Plugin
- 下载Cordova,并解压到任意目录
二、搭建环境
配置环境变量
三、Eclipse建立工程
1、新建普通android工程,New > Android Project
在根目录下新建
- /libs
- assets/www
拷贝PhoneGap中的资料
- 拷贝cordova-2.5.0.js 到 assets/www
- 拷贝cordova-2.5.0.jar到/libs
- 拷贝xml文件夹 到/res
- 拷贝android文件夹到/libs
建立build Path,鼠标右键单击/libs文件夹,并选择 Build Paths/ > Configure Build Path...
修改MainActivity
完整代码为
package com.hellocordova; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import org.apache.cordova.DroidGap; public class MainActivity extends DroidGap { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.loadUrl("file:///android_asset/www/index.html"); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
鼠标右键AndroidManifest.xml文件并选择Open With > XML Editor
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.hellocordova" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="5" android:targetSdkVersion="15" /> <supports-screens android:largeScreens="true" android:normalScreens="true" android:smallScreens="true" android:resizeable="true" android:anyDensity="true" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.RECEIVE_SMS" /> <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /> <uses-permission android:name="android.permission.READ_CONTACTS" /> <uses-permission android:name="android.permission.WRITE_CONTACTS" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.GET_ACCOUNTS" /> <uses-permission android:name="android.permission.BROADCAST_STICKY" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.hellocordova.MainActivity" android:label="@string/app_name" android:configChanges="orientation|keyboardHidden" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
在<uses-sdk和<application之间加入代码,同时为 <activity>标签加入属性
android:configChanges="orientation|keyboardHidden"
在assets/www中创建index.html文件,写入以下代码
<!DOCTYPE HTML> <html> <head> <title>Cordova</title> <script type="text/javascript" charset="utf-8" src="cordova-2.5.0.js"></script> </head> <body> <h1>Hello World</h1> </body> </html>
在模拟器中运行,Run As > Android Application