0

I'm trying to get the current location and sending its LAt and LON to another class.But my application is not getting the current location it is showing the LAST LOCATION.Please help me out how to get the current location using GPS and NETWORK provider.

Below is the code:

public class location  implements LocationListener 
{
private LocationManager mgr;
private String best;
Location location;
public static double myLocationLatitude;
public static double myLocationLongitude;

 public void locUpdate(Context context)
 {

    mgr = (LocationManager) context.getSystemService(context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
   // criteria.setAccuracy(Criteria.ACCURACY_COARSE);

    best = mgr.getBestProvider(criteria, true);
     mgr.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER,0,0,this);

  try{
     Location location = mgr.getLastKnownLocation(best);
    Intent i = new Intent();
    i.setClass(context,sentsms.class);
     i.putExtra("latitude", location.getLatitude());
     i.putExtra("longitude", location.getLongitude());
     i.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);                     
     i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  

     // Launch the new activity and add the additional flags to the intent
     context.getApplicationContext().startActivity(i);
    }
    catch(Exception e)
    {

    }
 }  





public void onLocationChanged(Location location) {

    dumpLocation(location);
}

public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

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

}

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

}


protected void onPause() 
{

   // super.onPause();
    mgr.removeUpdates(this);
}

protected void onResume() {

   // super.onResume();
    mgr.requestLocationUpdates(best, 15000, 10, this);
}

private void dumpLocation(Location l) {

    if (l != null){

        myLocationLatitude = l.getLatitude();
        myLocationLongitude = l.getLongitude();
    }
}
} 
Yushi
  • 416
  • 6
  • 24

2 Answers2

0

From the docs for getLastKnownLocation():

Returns a Location indicating the data from the last known location fix obtained from the given provider.

This can be done without starting the provider. Note that this location could be out-of-date, for example if the device was turned off and moved to another location.

If the provider is currently disabled, null is returned.

This means getLastKnownLocation() will retrieve the Location based on the last fix of the Provider. If you want updated Locations, use the Location received in onLocationChanged()

Emmanuel
  • 13,083
  • 4
  • 39
  • 53
0

Please try this way

LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// Creating a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Getting the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
if (provider != null) {

location = locationManager.getLastKnownLocation(provider);
// locationManager.requestLocationUpdates(provider, 1000*60*60,
// 1000, this);
// update only in 1 hour and 1 km of distance
locationManager.requestLocationUpdates(provider, 100, 1, this);
}
Maveňツ
  • 1
  • 12
  • 50
  • 89