初始化:
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); // 位置
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
// TODO: Open GPS
} else {
String bestProvider = locationManager.getBestProvider(
getLocationCriteria(), true);
// 获取位置信息
// 如果不设置查询要求,getLastKnownLocation方法传人的参数为LocationManager.GPS_PROVIDER
Location location = locationManager
.getLastKnownLocation(bestProvider);
// 监听状态
locationManager.addGpsStatusListener(gpsStatusListener);
// 绑定监听,有4个参数
// 参数1,设备:有GPS_PROVIDER和NETWORK_PROVIDER两种
// 参数2,位置信息更新周期,单位毫秒
// 参数3,位置变化最小距离:当位置距离变化超过此值时,将更新位置信息
// 参数4,监听
// 备注:参数2和3,如果参数3不为0,则以参数3为准;参数3为0,则通过时间来定时更新