Tutorial details

Getting started with PhoneGap in Eclipse for Android | App Code for Sale | Preview

Getting started with PhoneGap in Eclipse for Android | Android Tutorial

Android development with PhoneGap

Overview PAGE TOP

Android development with PhoneGap can be done in Windows, OS X, or Linux

Step 1: Setting up Android Tools

ADT Bundle – Just a single step to setup android development environment

Step 2: Downloading and installing PhoneGap

  1. Visit the PhoneGap download page and click the orange Download link to begin the download process.
  2. Extract the archive to your local file system for use later.

You are now ready to create your first PhoneGap project for Android within Eclipse.

Step 3: Creating the project in Eclipse

Follow these steps to create a new Android project in Eclipse:

  • Choose New > Android Project


  • On the Application Info screen, type a package name for your main Android application .This should be a namespace that logically represents your package structure; for example, com.yourcompany.yourproject.


  • Create New Project In Workspace and Click Next.


  • Configure Launch Icon and Background


  • Create Activity



Configure the project to use PhoneGap

At this point, Eclipse has created an empty Android project. However, it has not yet been configured to use PhoneGap. You’ll do that next.

  • Create an assets/www directory and a libs directory inside of the new Android project. All of the HTML and JavaScript for your PhoneGap application interface will reside within the assets/www folder


To copy the required files for PhoneGap into the project, first locate the directory where you downloaded PhoneGap, and navigate to the lib/android subdirectory


  • Copy cordova-2.7.0.js to the assets/www directory within your Android project.
  • Copy cordova-2.7.0.jar to the libs directory within your Android project.
  • Copy the xml directory into the res directory within your Android project


  • Next, create a file named index.html in the assets/www folder. This file will be used as the main entry point for your PhoneGap application’s interface.
  • In index.html, add the following HTML code to act as a starting point for your user interface development:

    01  <!DOCTYPE HTML>
    02  <html>
    03  <head>
    04  <title>PhoneGap</title>
    05  <script type="text/javascript" charset="utf-8" src="cordova-2.7.0.js"></script>
    06  </head>
    07  <body>
    08  <h1>Hello PhoneGap</h1>
    09  </body>
    10  </html>
  • You will need to add the cordova-2.7.0.jar library to the build path for the Android project. Right-click cordova-2.7.0.jar and select Build Path > Add To Build Path

Update the Activity class

Now you are ready to update the Android project to start using PhoneGap.

  • Open your main application Activity file. It will be located under the src folder in the project package that you specified earlier in this process.

For my project, which I named HelloPhoneGap, the main Android Activity file is named MainActivity.java, and is located in the package com.maanavan.hellophonegap, which I specified in the New Android Project dialog box.

  • In the main Activity class, add an import statement for org.apache.cordova.DroidGap:

    1 import org.apache.cordova.DroidGap;

  • Change the base class from Activity to DroidGap ; this is in the class definition following the word extends

    1 public class MainActivity extends DroidGap

  • Replace the call to setContentView() with a reference to load the PhoneGap interface from the local assets/www/index.html file, which you created earlier

    1 super.loadUrl(Config.getStartUrl());

Note: In PhoneGap projects, you can reference files located in the assets directory with a URL reference file:///androidasset, followed by the path name to the file. The file:///androidasset URI maps to the assets directory.


Configure the project metadata

You have now configured the files within your Android project to use PhoneGap. The last step is to configure the project metadata to enable PhoneGap to run.

  • Begin by opening the AndroidManifest.xml file in your project root. Use the Eclipse text editor by right-clicking the AndroidManifest.xml file and selecting Open With > Text Editor


In AndroidManifest.xml, add the following supports-screen XML node as a child of the root manifest node:

1   <supports-screens android:largeScreens="true"
2   android:normalScreens="true"
3   android:smallScreens="true"
4   android:resizeable="true"
5   android:anyDensity="true"
6   />

The supports-screen XML node identifies the screen sizes that are supported by your application. You can change screen and form factor support by altering the contents of this entry. To read more about , visit the Android developer topic on the supports-screen element.

