title: android: how to get updated location with gps
body: <p>I'm making an app to send current location of user via sms after fixed time but it always send the same coordinates. I read lot of links but I don't know where is the mistake kindly tell me what is the problem in my code you can edit the code as u like</p>
  
  <pre><code>public class AlarmReceiver extends BroadcastReceiver implements LocationListener  {
  
  
      long time = 600* 1000; 
      long distance = 10; 
      boolean isGPSEnabled = false;
      boolean isNetworkEnabled = false;
      Location location;
      String device_id;
      String phoneNo = "+923362243969";
  
      @SuppressLint("NewApi") 
      @Override
      public void onReceive(Context context, Intent intent) {
  
          System.out.println("alarm receiver....");
          Intent service = new Intent(context, MyService.class);
          context.startService(service);
  
          //Start App On device restart
          if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
          Intent App = new Intent(context, MainActivity.class);
          App.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          context.startActivity(App);
          }
         TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
         device_id = tm.getDeviceId();  // returns IMEI number  
  
      try {
          LocationManager   locationManager = (LocationManager) context.getSystemService(context.LOCATION_SERVICE);
              // getting GPS status
              isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
              // getting network status
              isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
  
       if (isGPSEnabled) {
              if (location == null) {
                  locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,time,distance, this);
                  Log.d("GPS Enabled", "GPS Enabled");
              if (locationManager != null) {
                    location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                    if (location != null) {
                          location.getLatitude();
                          location.getLongitude();
  
                          String Text =  " From GPS: Latitude = " + location.getLatitude() +" Longitude = " + location.getLongitude() + " Device Id: " + device_id;
  
                          SmsManager smsManager = SmsManager.getDefault();
                          smsManager.sendTextMessage(phoneNo, null, Text, null, null);
                          Log.i("Send SMS", "");
                          this.abortBroadcast(); 
                          } 
  
                  } 
              }       
       }
                    else {
                          if (isNetworkEnabled) {
                              locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, time,distance, this);
                              Log.d("Network", "Network");
                              if (locationManager != null) {
                                  location = locationManager .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                                  if (location != null) {
                                      location.getLatitude();
                                      location.getLongitude();
  
                                      String Text =  " From Network: Latitude = " + location.getLatitude() +" Longitude = " + location.getLongitude() + " Device Id: " + device_id;
  
                                      SmsManager smsManager = SmsManager.getDefault();
                                      smsManager.sendTextMessage(phoneNo, null, Text, null, null);
                                      Log.i("Send SMS", "");
                                      this.abortBroadcast(); 
                                  }
                              }
                          }
  
                      }   
      } catch (Exception e) {
  
            Toast.makeText(context, "no connection", Toast.LENGTH_LONG).show();
            e.printStackTrace();
                          }     
       }
  
      @Override
      public void onLocationChanged(Location location) {
          // TODO Auto-generated method stub
  
      }
  
      @Override
      public void onProviderDisabled(String provider) {
          // TODO Auto-generated method stub
  
      }
  
      @Override
      public void onProviderEnabled(String provider) {
          // TODO Auto-generated method stub
  
      }
  
      @Override
      public void onStatusChanged(String provider, int status, Bundle extras) {
          // TODO Auto-generated method stub
  
      }
  
  }
  </code></pre>