本文記錄在ecplise裏創建一個基於Cordova的android項目的基本步驟。html
準備工做:html5
一、cordova-3.0.0.jarandroid
二、cordova.jsexpress
三、發開android的eclipse的環境。Android SDK、ADT Pluginapache
開始創建項目安全
一、打開Eclipse,選擇文件->新建->Android Project。與創建原生的android項目同樣的步驟。app
二、把cordova-3.0.0.jar複製到libs目錄下面,若是沒有libs目錄就建立。less
三、在asset目錄下建立www文件夾,把cordova.js複製到www目錄下面。eclipse
四、從下載的包裏面複製xml文件夾到res目錄下面。xml文件夾裏面有文件config.xml。內容以下,有一些我的信息,改不改均可以。ide
<?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <widget xmlns = "http://www.w3.org/ns/widgets" id = "io.cordova.helloCordova" version = "2.0.0"> <name>Hello Cordova</name> <description> A sample Apache Cordova application that responds to the deviceready event. </description> <author href="http://cordova.io" email="callback-dev@incubator.apache.org"> Apache Cordova Team </author> <access origin="*.apache.org"/> <!-- <content src="http://mysite.com/myapp.html" /> for external pages --> <content src="index.html" /> <log level="DEBUG"/> <!-- Preferences for Android --> <preference name="useBrowserHistory" value="true" /> <preference name="exit-on-suspend" value="false" /> <feature name="Activity"> <param name="android-package" value="org.apache.cordova.test.ActivityPlugin" /> </feature> <feature name="App"> <param name="android-package" value="org.apache.cordova.App"/> </feature> <feature name="Geolocation"> <param name="android-package" value="org.apache.cordova.GeoBroker"/> </feature> <feature name="Device"> <param name="android-package" value="org.apache.cordova.Device"/> </feature> <feature name="Accelerometer"> <param name="android-package" value="org.apache.cordova.AccelListener"/> </feature> <feature name="Compass"> <param name="android-package" value="org.apache.cordova.CompassListener"/> </feature> <feature name="Media"> <param name="android-package" value="org.apache.cordova.AudioHandler"/> </feature> <feature name="Camera"> <param name="android-package" value="org.apache.cordova.CameraLauncher"/> </feature> <feature name="Contacts"> <param name="android-package" value="org.apache.cordova.ContactManager"/> </feature> <feature name="File"> <param name="android-package" value="org.apache.cordova.FileUtils"/> </feature> <feature name="NetworkStatus"> <param name="android-package" value="org.apache.cordova.NetworkManager"/> </feature> <feature name="Notification"> <param name="android-package" value="org.apache.cordova.Notification"/> </feature> <feature name="Storage"> <param name="android-package" value="org.apache.cordova.Storage"/> </feature> <feature name="FileTransfer"> <param name="android-package" value="org.apache.cordova.FileTransfer"/> </feature> <feature name="Capture"> <param name="android-package" value="org.apache.cordova.Capture"/> </feature> <feature name="Battery"> <param name="android-package" value="org.apache.cordova.BatteryListener"/> </feature> <feature name="SplashScreen"> <param name="android-package" value="org.apache.cordova.SplashScreen"/> </feature> <feature name="Echo"> <param name="android-package" value="org.apache.cordova.Echo"/> </feature> <feature name="Globalization"> <param name="android-package" value="org.apache.cordova.Globalization"/> </feature> <feature name="InAppBrowser"> <param name="android-package" value="org.apache.cordova.InAppBrowser"/> </feature> </widget>
如今項目須要添加的文件已經添加了,以下圖。
五、接下來,在 assets/www 文件夾下建立一個名爲 index.html 的文件。此文件將用做 Cordova 應用程序界面的主要入口點。文件主要是測試,可參考以下內容
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>a android app base on cordova</title> </head> <body> <h1>Hello Cordova</h1> </body> </html>
注意:文件名要與config.xml裏面設置的文件名一致。config.xml設置的代碼。
<content src="index.html" />
六、須要將 cordova-3.0.0.jar 庫添加到該 Android 項目的構建路徑。對文件cordova-3.0.0.jar右鍵-->Build Path-->Add to Build Path。
七、修改Activity類。
-添加如下導入語句
import org.apache.cordova.DroidGap;
-將基類從 Activity
更改成DroidGap
;它位於類定義中 extends
一詞的後面
public class MainActivity extends DroidGap
-用本地 assets/www/index.html 文件加載 Cordova 界面的引用替換setContentView()
調用函數
super.loadUrl("file:///android_asset/www/index.html");
修改以後以下圖
八、配置項目元數據,修改AndroidManifest.xml
-添加如下supports-screen XML
節點做爲 manifest
根節點的子節點
<supports-screens android:largeScreens="true" android:normalScreens="true" android:smallScreens="true" android:resizeable="true" android:anyDensity="true" />
supports-screen
節點可識別您應用程序支持的屏幕大小。您能夠經過更改此條目的內容來調整屏幕和外觀設置支持。要閱讀有關<supports-screens>,
的更多信息,請訪問 Android 開發人員主題 – 支持屏幕元素。
-爲 Cordova 應用程序配置權限,複製如下<uses-permission>
XML 節點,並粘貼它們做爲 AndroidManifest.xml 文件<manifest>
根節點的子節點。
<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" />
<uses-permission>
XML 值可識別您要爲應用程序啓用的功能。上述代碼行可啓用 PhoneGap 全部功能正常運行所需的所有權限。構建完應用程序後,您可能但願刪除不會實際用到的全部權限;這將會刪除應用程序安裝過程當中出現的安全警告。要閱讀有關 Android 權限和 <uses-permission>
元素的更多信息,請訪問 Android 開發人員主題 – 用戶權限元素。
-修改現有的 <activity>
代碼。找到<activity>
節點,它是 <application>
XML 節點的子節點。將下面的屬性添加到該<activity>
節點:
configChanges="orientation|keyboardHidden"
-爲 org.apache.cordova.DroidGap
類建立一個 <activity>
節點。添加下面的<activity>
節點做爲現有 <activity>
XML 節點的同級節點。
<activity android:name="org.apache.cordova.DroidGap" android:label="@string/app_name" android:configChanges="orientation|keyboardHidden"> <intent-filter></intent-filter> </activity>
此時,已將您的項目配置爲做爲 Android PhoneGap 項目運行。
九、按通常android項目運行便可。
信息來源: