package com.jqyd.android.module.lbs;
import com.jqyd.android.module.lbs.util.WriteFile;
import android.content.Context;
import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
class GpsThread extends Thread {
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 Context myApp;
private static String TAG = "GPSTHREAD";
private WriteFile shareMethod = null;
private SharedPreferences loationSharedFile = null;
private LocationListener locationListener;
public GpsThread(Context myApp, SharedPreferences loationSharedFile) {
this.myApp = myApp;
this.loationSharedFile = loationSharedFile;
shareMethod = new WriteFile("GpsLog");
initLintener();
}
private void initLintener() {
locationListener = new LocationListener() {
// 底层获得的位置会通过这个接口上报给应用
@Override
public void onLocationChanged(Location location) {
System.out.println("onLocationChanged");
updateWithNewLocation(location); // 更新位置
}
// Provider被disable时触发此函数,比如GPS被关闭
@Override
public void onProviderDisabled(String provider) {
System.out.println("onProviderDisabled");
stopGspListener();
}
// Provider被enable时触发此函数,比如GPS被打开
@Override
public void onProviderEnabled(String provider) {
System.out.println("onProviderEnabled");
updateWithNewLocation(location);
}
/*
* 位置服务状态的变化通过这个接口上报 Provider的转态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
*/
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
System.out.println("onStatusChanged");
}
};
}
private void updateWithNewLocation(Location slocation) {
SharedPreferences.Editor editor = loationSharedFile.edit();
if (slocation != null) {
Log.i(TAG, "GPS获取到位置,开始赋值--------------------------");
shareMethod.writeToFile("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();
//应该可以获取到文字信息,待测试
/*try {
StringBuffer sb = new StringBuffer();
List<Address> fromLocation = new Geocoder(myApp).getFromLocation(latitude, longitude, 3);
for(int i=0;i<fromLocation.get(0).getMaxAddressLineIndex();i++){
sb.append(fromLocation.get(0).getAddressLine(i));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
} else {
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;
}
}
/**
* 移出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.writeToFile("*********启动gps调用线程,开始获取gps位置信息********");
mLocationManager = (LocationManager) myApp.getSystemService(Context.LOCATION_SERVICE);// 此处实例化了 手机的本地位置服务
// 的一个对象
provider = mLocationManager.GPS_PROVIDER;// 获取provider对象
Log.i(TAG, "*******************GPS*******************"+ mLocationManager);
shareMethod.writeToFile("*******************GPS*******************"+ mLocationManager);
mLocationManager.requestLocationUpdates(provider, 1000, 1,// 设置location的更新频率
locationListener);
while (task) {
location = mLocationManager.getLastKnownLocation(provider);// 获得location
updateWithNewLocation(location); // 更新位置
while (location == null) {
if(locationListener!=null){
mLocationManager.requestLocationUpdates(provider, 1000, 1,// 设置location的更新频率
locationListener);
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {}
location = mLocationManager.getLastKnownLocation(provider);// 获得location
updateWithNewLocation(location); // 更新位置
}
}
Looper.loop();
}
}