phonegap-android-eclipse-quickstart
Requirements
It is assumed that you have the Android SDK installed and working. If not, then you need to install it before proceeding. You can find the download and installation instructions here. Also, ensure that you have created at least one Android virtual device (AVD). You will need an AVD to run your project in the Android emulator.
You also need to:
Download the latest copy of PhoneGap and extract its contents. We are only interested in the Android directory.
In Eclipse, ensure that you have told Eclipse where the Android SDK is installed in Preferences > Android.
Building The Sample Project
File > New > Android Project
Select Create new project from existing source
Click Browse and point it to the location of the sample app provided with your PhoneGap 0.9.4 download
Don't bother using older versions of Android. Use the highest SDK target available. Phonegap will take care of backwards compatibility for you. Note: You may experience issues with Android 3.0 so try using Android 2.2 or 2.3 first.
(You might experience an error here, where Eclipse can't find phonegap.jar. In this case, right click on the /libs folder and go to Build Paths/ > Configure Build Paths. Then, in the Libraries tab, add phonegap.jar to the Project. If Eclipse is being temperamental, you might need to refresh (F5) the project.)
You can now run you project as an Android Application. Right click the project and go to Run As and click Android Application. Eclipse might ask you to select an appropriate AVD. If there isn't one, then you'll need to create it before you can continue.
Creating A New Project
File > New > Android Project
And give it some sensible defaults.
Don't bother using older versions of Android. Use the highest SDK target available. Phonegap will take care of backwards compatibility for you.
From the PhoneGap download earlier, we need the following two files:
Android/phonegap-1.0.0.jar
Android/phonegap-1.0.0.js
In the root directory of the project you created in Eclipse, create two new directories:
/libs
/assets /www
Now copy
Android/phonegap-1.0.0.jar to /libs
Android/phonegap-1.0.0.js to /assets/www
In Eclipse, select the project in the Package Explorer and refresh (F5) the project. The copied file will appear in the project.
Now, create index.html in your www folder and add some code like so:
<!DOCTYPE HTML>
<html>
<head>
<title>PhoneGap</title>
<script type="text/javascript" charset="utf-8" src="phonegap-1.0.0.js"></script>
<script type="text/javascript" charset="utf-8">
function onLoad(){
document.addEventListener("deviceready", onDeviceReady, true);
}
function onDeviceReady(){
navigator.notification.alert("PhoneGap is working");
}
</script>
</head>
<body onload="onLoad();">
<h1>Welcome to PhoneGap</h1>
<h2>Edit assets/www/index.html</h2>
</body>
</html>
Make a few adjustments to the project's main Java file found in the src folder in Eclipse.
Change the class's extend from Activity to DroidGap
Replace the setContentView() line with super.loadUrl("file:///android_asset/www/index.html");
Add import com.phonegap.*;
<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" />
No comments:
Post a Comment