通过GPS获取android 手机经纬度并每十分钟上传一次,地理位置解析是通过高德地图方法进行。由于需要长时间运行,写在服务中。
public class PushGPSService extends Service {
private LocationManager locationManager;
private GeocodeSearch geocodeSearch;
private LocationParam locationParam;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
getGPSLocation();
//地理搜索类,高德地图提供
geocodeSearch = new GeocodeSearch(this);
geocodeSearch.setOnGeocodeSearchListener(onGeocodeSearchListener);
return super.onStartCommand(intent, flags, startId);
}
private void getGPSLocation(){
locationManager = (LocationManager) Context.getSystemService(Context.LOCATION_SERVICE);
//判断GPS是否开启
if (locationManager != null){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10*60*1000, 0, locationListener);
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
//未打开操作
}
}
}
GeocodeSearch.OnGeocodeSearchListener onGeocodeSearchListener = new GeocodeSearch.OnGeocodeSearchListener() {
@Override
public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {
RegeocodeAddress regeocodeAddress = regeocodeResult.getRegeocodeAddress();//根据经纬度解析后的信息
LogWriter.write("push GPS Location locationParam");
//根据自己的接口使用。
}
@Override
public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {
}
};
LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
LatLng latLng = new LatLng(location.getLatitude(),location.getLongitude());
getLocationCity(location,latLng);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
switch (status){
//GPS状态为可见时
case LocationProvider.AVAILABLE:
break;
//GPS状态为服务区外时
case LocationProvider.OUT_OF_SERVICE:
break;
//GPS状态为暂停服务时
case LocationProvider.TEMPORARILY_UNAVAILABLE:
break;
}
}
//GPS开启时触发
@Override
public void onProviderEnabled(String provider) {//每次打开GPS进行一次上报
Location onProviderEnabled=locationManager.getLastKnownLocation(provider);
if (onProviderEnabled == null){ //部分机型获取上一次信息需要点时间
try {
Thread.sleep(5000);
onProviderEnabled=locationManager.getLastKnownLocation(provider);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//针对部分机型无法获取getLastKnownLocation问题,获取的值为null
if (onProviderEnabled == null){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10*60*1000, 0, locationListener);
}
if (onProviderEnabled != null){
LatLng latLng = new LatLng(onProviderEnabled.getLatitude(),onProviderEnabled.getLongitude());
getLocationCity(onProviderEnabled,latLng);
}
}
//GPS关闭时触发
@Override
public void onProviderDisabled(String provider) {
//关闭GPS时操作
}
};
@Override
public void onDestroy() {
locationManager.removeUpdates(locationListener);
super.onDestroy();
}
private void getAddressByLatlng(LatLng latLng) {
//逆地理编码查询条件:逆地理编码查询的地理坐标点、查询范围、坐标类型。
LatLonPoint latLonPoint = new LatLonPoint(latLng.latitude, latLng.longitude);
RegeocodeQuery query = new RegeocodeQuery(latLonPoint, 500f, GeocodeSearch.AMAP);
//异步查询
geocodeSearch.getFromLocationAsyn(query);
}
private void getLocationCity(Location location,LatLng latLng){
getAddressByLatlng(latLng);
}
}