title: GPS based Alarm Application in Android
body: <p>I want to create an application like: </p>
  
  <ol>
  <li>I am in city X</li>
  <li>I want to get notification(like ringing alarm or vibration etc) when I reach city Y.</li>
  </ol>
  
  <p>I already have sqlite database containing information(latitude, longitude) about many cities.</p>
  
  <p>I have searched Google and SO but didn't find proper solution.</p>
  
  <p>So far my research says this: </p>
  
  <p>What I have to do is to create a <code>Service</code> or <code>BroadcastReceiver</code> to get current location of the mobile phone using GPS.</p>
  
  <p>Correct me if I am going wrong.</p>
  
  <p>I want to get current position of mobile phone at an interval of 10mins. And based on that latitude longitude values I will compare that with the Scheduled city latitude longitude.</p>
  
  <p>If match occurs then it should give notification to user.</p>
  
  <p>Things that I have done: </p>
  
  <ol>
  <li><p>I know how to get current location of user. (But not by creating service. Thats what I wanted to know! )</p></li>
  <li><p>I am able to add tasks in sqlite database.</p></li>
  </ol>
  
  <p>Please guide me how can I approach for this application.  </p>
  
  <p>Starting it when the user launches the application: </p>
  
  <pre><code> Intent i = new Intent(context, MyLocationApps.class);     
   context.startService(i);
  </code></pre>
  
  <hr>
  
  <pre><code>public class MyLocationApps extends Service{
  
          LocationManager locMan;
          Location curLocation;
          Boolean locationChanged;
  
          LocationListener gpsListener = new LocationListener() {
          public void onLocationChanged(Location location) {
                  // Log.w("GPS", "Started");
                  if (curLocation == null) {
                          curLocation = location;
                          locationChanged = true;
                  }
  
                  if (curLocation.getLatitude() == location.getLatitude() &amp;&amp; curLocation.getLongitude() == location.getLongitude())
                          locationChanged = false;
                  else
                          locationChanged = true;
  
                  curLocation = location;
  
                  if (locationChanged)
                          locMan.removeUpdates(gpsListener);
  
          }
  
          public void onProviderDisabled(String provider) {
  
          }
  
          public void onProviderEnabled(String provider) {
                  // Log.w("GPS", "Location changed", null);
          }
  
          public void onStatusChanged(String provider, int status,
                          Bundle extras) {
                  if (status == 0)// UnAvailable
                  {
  
                  } else if (status == 1)// Trying to Connect
                  {
  
                  } else if (status == 2) {// Available
  
                  }
          }
  
  };
  
  
         @Override
         public void onCreate() {
                 Toast.makeText(getBaseContext(),"Inside onCreate of Service", Toast.LENGTH_LONG).show();
  
                 Log.e(TAG, "Inside onCreate of Service");
                 super.onCreate();
  
                 locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                 locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER,0, 0, gpsListener);
                 /*if (locMan.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                         locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER,20000, 1, gpsListener);
                 } else {
                         this.startActivity(new Intent("android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS"));
                 }
  */
                 if (curLocation != null) {
                         double lat = curLocation.getLatitude();
                         double lng = curLocation.getLongitude();
                         Toast.makeText(getBaseContext(),"Lat : " + String.valueOf(lat) + "\n Long : "+ String.valueOf(lng), Toast.LENGTH_LONG).show();
  
                 }
                 else
                 {
                     Toast.makeText(getBaseContext(),"Didn Get any location", Toast.LENGTH_LONG).show();
                 }
         }
  
          final String TAG="LocationService";
          @Override
         public int onStartCommand(Intent intent, int flags, int startId) {
                   Toast.makeText(getBaseContext(),"Inside onStartCommand of Service", Toast.LENGTH_LONG).show();
                   Log.e(TAG, "Inside onStartCommand of Service");
  
  
  
                   return super.onStartCommand(intent, flags, startId);
         }
         @Override
         public void onLowMemory() {
                 // TODO Auto-generated method stub
                 super.onLowMemory();
         }
  
  
            @Override
            public void onStart(Intent i, int startId)
            {
                Toast.makeText(getBaseContext(),"Inside onStart of Service", Toast.LENGTH_LONG).show();
               Log.e(TAG, "Inside onStart of Service");
  
  
        }
  
            public IBinder onBind(Intent arg0) {
                      Log.e(TAG, "Inside onBind of Service");
                       return null;
            }
  }
  </code></pre>
  
  <p><strong>Problem in code</strong></p>
  
  <p>When i run the application for the first time, it displays Toast Messages.
  But it never displays a Toast Message even i send location thru DDMS..</p>
  
  <p>Where i am going wrong can you please tell me ?</p>