在配備Android系統的手機中,一般都配備了GPS設備。Android為我們獲取GPS數據提供了很好的接口。本文來說一下如何使用Android獲取GPS的經緯度。
1 從Service繼承一個類。
2 創建startService()方法。
3 創建endService()方法 重載onCreate方法和onDestroy方法,並在這兩個方法裡面來調用startService以及endService。
4 在startService中,通過getSystemService方法獲取Context.LOCATION_SERVICE。
5 基於LocationListener實現一個新類。默認將重載四個方法onLocationChanged、onProviderDisabled、onProviderEnabled、onStatusChanged。對於onLocationChanged方法是我們更新最新的GPS數據的方法。一般我們的操作都只需要在這裡進行處理。
6 調用LocationManager的requestLocationUpdates方法,來定期觸發獲取GPS數據即可。在onLocationChanged函數裡面可以實現我們對得到的經緯度的最終操作。
7 最後在我們的Activity裡面通過按鈕來啟動Service,停止Service。
示意代碼如下:
- package com.jouhu.gpsservice;
-
- import android.app.Service;
- import android.content.Context;
- import android.content.Intent;
- import android.location.LocationListener;
- import android.location.LocationManager;
- import android.os.Binder;
- import android.os.IBinder;
- import android.util.Log;
-
- public class GPSService extends Service {
-
- //2000ms
- private static final long minTime = 2000;
- //最小變更距離 10m
- private static final float minDistance = 10;
-
- String tag = this.toString();
-
- private LocationManager locationManager;
- private LocationListener locationListener;
-
- private final IBinder mBinder = new GPSServiceBinder();
-
- public void startService()
- {
- locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
- locationListener = new GPSServiceListener();
- locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime, minDistance, locationListener);
- }
-
- public void endService()
- {
- if(locationManager != null && locationListener != null)
- {
- locationManager.removeUpdates(locationListener);
- }
- }
-
- @Override
- public IBinder onBind(Intent arg0) {
- // TODO Auto-generated method stub
- return mBinder;
- }
-
- @Override
- public void onCreate()
- {
- //
- startService();
- Log.v(tag, "GPSService Started.");
- }
-
- @Override
- public void onDestroy()
- {
- endService();
- Log.v(tag, "GPSService Ended.");
- }
-
- public class GPSServiceBinder extends Binder {
- GPSService getService() {
- return GPSService.this;
- }
- }
- }
GPRSServiceListener的實現:
- package com.jouhu.gpsservice;
-
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.GregorianCalendar;
- import java.util.TimeZone;
-
- import android.location.Location;
- import android.location.LocationListener;
- import android.location.LocationProvider;
- import android.os.Bundle;
- import android.util.Log;
- import android.widget.Toast;
-
- public class GPSServiceListener implements LocationListener {
-
- private static final String tag = "GPSServiceListener";
- private static final float minAccuracyMeters = 35;
- private static final String hostUrl = "http://doandroid.info/gpsservice/position.php?";
- private static final String user = "huzhangyou";
- private static final String pass = "123456";
- private static final int duration = 10;
- private final DateFormat timestampFormat = new SimpleDateFormat("yyyyMMddHHmmss");
-
- public int GPSCurrentStatus;
-
- @Override
- public void onLocationChanged(Location location) {
- // TODO Auto-generated method stub
- if(location != null)
- {
- if (location.hasAccuracy() && location.getAccuracy() <= minAccuracyMeters)
- {
- //獲取時間參數,將時間一並Post到服務器端
- GregorianCalendar greg = new GregorianCalendar();
- TimeZone tz = greg.getTimeZone();
- int offset = tz.getOffset(System.currentTimeMillis());
- greg.add(Calendar.SECOND, (offset/1000) * -1);
- StringBuffer strBuffer = new StringBuffer();
- strBuffer.append(hostUrl);
- strBuffer.append("user=");
- strBuffer.append(user);
- strBuffer.append("&pass=");
- strBuffer.append(pass);
- strBuffer.append("&Latitude=");
- strBuffer.append(location.getLatitude());
- strBuffer.append("&Longitude=");
- strBuffer.append(location.getLongitude());
- strBuffer.append("&Time=");
- strBuffer.append(timestampFormat.format(greg.getTime()));
- strBuffer.append("&Speed=");
- strBuffer.append(location.hasSpeed());
- doGet(strBuffer.toString());
- Log.v(tag, strBuffer.toString());
- }
- }
- }
-
- //將數據通過get的方式發送到服務器,服務器可以根據這個數據進行跟蹤用戶的行走狀態
- private void doGet(String string) {
- // TODO Auto-generated method stub
- //
- }
-
- @Override
- public void onProviderDisabled(String provider) {
- // TODO Auto-generated method stub
- }
-
- @Override
- public void onProviderEnabled(String provider) {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public void onStatusChanged(String provider, int status, Bundle extras)
- {
- // TODO Auto-generated method stub
- GPSCurrentStatus = status;
- }
-
- }