package com.example.Maps03;


import android.app.Service;
import android.content.BroadcastReceiver;
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.provider.Settings;

public class GPS_Service extends Service
{

	LocationListener listener; 
	LocationManager locationManager;
	
	@Override
	public IBinder onBind(Intent intent)
	{
		// TODO Auto-generated method stub
		return null;
	}

	
	
	public void onCreate()
	{
		
		listener = new LocationListener() 
		{
	        public void onLocationChanged(Location location) 
	        {
	        	Intent i = new Intent("location_update");
	        	i.putExtra("coordinates", location.getLongitude() + " " + location.getLatitude());
	            sendBroadcast(i);
	        }

			@Override
			public void onStatusChanged(String provider, int status, Bundle extras) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onProviderEnabled(String provider) 
			{
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onProviderDisabled(String provider) 
			{
				// TODO Auto-generated method stub
				Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
				startActivity(i);
			}
	       
			
	    };
		
	    
	      locationManager = (LocationManager)  getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
	      
	      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000,  0,  listener );
	        
	      
	}
	
	 @Override
	 public void onDestroy()
	 {
	        super.onDestroy();
	        if(locationManager != null)
	        	locationManager.removeUpdates(listener);
	    }
	
}