Next, you need to configure permissions for the PhoneGap application.

Copy the following XML nodes and paste them as children of the root node in the AndroidManifest.xml file:

01  <uses-permission android:name="android.permission.CAMERA" />
02     <uses-permission android:name="android.permission.VIBRATE" />
03     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
04     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
05     <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
06     <uses-permission android:name="android.permission.INTERNET" />
07     <uses-permission android:name="android.permission.RECEIVE_SMS" />
08     <uses-permission android:name="android.permission.RECORD_AUDIO" />
09     <uses-permission android:name="android.permission.RECORD_VIDEO"/>
10     <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
11     <uses-permission android:name="android.permission.READ_CONTACTS" />
12     <uses-permission android:name="android.permission.WRITE_CONTACTS" /> 
13     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
14     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
15     <uses-permission android:name="android.permission.GET_ACCOUNTS" />
16     <uses-permission android:name="android.permission.BROADCAST_STICKY" />

The XML values identify the features that you want to be enabled for your application. The lines above enable all permissions required for all features of PhoneGap to function. After you have built your application, you may want to remove any permissions that you are not actually using; this will remove security warnings during application installation. To read more about Android permissions and the element, visit the Android developer topic on the uses-permission element..

After you have configured application permissions, you need to modify the existing node.

  • Locate the node, which is a child of the XML node. Add the following attribute to the node:

    1 android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale">

Android Manifest.xml

01  <?xml version="1.0" encoding="utf-8"?>
02  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
03      package="com.maanavan.hellophonegap"
04      android:versionCode="1"
05      android:versionName="1.0"
06       >  <supports-screens
07          android:largeScreens="true"
08          android:normalScreens="true"
09          android:smallScreens="true"
10          android:xlargeScreens="true"
11          android:resizeable="true"
12          android:anyDensity="true"
13          />
15      <uses-sdk
16          android:minSdkVersion="8"
17          android:targetSdkVersion="17" />
19      <uses-permission android:name="android.permission.CAMERA" />
20      <uses-permission android:name="android.permission.VIBRATE" />
21      <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
22      <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
23      <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
24      <uses-permission android:name="android.permission.INTERNET" />
25      <uses-permission android:name="android.permission.RECEIVE_SMS" />
26      <uses-permission android:name="android.permission.RECORD_AUDIO" />
27      <uses-permission android:name="android.permission.RECORD_VIDEO"/>
28      <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
29      <uses-permission android:name="android.permission.READ_CONTACTS" />
30      <uses-permission android:name="android.permission.WRITE_CONTACTS" /> 
31      <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
32      <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
33      <uses-permission android:name="android.permission.GET_ACCOUNTS" />
34      <uses-permission android:name="android.permission.BROADCAST_STICKY" />
36      <application
37          android:allowBackup="true"
38          android:icon="@drawable/ic_launcher"
39          android:label="@string/app_name"
40          android:theme="@style/AppTheme" >
41          <activity
42              android:name="com.maanavan.hellophonegap.MainActivity"
43              android:label="@string/app_name" >
44              android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale">
45              <intent-filter>
46                  <action android:name="android.intent.action.MAIN" />
48                  <category android:name="android.intent.category.LAUNCHER" />
49              </intent-filter>
50          </activity>
51      </application>
53  </manifest>

At this point, your project is configured to run as a PhoneGap project for Android. If you run into any issues, verify your configuration against the example provided at the PhoneGap getting started site for Android.

Running the application

To launch your PhoneGap application in the Android emulator, right-click the project root, and select Run As > Android Application


If you don’t have any Android virtual devices set up, you will be prompted to configure one. To learn more about configuring Android emulator virtual devices

Eclipse will automatically start an Android emulator instance (if one is not already running), deploy your application to the emulator, and launch the application


Reference PAGE TOP


0 Comments Leave a comment

Please login in order to leave a comment.

Newest first

to your Chupamobile Account.

The Easiest way to Launch your next App or Game.

Join Chupamobile and get instant access to thousands of ready made App and Game Templates.

Creating an account means you’re okay with Chupamobile’s Terms of Service and Privacy Policy.