Android以后台Service的方式获取GPS数据,并定时发送到服务器

时间:2022-09-17 20:50:23
在配备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.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;
}

}