2010年12月25日 星期六

SchoolLife-Learning-Android *PhoneGap (跨平台手機app)

 

<陳老師網站>

PhoneGap –> get started

以下文章出處<here>

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.2 download

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:

  1. Android/phonegap.jar
  2. Android/phonegap.js

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

  1. /libs
  2. /assets /www

Now copy

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

然後在專案上按右鍵>>Bulid Path>>Cofigure Bulid Path

image

Libraries>>Add JARs…

image

Now, create index.html in your www folder and add some code like so:

<!DOCTYPE HTML>
<html>

  <head>
    <title>PhoneGap</title>

  </head>

  <body>
  <h1>Welcome to PhoneGap</h1>

  <h2>Edit assets/www/index.html</h2>

  <script type="text/javascript" charset="utf-8" src="phonegap.js"></script>      

  <script type="text/javascript" charset="utf-8">
        document.addEventListener("deviceready", function() {

            alert('initialized');
    }, true);

  </script>

  </body>

</html>

Make a few adjustments too the project's main Java file found in the src folder in Eclipse.

  1. Change the class's extend from Activity to DroidGap
  2. Replace the setContentView() line with super.loadUrl("file:///android_asset/www/index.html");
  3. Add import com.phonegap.*;

(You might experience an error here, where Eclipse can't find phonegap-0.9.2.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-0.9.2.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" />

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.

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.

沒有留言:

張貼留言