在上文中,介紹了GPS概念及Android開發GPS應用涉及到的常用類和方法。在本文中,開發一個小應用,即時擷取定位資訊,包括使用者所在的緯度、經度、高度、方向、移動速度等。代碼如下: Activity: [java] package comhome.location; import android.app.Activity; import android.content.Context; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.widget.EditText; public class LocationTestActivity extends Activity { // 定義LocationManager對象 private LocationManager locationManager; private EditText show; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); show = (EditText) findViewById(R.id.main_et_show); // 擷取系統LocationManager服務 locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); // 從GPS擷取最近的定位資訊 Location location = locationManager .getLastKnownLocation(LocationManager.GPS_PROVIDER); // 將location裡的位置資訊顯示在EditText中 updateView(location); // 設定每2秒擷取一次GPS的定位資訊 locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 8, new LocationListener() { @Override public void onLocationChanged(Location location) { // 當GPS定位資訊發生改變時,更新位置 updateView(location); } @Override public void onProviderDisabled(String provider) { updateView(null); } @Override public void onProviderEnabled(String provider) { // 當GPS LocationProvider可用時,更新位置 updateView(locationManager .getLastKnownLocation(provider)); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } }); } private void updateView(Location location) { if (location != null) { StringBuffer sb = new StringBuffer(); sb.append("即時的位置資訊:\n經度:"); sb.append(location.getLongitude()); sb.append("\n緯度:"); sb.append(location.getLatitude()); sb.append("\n高度:"); sb.append(location.getAltitude()); sb.append("\n速度:"); sb.append(location.getSpeed()); sb.append("\n方向:"); sb.append(location.getBearing()); sb.append("\n精度:"); sb.append(location.getAccuracy()); show.setText(sb.toString()); } else { // 如果傳入的Location對象為空白則清空EditText show.setText(""); } } } package comhome.location; import android.app.Activity;import android.content.Context;import android.location.Location;import android.location.LocationListener;import android.location.LocationManager;import android.os.Bundle;import android.widget.EditText; public class LocationTestActivity extends Activity {// 定義LocationManager對象private LocationManager locationManager;private EditText show; @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);show = (EditText) findViewById(R.id.main_et_show);// 擷取系統LocationManager服務locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);// 從GPS擷取最近的定位資訊Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);// 將location裡的位置資訊顯示在EditText中updateView(location);// 設定每2秒擷取一次GPS的定位資訊locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,2000, 8, new LocationListener() { @Overridepublic void onLocationChanged(Location location) {// 當GPS定位資訊發生改變時,更新位置updateView(location);} @Overridepublic void onProviderDisabled(String provider) {updateView(null);} @Overridepublic void onProviderEnabled(String provider) {// 當GPS LocationProvider可用時,更新位置updateView(locationManager.getLastKnownLocation(provider)); } @Overridepublic void onStatusChanged(String provider, int status,Bundle extras) {}});} private void updateView(Location location) {if (location != null) {StringBuffer sb = new StringBuffer();sb.append("即時的位置資訊:\n經度:");sb.append(location.getLongitude());sb.append("\n緯度:");sb.append(location.getLatitude());sb.append("\n高度:");sb.append(location.getAltitude());sb.append("\n速度:");sb.append(location.getSpeed());sb.append("\n方向:");sb.append(location.getBearing());sb.append("\n精度:");sb.append(location.getAccuracy());show.setText(sb.toString());} else {// 如果傳入的Location對象為空白則清空EditTextshow.setText("");}} }布局XML: [html] <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <EditText android:id="@+id/main_et_show" android:layout_width="match_parent" android:layout_height="match_parent" android:cursorVisible="false" android:editable="false" /> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <EditText android:id="@+id/main_et_show" android:layout_width="match_parent" android:layout_height="match_parent" android:cursorVisible="false" android:editable="false" /> </LinearLayout>許可權: [html] <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>附片效果: