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 ffset = 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
}
}
摘自 offbye的技术博客
1、若是定位终端的话,请GPS公司帮忙改下数据就完事了。2、通过模拟一段路程轨迹,然后上传到服务器,服务器就产生了里程。
3、通过电脑软件修改GPS里面的记录文件就可以更改里程数,但前提是可以破解GPS的内部文件并进行修改。
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)