incubator-callback-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Apache Wiki <wikidi...@apache.org>
Subject [Cordova Wiki] Update of "AndroidEclipseQuickstart" by BryceCurtis
Date Mon, 02 Apr 2012 21:24:20 GMT
Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Cordova Wiki" for change notification.

The "AndroidEclipseQuickstart" page has been changed by BryceCurtis:
http://wiki.apache.org/cordova/AndroidEclipseQuickstart

New page:
This is a tutorial for working with Cordova for Android on Eclipse with the latest version
of Cordova.

= 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 Cordova 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. (See below.)

(IMAGE)

Now you can either build the sample project or skip down to create a new project.

= 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 Cordova
download

(IMAGE)

Don't bother using older versions of Android. Use the highest SDK target available. Cordova
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.

(IMAGE) 
 
= Creating A New Project =
 
File > New > Android Project

(IMAGE)

And give it some sensible defaults. 

(IMAGE)

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:

 1. Android/phonegap-1.6.0.jar
 1. Android/phonegap-1.6.0.js

In the root directory of the project you created in Eclipse, create two new directories:

 1. /libs
 1. /assets/www

Now copy

 1. Android/phonegap-1.6.0.jar to /libs 
 1. Android/phonegap-1.6.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>

(IMAGE)

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 org.apache.cordova.*;

(IMAGE)

(You might experience an error here, where Eclipse can't find phonegap-1.6.0.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-1.6.0.jar to the Project. If Eclipse is being temperamental,
you might need to refresh (F5) the project once again.)

Finally, lets add some permissions to the AndroidManifest.xml file to allow phonegap to run
properly.

Open up your manifest file in your favourite editor and paste the following information after
the versionName but before the application tag:

    <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" />

(IMAGE)

In AndroidManifest.xml, make sure the activity tag contains the following attribute:

    android:configChanges="orientation|keyboardHidden"

This tells android not to reload index.html when these events happen; phonegap will handle
these events appropriately.

Last step is to copy the xml folder that came with the PhoneGap download into the res folder
of your 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.

If all goes well then the AVD will load in the emulator -- be prepared to wait for this --
and you application will be uploaded, installed, and run.

(IMAGE)


Have fun!

Mime
View raw message