package com.jiuqi.njt.util;
import com.jiuqi.njt.data.MyApp;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Looper;
public class GpsService extends Service {
private GpsThread gpsThread = null;
private MyApp myApp = null;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
}
/**
* 手机的GPS线程
*
* @author
*/
class GpsThread extends Thread implements Runnable {
private LocationManager mLocationManager;
private Location location;
private String provider;
private double longitude = 0.0;
private double latitude = 0.0;
private long time = 0;
private boolean task = true;
public GpsThread(boolean task1){
task = task1;
}
private void updateWithNewLocation(Location slocation) {
if (slocation != null) {
if(time == slocation.getTime()){
myApp.setActive(false);
myApp.setLat(0.0);
myApp.setLon(0.0);
myApp.setTime(0);
}else{
latitude = slocation.getLatitude();
longitude = slocation.getLongitude();
time = slocation.getTime();
location = slocation;
myApp.setActive(true);
myApp.setLat(latitude);
myApp.setLon(longitude);
myApp.setTime(time);
}
}else{
latitude = 0.0;
longitude = 0.0;
time = 0;
location = null;
}
}
private LocationListener locationListener = new LocationListener() {
// 底层获得的位置会通过这个接口上报给应用
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
// Provider被disable时触发此函数,比如GPS被关闭
public void onProviderDisabled(String provider) {
stopGspListener();
}
// Provider被enable时触发此函数,比如GPS被打开
public void onProviderEnabled(String provider) {
}
/*
* 位置服务状态的变化通过这个接口上报 Provider的转态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
*/
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
};
/**
* 移出gps监听器
*/
public void stopGspListener() {
//System.out.println("stopGspListener:"+locationListener);
if (locationListener != null) {
mLocationManager.removeUpdates(locationListener);
locationListener = null;
location = null;
System.out.println("locationListener:"+locationListener);
}
}
@SuppressWarnings("static-access")
public void run() {
Looper.prepare();
System.out.println("*********启动gps调用线程,开始获取gps位置信息********");
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);// 此处实例化了 手机的本地位置服务 的一个对象
provider = mLocationManager.GPS_PROVIDER;// 获取provider对象
mLocationManager.requestLocationUpdates(provider, 1000, 1,//设置location的更新频率
locationListener);
while (task) {
location = mLocationManager.getLastKnownLocation(provider);//获得location
updateWithNewLocation(location); // 更新位置
while (location == null) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
location = mLocationManager.getLastKnownLocation(provider);//获得location
updateWithNewLocation(location); // 更新位置
}
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Looper.loop();
}
};
@Override
public void onStart(Intent intent, int startId) {
myApp = (MyApp) this.getApplication();
gpsThread = new GpsThread(true);
gpsThread.start();
super.onStart(intent, startId);
}
@Override
public void onDestroy() {
gpsThread.stopGspListener();
gpsThread.stop();
super.onDestroy();
}
}