Phonegap(Cordova)开发文档
(Android)
注意:Adobe 已经将Phonegap捐献给apache,同时也正式更名为Cordova,下面将直接用Cordova代替Phonegap
第一章Phonegap的安装和环境配置
要求:
●Eclipse 3.4 以上
安装SDK +Cordova
●下载并且安装Eclipse Classic
●下载并且安装 Android SDK
●下载并且安装ADT Plugin
●下载并解压Cordova
Cordova第一个程序:Hello
●打开Eclipse,选择File>new>Project>Android Application Project。
填写Application 名称,项目名称,包名,选择sdk版本等。
●指定启动图标。
●选择创建一个空Activity
●确保Activity不继承任何类,单击Finish完成创建。
●在项目的assets文件夹下建立www文件夹
●从解压的Cordova文件夹中拷贝cordova-2.1.0.js 到/assets/www
●从解压的Cordova文件夹中拷贝cordova-2.1.0.jar到/libs
●将cordova-2.1.0.jar加入到类库里面。
●从解压的Cordova文件夹中拷贝xml文件夹到/res
●编辑MainActivity.java文件
?添加import org.apache.cordova.*;
?让Activity替换DroidGap;
?用setContentView(); 替换
super.loadUrl("file:///android_asset/www/index.ht ml");
●编辑AndroidManifest.xml文件,在
加入一下代码:
android:largeScreens="true" android:normalScreens="true" android:smallScreens="true" android:resizeable="true" android:anyDensity="true"/> android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/> ●在 android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" ●最后,你的AndroidManifest.xml文件应该是这样的: 在/assets/www中创建index.ht ml文件。黏贴如下代码:
●在项目上点击右键Run As > Android Application
●Eclipse会让你选择一个AVD。如果没有,你就需要去创建一个。
第二章事件
Deviceready
当Cordova完全载入后,该事件被调用。
详情:
这个事件非常重要,因为每个Cordova程序都需要使用。
支持平台:
?Android
?BlackBerry WebWorks (OS 5.0 and higher)
?iOS
?Windows Phone 7
?Bada 1.2 & 2.x
?Tizen
例子:
// Call onDeviceReady when Cordova is loaded.
//
// At this point, the document has loaded but cordova-2.1.0.js has not.
// When Cordova is loaded and talking with the native device,
// it will call the event `deviceready`.
//
function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false); }
// Cordova is loaded and it is now safe to make calls Cordova methods //
function onDeviceReady() {
// Now safe to use the Cordova API
}
Pause
当Cordova被放入后台运行的时候,该事件被调用。
支持平台:
?Android
?BlackBerry WebWorks (OS 5.0 and hi gher)
?iOS
?Windows Phone 7
例子:
// Call onDeviceReady when Cordova is loaded.
//
// At this point, the document has loaded but cordova-2.1.0.js has not.
// When Cordova is loaded and talking with the native device,
// it will call the event `deviceready`.
//
function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}
// Cordova is loaded and it is now safe to make calls Cordova methods //
function onDeviceReady() {
document.addEventListener("pause", onPause, false);
}
// Handle the pause event
//
function onPause() {
}
Resume
当程序重新获得焦点,该事件被调用。
支持平台:
Android
?BlackBerry WebWorks (OS 5.0 and higher)
?iOS
?Windows Phone 7
例子:
// Call onDeviceReady when Cordova is loaded.
//
// At this point, the document has loaded but cordova-2.1.0.js has not.
// When Cordova is loaded and talking with the native device,
// it will call the event `deviceready`.
//
function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}
// Cordova is loaded and it is now safe to make calls Cordova methods //
function onDeviceReady() {
document.addEventListener("resume", onResume, false);
}
// Handle the resume event
//
function onResume() {
}
Online
当程序连接网络的时候,该事件被调用。
支持平台:
?Android
?BlackBerry WebWorks (OS 5.0 and higher)
?iOS
?Windows Phone 7
?Tizen
例子:
// Call onDeviceReady when Cordova is loaded.
//
// At this point, the document has loaded but cordova-2.1.0.js has not.
// When Cordova is loaded and talking with the native device,
// it will call the event `deviceready`.
//
function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}
// Cordova is loaded and it is now safe to make calls Cordova methods
//
function onDeviceReady() {
document.addEventListener("online", onOnline, false);
}
// Handle the online event
//
function onOnline() {
}
Offline
当程序断开网络连接的时候,该事件被调用。
支持平台:
?Android
?BlackBerry WebWorks (OS 5.0 and higher)
?iOS
?Windows Phone 7
?Tizen
例子:
// Call onDeviceReady when Cordova is loaded.
//
// At this point, the document has loaded but cordova-2.1.0.js has not.
// When Cordova is loaded and talking with the native device,
// it will call the event `deviceready`.
//
function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}
// Cordova is loaded and it is now safe to make calls Cordova methods //
function onDeviceReady() {
document.addEventListener("offline", onOffline, false);
}
// Handle the offline event
//
function onOffline() {
}
Backbutton