Accessing GPS Information in Your Application
You need access the GPS location in any class of your application.
Add a class (say 'MyLocationListener') which implements 'LocationListener' interface. create an instance of this class where you want to access the GPS information and use it for the relevant requirement.
Below class 'MyLocationListener' implements 'LocationListener'
public class MyLocationListener implements LocationListener
public void onLocationChanged(Location loc)
/** The above 'Location' object 'loc' can be used for accessing GPS information, however it is not always possible
in an application to perform all the GPS information related tasks in this overridden method due to reasons like
for example in an application providing the information of shopping malls near the user's current location,
so app accesses the names of malls according to the user's location and displays them to user,on choosing one of them
app displays the different offers given by that mall.
In this example the location of the user decides the mall's name to be fetched from database through a
database handler which is private member of the class hosting the view to display list of mall,hence that database
handler can't be accessible in this overridden method hence this operation cannot be carried out.*/
public void onProviderDisabled(String provider)
public void onProviderEnabled(String provider)
public void onStatusChanged(String provider, int status, Bundle extras)
}// End of Class MyLocationListener.
Add the above class file in package of your application, its instance can be used as below for accessing GPS information in any class.
public class AccessGPS extends Activity
//declaration of required objects
protected void onCreate(Bundle savedInstanceState)
//instantiating objects for accessing GPS information.
mlocListener = new MyLocationListener();
//reuest for location updates
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
// Access the last identified location
lastKnownLocation = mlocManager.getLastKnownLocation(locationProvider);
// The above object can be used for accessing GPS data as below
// The above GPS data can be used for carrying out the operations specific to the location.