在安装PhoneGap开发环境之前,需要按顺序安装以下工具:
1.Java SDK
java sdk,不安装的话不能正常安装Android SDK。
安装成功检测:启动DOS窗口start-->run-->cmd,在DOS窗口中键入:java -version,如能显示版本信息说明安装正常。
2.Eclipse
java开发工具,这我就不用多说了,推荐装classic版的。
3.Android SDK
下下来安装完之后是一个Android SDK Manager,你需要下载以下组件,可能需要较长时间:
4.ADT Plugin
这是一个Eclipse插件,作用是关联Android SDK,使你的Eclipse能够新建Android工程,安装方法如下:
打开Eclipse中的菜单 “Help”->”InstallNewSoftware”进入软件安装界面,点击“Add”按钮。
https://dl-ssl.google.com/android/eclipse
5.PhoneGap
下载PhoneGap,解压缩即可,打开里面的libs>android文件夹:
其中标红的三个文件夹是我们需要用到的。
二.新建一个PhoneGap项目
1.在eclipse中新建Android Project。
2.在项目的目录下,建两个文件夹:
/libs
/assets/www
3.进入将刚刚下载并解压的PhoneGap包里Anroid目录,我们需要的资源都在这个目录下。
将cordova-2.0.0.js这个js文件(具体名称视当时下载的版本而定)copy到/assets/www目录下,
把cordova-2.0.0.jar文件copy到/libs目录下。
再把xml目录(xml整个文件夹)copy到android项目的res目录下。
4.在/assets/www下建立index.html文件,内容看起来像这样:[code]
Hello World
[/code]5.将以下权限配置的xml内容copy到AndroidManifest.xml文件中:[code]
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:resizeable="true"
android:anyDensity="true"
/>
android:configChanges="orientation|keyboardHidden
这是为了保证机器在横竖屏切换的时候不会重新执行Activity的onCreate方法;
7.AndroidManifest.xml最后看起来会像这样:[code]
android:versionCode="1"
android:versionName="1.0">
android:largeScreens="true"
android:normalScreens="true"
android:resizeable="true"
android:smallScreens="true"
android:xlargeScreens="true" />
android:theme="@style/AppTheme">
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name" >
android.intent.action.MAIN表示是最先启动的的界面;
android.intent.category.LAUNCHER决定应用程序是否显示在程序列表里;
另外需要注意的是:[code]
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name" >[/code]我们的
8.在刚刚新建的Android Project中找到libs目录并在cordova-2.0.0.jar上点击右键,选择 Build Path->Add to Build Path
9.最后再修改下src下的Java主文件(如果没有就自己创建一个),我们要做以下几件事:
1)添加import com.phonegap.*;
2)删掉import android.app.Activity;
3)还记得刚才的outer类么?这里将outer继承为DroidGap;
4)把setContentView()这行替换为super.loadUrl("file:///android_asset/www/index.html");
5)最后看起来就像这样:[code]package com.example.shawn;
import android.os.Bundle;
import org.apache.cordova.*;
public class outer extends DroidGap {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/index.html");
}
}[/code]package com.example.shawn;这句话是干嘛的呢?这是java中常用的,类似于文件的姓氏,看看文件树就知道了,这是放在文件前面定位用的。
public class outer extends DroidGap;这里将outer类继承为DroidGap,同时本文件的文件名也要注意保持一致(outer.java),否则会报错。
super.loadUrl("file:///android_asset/www/index.html");这句话大家可以理解为加载一个网页,这个路径大家很熟悉,就是我们的首页。透过这句话我们也可以看到phoneGap最根本的东西,就是在原生语言与网页语言之间架一座桥。
然后就可以在模拟器下试着运行一下这个项目,成功的话会出现Hello World的界面。
ok了,至此为止,phonegap的android开发环境就搭建好了,您可以基于phonegap编写自己的android应用了!
作者:shawn-xie