0

friends,

i am using following code to get GPS location

it is causing two problems

1) when my location is changed / I move my phone from one area to another I dont get updated gps location(latitude/longitude)

2) after getting gps location my gps of phone is enabled how to disable it?

 double MyDeviceLatitude,MyDeviceLongitude;

public void GetGPSLocation()
{

 LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

                       LocationListener myLocationListener = new CurrentLocationListener(); 

                       locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30, 30, myLocationListener);

                   Location   currentLocation = locationManager.getLastKnownLocation("gps"); 

if(currentLocation != null)
                       {

                       MyDeviceLatitude = currentLocation.getLatitude();
                       MyDeviceLongitude = currentLocation.getLongitude();
}else
{

 currentLocation = locationManager.getLastKnownLocation("network");
 MyDeviceLatitude = currentLocation.getLatitude();
 MyDeviceLongitude = currentLocation.getLongitude();



}



public class CurrentLocationListener implements LocationListener{

            public void onLocationChanged(Location argLocation) 
            {
                if (argLocation != null) 
                {
                    MyDeviceLatitude = argLocation.getLatitude();
                    MyDeviceLongitude = argLocation.getLongitude();
                }

            }
            public void onProviderDisabled(String provider) {
            }

            public void onProviderEnabled(String provider) {
            }

            public void onStatusChanged(String provider, int status, Bundle arg2) {
            }
            }

}

any help would be appreciated.

Michael Eakins
  • 4,149
  • 3
  • 35
  • 54
UMAR-MOBITSOLUTIONS
  • 77,236
  • 95
  • 209
  • 278

1 Answers1

0

UMMA Do check this similar post that uses a Broadcast receiver thats seems to solve your problem Android: How to get location information from intent bundle extras when using LocationManager.requestLocationUpdates()

Community
  • 1
  • 1
100rabh
  • 6,156
  • 5
  • 27
  • 41
  • example is not complete so i cannot implement this :( – UMAR-MOBITSOLUTIONS Oct 29 '10 at 07:46
  • @UMMA you need to create a LocationReceiver (that extends BroadcastReceiver) & add that to your manifest as mentioned in the link.Do read guides on how to integrate this BroadcastReceiver with your Activity/Service. You may pass obtained location to your Activity/Service. – 100rabh Oct 29 '10 at 08:39