Android以後臺Service的方式獲取GPS資料,並定時傳送到伺服器
阿新 • • 發佈:2019-01-09
在配備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。
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.offbye.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; } } }
GPSServiceListener的實現
package com.offbye.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; } }