package com.jqyd.shareInterface; import com.jqyd.app.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; import android.os.PowerManager; import android.os.PowerManager.WakeLock; public class GpsService extends Service { private GpsThread gpsThread = null; private MyApp myApp = null; private WakeLock wakeLock = 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(); System.out.println("******************onCreate*********************"); } /** * 手机的GPS线程 * * @author */ 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; 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{ System.out.println("GPS获取到位置,开始赋值--------------------------"); 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) { System.out.println("onLocationChanged"); } // Provider被disable时触发此函数,比如GPS被关闭 public void onProviderDisabled(String provider) { System.out.println("onProviderDisabled"); stopGspListener(); } // Provider被enable时触发此函数,比如GPS被打开 public void onProviderEnabled(String provider) { System.out.println("onProviderEnabled"); updateWithNewLocation(location); } /* * 位置服务状态的变化通过这个接口上报 Provider的转态在可用、暂时不可用和无服务三个状态直接切换时触发此函数 */ public void onStatusChanged(String provider, int status, Bundle extras) { System.out.println("onStatusChanged"); } }; /** * 移出gps监听器 */ public void stopGspListener() { //System.out.println("stopGspListener:"+locationListener); 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(); System.out.println("*********启动gps调用线程,开始获取gps位置信息********"); mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);// 此处实例化了 手机的本地位置服务 的一个对象 provider = mLocationManager.GPS_PROVIDER;// 获取provider对象 System.out.println("*******************GPS*******************"+mLocationManager); 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) { // TODO Auto-generated method stub System.out.println("******************onStart*********************"); myApp = (MyApp) this.getApplication(); // 保持手机在待机的状态下service依然能够正常运行 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,GpsService.class.getName()); wakeLock.acquire(); gpsThread = new GpsThread(true); gpsThread.start(); super.onStart(intent, startId); } @Override public void onDestroy() { // TODO Auto-generated method stub System.out.println("----------------关闭GPS搜索----------------"); gpsThread.stopGspListener(); //连续上报--节省电量,释放资源 if (wakeLock != null) {// 保持深度休眠,在service停止运行的时候,释放wakelock锁 wakeLock.release(); wakeLock = null; } //gpsThread.stop(); super.onDestroy(); } }