android PhoneGap 的入门例子

 

1,到 PhoneGap 官方网站下载相关文件。

2,新建一个 Android 工程。

3,将下载后的相关文件添加到 Android 工程的相关目录,如下图:

phonegap

4,将如下权限添加到 AndroidManifest.xml 文件中。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
< 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" />
< uses -permission android:name = "android.permission.GET_ACCOUNTS" />
< uses -permission android:name = "android.permission.BROADCAST_STICKY" />

5,在应用的 Activity 中添加 android:configChanges=”orientation|keyboardHidden” 属性,如下所示:

?
1
2
3
4
5
6
7
8
9
10
< application android:icon = "@drawable/icon" android:label = "@string/app_name" >
         < activity android:name = ".PhoneGapActivity"
                   android:label = "@string/app_name"
                 android:configChanges = "orientation|keyboardHidden" >
             < intent -filter>
                 < action android:name = "android.intent.action.MAIN" />
                 < category android:name = "android.intent.category.LAUNCHER" />
             </ intent >
         </ activity >
</ application >

6,修改 PhoneGapActivity.java 类,让其继承 DroidGap.java,并且将 setContentView(R.layout.main) 替换为 super.loadUrl(“file:///android_asset/www/index.html”)。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
package net.androidla.phonegap;
 
import org.apache.cordova.DroidGap;
import android.os.Bundle;
 
public class PhoneGapActivity extends DroidGap {
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         //setContentView(R.layout.main);
         super .loadUrl( "file:///android_asset/www/index.html" );
     }
}

7,在 /assets/www 目录下新建一个 index.html 文件,内容如下:

?
1
2
3
4
5
6
7
8
9
10
< !DOCTYPE HTML>
< html >
< head >
< title >PhoneGap</ title >
< script type = "text/javascript" charset = "utf-8" src = "phonegap.js" ></ script >
</ head >
< body >
< h1 >Hello World</ h1 >
</ body >
</ html >

8,最后启动应用,在我的手机上的运行效果如下图:

phonegaphelloworld2

入门参考:http://phonegap.com/start#android

API Docs:http://docs.phonegap.com/en/1.5.0/index.html

你可能感兴趣的:(android,PhoneGap,入门例子)