package
com.myeoa.test.gps.InterFace;
import
java.util.Timer;
import
java.util.TimerTask;
import
com.myeoa.test.gps.MainActivity;
import
android.content.Context;
import
android.location.Criteria;
import
android.location.Location;
import
android.location.LocationListener;
import
android.location.LocationManager;
import
android.os.Bundle;
import
android.os.Handler;
import
android.os.Message;
/**
* GPS定位服务类,封装了GPS类的细节,只向用户暴露简单的start()和stop()两个方法。需要用户实现{<a href="\"http://www./home.php?mod=space&uid=91636\"" target="\"_blank\"">@link</a> IGPSCallback}接口中的方法
* <p>
* 使用方法: <br>
* GPSService gpsService = new GPSService(gpsCallback, MainActivity.this, true);
* <br>
* gpsService.start();
*
* <a href="\"http://www./home.php?mod=space&uid=133757\"" target="\"_blank\"">@see</a> IGPSCallback
*
* @author 优化设计,QQ:50199907 Email:yw1530@126.com
*
*/
public
class
GPSService {
/**
* GPS函数回调接口
*/
private
IGPSCallback gpsCallback;
private
LocationManager loctionManager;
private
String provider;
/**
* GPSService是否运行
*/
private
boolean
isRun;
/**
* <a href="\"http://www./home.php?mod=space&uid=7300\"" target="\"_blank\"">@return</a> GPS状态,true为正在运行,false已停止。
*/
public
boolean
getIsRun() {
return
isRun;
}
/**
* 超时停止
*/
private
boolean
isAutoStop =
false
;
/**
* 定时器
*/
private
Timer timer;
/**
* 超时时间(秒)
*/
private
int
outTime =
180
;
/**
* 精度
*/
private
final
float
accuracy =
500
.0f;
/**
* GPS配置参数
*/
private
Criteria criteria;
/**
* @return 获取criteria
*/
public
Criteria getCriteria() {
initCriteria();
return
criteria;
}
/**
* 初始化GPS参数
*/
private
void
initCriteria() {
if
(criteria ==
null
) {
criteria =
new
Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(
false
);
criteria.setBearingRequired(
false
);
criteria.setCostAllowed(
true
);
criteria.setPowerRequirement(Criteria.POWER_LOW);
}
}
/**
* 最后一次错误描述
*/
private
String lastErrorDescription =
""
;
/**
* @return 获取GPSSerivce最后一次出错的描述
*/
public
String getError() {
return
lastErrorDescription;
}
/**
* 设置最后一次错误描述,该描述可以通过getError()方法获取。
*
* @see GPSService#getError()
*
* @param error
* 错误说明
*/
private
void
setError(String error) {
if
(error ==
null
)
return
;
this
.lastErrorDescription = error;
}
/**
* GPSService构造函数
*
* @param gpsCallback
* 回调函数接口
* @param context
* Context
*/
public
GPSService(IGPSCallback gpsCallback, Context context) {
super
();
this
.gpsCallback = gpsCallback;
loctionManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
initCriteria();
private
final
LocationListener locationListener =
new
LocationListener() {
@Override
public
void
onStatusChanged(String provider,
int
status, Bundle extras) {
}
@Override
public
void
onProviderEnabled(String provider) {
}
@Override
public
void
onProviderDisabled(String provider) {
}
@Override
public
void
onLocationChanged(Location location) {
if
(location !=
null
) {
if
(location.hasAccuracy() && location.getAccuracy() <= accuracy) {
if
(isAutoStop) {
stop();
}
gpsCallback.gpsCallback(location);
}
}
}
};
}