2010. 4. 15. 11:47

android gps 관련 소스





import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import android.app.PendingIntent;
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.location.LocationProvider;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

public class ContentCreationService extends Service {

	/** Debugging Tag*/
	private static final String TAG = "SERVICE";
	
	private Bundle bundle = new Bundle();
	private String errorMessage;
	private String infoMessage = "";
	private Context context;
	private String[] location;
	private LocationManager lM;
	private LocationListener mLocationListener;
	private Condition condition;
	private final Lock lock = new ReentrantLock(); // Get the Monitor Lock
	
	private final IContent.Stub binder=new IContent.Stub() {
		
		/// Here are the methods you can use after binding the service
	    	};
	
	@Override
	public void onCreate() {
		super.onCreate();
		nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 
		Log.e(TAG,"Service onCreate done");
		context = this;
	}
	
	public void onStart(Intent intent,int startId) {
		Thread background = new Thread(new Runnable(){
			public void run(){		
				
				// Submit a message to the other threads handler in order to start the method "getLocation()"
				handler.sendEmptyMessage(1);
				
				// Initalizing the condition. (get it from the monitor) 
				condition = lock.newCondition(); 	
				// Lock this part
				lock.lock(); 
				try { 
				  Log.e(TAG,"Waiting for Location");
				  // Thread waits untill he gets the condition.signal() from the other thread
				  condition.await(); 
				} catch ( InterruptedException e ) { 
				  Log.e(TAG,"Thread got interrupted...");
				} 
				finally { 
				  // Unlock this part
				  lock.unlock(); 
				}

                               // Go on working ....

			}
        	
        });
	
		background.start();
		
		

	}

    Handler handler = new Handler(){
    	/** Ueberschreibt die handleMessage() des Handlers um Nachrichten die ankommen
    	 *  selbst zu verwalten. Wenn eine Nachricht angekommen ist wertet er den aktuellen Status, 
    	 *  des Service aus und zeigt eine Notification an. 
    	 */
		@Override
		public void handleMessage(Message msg) {
			if(msg.what == 1){
				getLocation();
			}

		}
    };



	@Override
	public IBinder onBind(Intent intent) {
		return(binder);
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		Log.e(TAG,"Service onDestroy done");

	}
	

	public void getLocation(){
		Log.e(TAG,"Started Location Search");
		lM = (LocationManager)context.getSystemService(context.LOCATION_SERVICE);	
		lM.setTestProviderStatus(lM.GPS_PROVIDER,LocationProvider.AVAILABLE, null, System.currentTimeMillis()); 
		lM.setTestProviderEnabled(lM.GPS_PROVIDER,true);
		mLocationListener = new MyLocationListener();
		lM.requestLocationUpdates(lM.GPS_PROVIDER, 0, 0, mLocationListener);

	}
	
	private class MyLocationListener implements LocationListener {

    	public void onLocationChanged(Location loc) {
            if (loc == null) {
                Log.e(TAG, "location changed to null");
            } else {
        		Log.e(TAG,"Location -> Lon: "+loc.getLongitude()+" Lat: "+loc.getLatitude());
        		location = new String[2];
        		location[0] = String.valueOf(loc.getLongitude());
        		location[1] = String.valueOf(loc.getLatitude());
                Log.e(TAG, "location changed : " + loc.toString());
                // Lock this part
                lock.lock(); 
                try { 
                  // Send the signal to the other thread to go on with its work, the location is now available
                  condition.signal(); 
                } 
                finally { 
                  // Unlock this part
                  lock.unlock(); 
                }
        		lM.removeUpdates(mLocationListener);
            }
        }


        public void onStatusChanged(java.lang.String arg0, int arg1, Bundle extras) {
            // ignore
        }

        public void onProviderEnabled(java.lang.String arg0) {
            // ignore
        }

        public void onProviderDisabled(java.lang.String arg0) {
            // ignore
        }
    }
	
}