package com.jqyd.app; import java.util.Date; import com.jqyd.pub.JqydDateUtil; import android.content.Context; import android.content.SharedPreferences; import android.location.Criteria; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.location.LocationProvider; import android.os.Bundle; import android.os.Looper; import android.util.Log; public class GpsThread extends Thread implements Runnable{ private LocationManager mLocationManager = null; private Location location = null; private String provider; private double longitude = 0.0; private double latitude = 0.0; private long time = 0; private boolean task = true; private MyApp myApp ; private static String TAG="GPSTHREAD"; private ShareMethod shareMethod = null; private SharedPreferences loationSharedFile =null; public IGPSLoc inf; public interface IGPSLoc{ public void getLoc(double lat,double lon,long time,int active); } public GpsThread(MyApp myApp, SharedPreferences loationSharedFile){ this.myApp = myApp; this.loationSharedFile = loationSharedFile; shareMethod = new ShareMethod(myApp); } public void setListener(IGPSLoc inf){ this.inf = inf; } private void updateWithNewLocation(Location slocation) { SharedPreferences.Editor editor = loationSharedFile.edit(); if (slocation != null) { if(inf!=null){ inf.getLoc(slocation.getLatitude(), slocation.getLongitude(), slocation.getTime(), 1); } task=false; Log.i(TAG, "GPS获取到位置,开始赋值--------------------------"); shareMethod.recordLog("GPS获取到位置,开始赋值--------------------------"+time); latitude = slocation.getLatitude(); longitude = slocation.getLongitude(); time = slocation.getTime(); location = slocation; editor.putString("lat",latitude+""); editor.putString("lon",longitude+""); editor.putLong("time", time); editor.putInt("active", 1); editor.commit(); }else{ if(inf!=null){ inf.getLoc(0, 0, 0, 0); } editor.putString("lat","0.0"); editor.putString("lon","0.0"); editor.putLong("time", time); editor.putInt("active", 0); editor.commit(); latitude = 0.0; longitude = 0.0; time = 0; location = null; } } private LocationListener locationListener = new LocationListener() { // 底层获得的位置会通过这个接口上报给应用 @Override public void onLocationChanged(Location location) { System.out.println("onLocationChanged"); shareMethod.recordLog("onLOcationChanged:_"+location); updateWithNewLocation(location); } // Provider被disable时触发此函数,比如GPS被关闭 @Override public void onProviderDisabled(String provider) { System.out.println("onProviderDisabled"); shareMethod.recordLog("onProviderDisabled:_"+location); stopGspListener(); } // Provider被enable时触发此函数,比如GPS被打开 @Override public void onProviderEnabled(String provider) { System.out.println("onProviderEnabled"); shareMethod.recordLog("onProviderEnabled:_"+location); updateWithNewLocation(location); } /* * 位置服务状态的变化通过这个接口上报 Provider的转态在可用、暂时不可用和无服务三个状态直接切换时触发此函数 */ @Override public void onStatusChanged(String provider, int status, Bundle extras) { System.out.println("onStatusChanged"); switch (status) { //GPS状态为可见时 case LocationProvider.AVAILABLE: Log.i(TAG, "当前GPS状态为可见状态"); shareMethod.recordLog("当前GPS状态为可见状态"); break; //GPS状态为服务区外时 case LocationProvider.OUT_OF_SERVICE: Log.i(TAG, "当前GPS状态为服务区外状态"); shareMethod.recordLog("当前GPS状态为服务区外状态"); break; //GPS状态为暂停服务时 case LocationProvider.TEMPORARILY_UNAVAILABLE: Log.i(TAG, "当前GPS状态为暂停服务状态"); shareMethod.recordLog("当前GPS状态为暂停服务状态"); break; default: break; } } }; /** * 移出gps监听器 */ public void stopGspListener() { //System.out.println("stopGspListener:"+locationListener); task = false; if (locationListener != null) { if(mLocationManager != null){ mLocationManager.removeUpdates(locationListener); } locationListener = null; location = null; System.out.println("locationListener:"+locationListener); } } @SuppressWarnings("static-access") public void run() { Looper.prepare(); Log.i(TAG, "*********启动gps调用线程,开始获取gps位置信息********"); shareMethod.recordLog("*********启动gps调用线程,开始获取gps位置信息********"); mLocationManager = (LocationManager) myApp.getSystemService(Context.LOCATION_SERVICE);// 此处实例化了 手机的本地位置服务 的一个对象 provider = mLocationManager.GPS_PROVIDER;// 获取provider对象 Log.i(TAG, "*******************GPS*******************"+mLocationManager); shareMethod.recordLog("*******************GPS*******************"+mLocationManager); mLocationManager.requestLocationUpdates(provider, 1000, 1,//设置location的更新频率 locationListener); while (task) { location = mLocationManager.getLastKnownLocation(provider);//获得location shareMethod.recordLog("通过getLastKnownLocation(provider)方法获得的location对象:"+location+"获取到的经纬度为:"+(location==null?"null":location.getLatitude())+","+(location==null?"null":location.getLongitude())); updateWithNewLocation(location); // 更新位置 while (location == null) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } location = mLocationManager.getLastKnownLocation(getProvider());//获得location shareMethod.recordLog("2通过getLastKnownLocation(+"+getProvider()+"+)方法获得的location对象:"+location+"获取到的经纬度为:"+(location==null?"null":location.getLatitude())+","+(location==null?"null":location.getLongitude())); updateWithNewLocation(location); // 更新位置 } } Looper.loop(); } private String getProvider(){ Criteria criteria = new Criteria(); // 获得最好的定位效果 criteria.setAccuracy(Criteria.ACCURACY_FINE); criteria.setAltitudeRequired(false); criteria.setBearingRequired(false); criteria.setCostAllowed(false); // 使用省电模式 criteria.setPowerRequirement(Criteria.POWER_LOW); String provider =mLocationManager.getBestProvider(criteria, true); return provider; } }