GPS – Android
Download the source code here- working code
LocationManager class provides the access to know the location of the mobile. It will provide the current geo location to the user.
Here no need to instantiate the class directly, instead you can use Context.getSystemService(Context.LOCATION_SERVICE)
Android provides the android.location package which provides the API to determince the current geo position.
LocationProvider is a super class of different location providers, which will provide the current geo location.
We can get the location by using three methods,
Network - uses mobile network,wifi
Gps - uses mobile gps to find the location via satellites.
Passive - allows to participate for getting location on other components to save the power.
To use the gps service in our android mobile, we need to declare permission in manifest file:
If your getting location by gprs we need to declare
android.provider.Settings class provides to change the gps settings
LocationManager service = (LocationManager)getSystemService(LOCATION_SERVICE);
boolean enable = service.isProviderEnabled(LocationManager.GPS_PROVIDER);
We can check the gps enabled by using if, else conditions
enable the gps by going to mobilesettings.
If we use intents, we can directly goes to gps settings
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
Some constant methods:
public static final String GPS_PROVIDER
name of the gps provider. This will provide the location updates using satellites.
public static final String KEY_PROVIDER_ENABLED
it will hold the Boolean value when a provider enabled/disabled event is broadcast using a Pending Intent.
public static final String PROVIDERS_CHANGED_ACTION
Broadcast intent action when the configured location providers change.
Some public methods:
public GpsStatus getGpsStatus(GpsStatus status)
it will gives the information about current gps status.
Public LocationProvider getProvider (String name)
It gives the information associated with location provider of the given name
public Boolean isProviderEnabled(String provider)
Returns the current enabled/disabled status of the given provider.