Android以后台Service的方式获取GPRS数据


在配备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. package com.jouhu.gpsservice;  
  2.    
  3. import android.app.Service;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.location.LocationListener;  
  7. import android.location.LocationManager;  
  8. import android.os.Binder;  
  9. import android.os.IBinder;  
  10. import android.util.Log;  
  11.    
  12. public class GPSService extends Service {  
  13.    
  14.     //2000ms   
  15.     private static final long minTime = 2000;  
  16.     //最小变更距离 10m   
  17.     private static final float minDistance = 10;  
  18.    
  19.     String tag = this.toString();  
  20.    
  21.     private LocationManager locationManager;  
  22.     private LocationListener locationListener;  
  23.    
  24.     private final IBinder mBinder = new GPSServiceBinder();  
  25.    
  26.     public void startService()  
  27.     {  
  28.         locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);  
  29.         locationListener = new GPSServiceListener();  
  30.         locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime, minDistance, locationListener);  
  31.     }  
  32.    
  33.     public void endService()  
  34.     {  
  35.         if(locationManager != null && locationListener != null)  
  36.         {  
  37.             locationManager.removeUpdates(locationListener);  
  38.         }  
  39.     }  
  40.    
  41.     @Override  
  42.     public IBinder onBind(Intent arg0) {  
  43.         // TODO Auto-generated method stub   
  44.         return mBinder;  
  45.     }  
  46.    
  47.     @Override  
  48.     public void onCreate()  
  49.     {  
  50.         //   
  51.         startService();  
  52.         Log.v(tag, "GPSService Started.");  
  53.     }  
  54.    
  55.     @Override  
  56.     public void onDestroy()  
  57.     {  
  58.         endService();  
  59.         Log.v(tag, "GPSService Ended.");  
  60.     }  
  61.    
  62.     public class GPSServiceBinder extends Binder {  
  63.         GPSService getService() {  
  64.             return GPSService.this;  
  65.         }  
  66.     }  
  67. }  

GPRSServiceListener的实现:

  1. package com.jouhu.gpsservice;  
  2.    
  3. import java.text.DateFormat;  
  4. import java.text.SimpleDateFormat;  
  5. import java.util.Calendar;  
  6. import java.util.GregorianCalendar;  
  7. import java.util.TimeZone;  
  8.    
  9. import android.location.Location;  
  10. import android.location.LocationListener;  
  11. import android.location.LocationProvider;  
  12. import android.os.Bundle;  
  13. import android.util.Log;  
  14. import android.widget.Toast;  
  15.    
  16. public class GPSServiceListener implements LocationListener {  
  17.    
  18.     private static final String tag = "GPSServiceListener";  
  19.     private static final float minAccuracyMeters = 35;  
  20.     private static final String hostUrl = "http://doandroid.info/gpsservice/position.php?";  
  21.     private static final String user = "huzhangyou";  
  22.     private static final String pass = "123456";  
  23.     private static final int duration = 10;  
  24.     private final DateFormat timestampFormat = new SimpleDateFormat("yyyyMMddHHmmss");  
  25.    
  26.     public int GPSCurrentStatus;  
  27.    
  28.     @Override  
  29.     public void onLocationChanged(Location location) {  
  30.         // TODO Auto-generated method stub   
  31.         if(location != null)  
  32.         {  
  33.             if (location.hasAccuracy() && location.getAccuracy() <= minAccuracyMeters)  
  34.             {  
  35.                 //获取时间参数,将时间一并Post到服务器端   
  36.                 GregorianCalendar greg = new GregorianCalendar();  
  37.                 TimeZone tz = greg.getTimeZone();  
  38.                 int offset = tz.getOffset(System.currentTimeMillis());  
  39.                 greg.add(Calendar.SECOND, (offset/1000) * -1);  
  40.                 StringBuffer strBuffer = new StringBuffer();  
  41.                 strBuffer.append(hostUrl);  
  42.                 strBuffer.append("user=");  
  43.                 strBuffer.append(user);  
  44.                 strBuffer.append("&pass=");  
  45.                 strBuffer.append(pass);  
  46.                 strBuffer.append("&Latitude=");  
  47.                 strBuffer.append(location.getLatitude());  
  48.                 strBuffer.append("&Longitude=");  
  49.                 strBuffer.append(location.getLongitude());  
  50.                 strBuffer.append("&Time=");  
  51.                 strBuffer.append(timestampFormat.format(greg.getTime()));  
  52.                 strBuffer.append("&Speed=");  
  53.                 strBuffer.append(location.hasSpeed());  
  54.                 doGet(strBuffer.toString());  
  55.                 Log.v(tag, strBuffer.toString());  
  56.             }  
  57.         }  
  58.     }  
  59.    
  60.     //将数据通过get的方式发送到服务器,服务器可以根据这个数据进行跟踪用户的行走状态   
  61.     private void doGet(String string) {  
  62.         // TODO Auto-generated method stub   
  63.         //   
  64.     }  
  65.    
  66.     @Override  
  67.     public void onProviderDisabled(String provider) {  
  68.         // TODO Auto-generated method stub   
  69.     }  
  70.    
  71.     @Override  
  72.     public void onProviderEnabled(String provider) {  
  73.         // TODO Auto-generated method stub   
  74.    
  75.     }  
  76.    
  77.     @Override  
  78.     public void onStatusChanged(String provider, int status, Bundle extras)  
  79.     {  
  80.         // TODO Auto-generated method stub   
  81.         GPSCurrentStatus = status;  
  82.     }  
  83.    
  84. }  

相关内容