package peng.zhang.mobilesafe01.servicer;

import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

public class GPSService extends Service {
	private LocationManager lm;
	private MyLocationListener listener;

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		lm=(LocationManager) getSystemService(LOCATION_SERVICE);
		/**
		 * List<String> providers=lm.getAllProviders();
		 for(String pv :providers){
			Log.d(TAG, pv);
		 }
		 */
		
		//ÎªÄÚÈÝÌá¹©ÕßÉèÖÃÌõ¼þ
				Criteria criteria=new Criteria();
				criteria.setAccuracy(Criteria.ACCURACY_FINE);
		//ÉèÖÃ²ÎÊýÏ¸»¯£º
		//criteria.setAccuracy(Criteria.ACCURACY_FINE);//ÉèÖÃÎª×î´ó¾«¶È 
		//criteria.setAltitudeRequired(false);//²»ÒªÇóº£°ÎÐÅÏ¢ 
		//criteria.setBearingRequired(false);//²»ÒªÇó·½Î»ÐÅÏ¢ 
		//criteria.setCostAllowed(true);//ÊÇ·ñÔÊÐí¸¶·Ñ 
		//criteria.setPowerRequirement(Criteria.POWER_LOW);//¶ÔµçÁ¿µÄÒªÇó
		//ÉèÖÃÎ»ÖÃ¸üÐÂ¼àÌýÆ÷£¬ÐèÒª´«Èë²ÎÊýÎª¶¨Î»·½Ê½¡¢¸üÐÂÊ±¼ä£¨Ò»°ãÒ»·ÖÖÓ£©£¬¸üÐÂ¾àÀë£¨Ò»°ã50Ã×£©£¬¼àÌýÆ÷
	
		//ÉèÖÃ»ñÈ¡×îºÃµÄÎ»ÖÃÌá¹©Æ÷
		listener =new MyLocationListener();
		String provider=lm.getBestProvider(criteria, true);
		lm.requestLocationUpdates(provider,0, 0, listener);
		Log.d("Location", "Location0"+"Æô¶¯·þÎñ");
	}
	//·þÎñÏú»Ù£¬È¡Ïû×¢²á
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		lm.removeUpdates(listener);
		listener=null;
	}
	
	class  MyLocationListener implements LocationListener{

		/**
		 * µ±Î»ÖÃ¸Ä±äµÄÊ±ºò»Øµ÷
		 */
		@Override
		public void onLocationChanged(Location location) {
			Log.d("Location", "Location1"+"MyLocationListenerÖ´ÐÐÁË");
			String longitude = "jj:" + location.getLongitude() + "\n";
			String latitude = "w:" + location.getLatitude() + "\n";
			String accuracy = "a" + location.getAccuracy() + "\n";
			Log.d("Location", "Location1"+longitude);
			//½«±ê×¼×ø±ê×ª»»Îª»ðÐÇ×ø±ê
			//ÊµÀý»¯ModifyOffsetÐèÒªÊäÈëÒ»¸öÊäÈëÁ÷£¬Õâ¸öÊäÈëÁ÷¼ÈÊÇ¶ÁÈ¡Êý¾Ý¿âÁ÷
//			InputStream in = null;
//			try {
//				in=getResources().getAssets().open("axisoffset.dat");
//				ModifyOffset offset=ModifyOffset.getInstance(in);
//				//½«±ê×¼×ø±ê×ª»»Îª»ðÐÇ×ø±ê
//				PointDouble double1=offset.s2c(new PointDouble(location.getLongitude(), location.getLatitude()));
//				//×ª»»Íê³É£¬È¡³ö×ø±ê
//				longitude ="j:" + offset.X+ "\n";
//				latitude =  "w:" +offset.Y+ "\n";
//			} catch (IOException e) {
//				e.printStackTrace();
//			} catch (Exception e) {
//				e.printStackTrace();
//			}
			//¶ÔµÃµ½µÄ¾­Î³¶È½øÐÐ±£´æ
			SharedPreferences sp=getSharedPreferences("config", MODE_PRIVATE);
			Editor editor=sp.edit();
			editor.putString("lastlocation", longitude + latitude + accuracy);
			editor.commit();
		}
		/**
		 * µ±×´Ì¬·¢Éú¸Ä±äµÄÊ±ºò»Øµ÷ ¿ªÆô--¹Ø±Õ £»¹Ø±Õ--¿ªÆô
		 */
		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
			// TODO Auto-generated method stub
			
		}

		/**
		 * Ä³Ò»¸öÎ»ÖÃÌá¹©Õß¿ÉÒÔÊ¹ÓÃÁË
		 */
		@Override
		public void onProviderEnabled(String provider) {
			
		}

		/**
		 * Ä³Ò»¸öÎ»ÖÃÌá¹©Õß²»¿ÉÒÔÊ¹ÓÃÁË
		 */
		@Override
		public void onProviderDisabled(String provider) {
			// TODO Auto-generated method stub
			
		}
	}
		

}
