This article will show you how to programmatically access the data returned by your built-in GPS receiver.
In Android, location-based services are provided by the LocationManager class located in the android.location package.
Using the LocationManager class, we can obtain periodic updates of the device's geographical locations as well as fire an intent when it enters the proximity of a certain location.
1. Obtain a reference to the LocationManager class using the getSystemService() method.
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
2. Create a LocationListener Class for notify the location changes.
Our MyLocationListener class should implements the LocationListener abstract class. There are four methods that you need to override in this implementation:
* onLocationChanged(Location location): This method is called when the location has changed.
* onProviderDisabled(String provider): This method is called when the provider is disabled by the user.
* onProviderEnabled(String provider): This method is called when the provider is enabled by the user.
* onStatusChanged(String provider, int status, Bundle extras): This method is called when the provider status changes.
3. To be notified whenever there is a change in location, you need to register for a request for changes in locations so that your program can be notified periodically. This is done via the requestLocationUpdates() method (see Listing 1).
This method takes in four parameters:
* provider: The name of the provider with which you register
* minTime: The minimum time interval for notifications, in milliseconds.
* minDistance: The minimum distance interval for notifications, in meters.
* listener: An object whose onLocationChanged() method will be called for each location update.
4. Set the following User-Permission in androidmanifest.xml
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
Sample Code :
MainActivity.java
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new mylocationlistener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
}
private class mylocationlistener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
if (location != null) {
Log.d("LOCATION CHANGED", location.getLatitude() + "");
Log.d("LOCATION CHANGED", location.getLongitude() + "");
Toast.makeText(MainActivity.this,
location.getLatitude() + "" + location.getLongitude(),
Toast.LENGTH_LONG).show();
}
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
}
In Android, location-based services are provided by the LocationManager class located in the android.location package.
Using the LocationManager class, we can obtain periodic updates of the device's geographical locations as well as fire an intent when it enters the proximity of a certain location.
1. Obtain a reference to the LocationManager class using the getSystemService() method.
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
2. Create a LocationListener Class for notify the location changes.
Our MyLocationListener class should implements the LocationListener abstract class. There are four methods that you need to override in this implementation:
* onLocationChanged(Location location): This method is called when the location has changed.
* onProviderDisabled(String provider): This method is called when the provider is disabled by the user.
* onProviderEnabled(String provider): This method is called when the provider is enabled by the user.
* onStatusChanged(String provider, int status, Bundle extras): This method is called when the provider status changes.
3. To be notified whenever there is a change in location, you need to register for a request for changes in locations so that your program can be notified periodically. This is done via the requestLocationUpdates() method (see Listing 1).
This method takes in four parameters:
* provider: The name of the provider with which you register
* minTime: The minimum time interval for notifications, in milliseconds.
* minDistance: The minimum distance interval for notifications, in meters.
* listener: An object whose onLocationChanged() method will be called for each location update.
4. Set the following User-Permission in androidmanifest.xml
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
Sample Code :
MainActivity.java
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new mylocationlistener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
}
private class mylocationlistener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
if (location != null) {
Log.d("LOCATION CHANGED", location.getLatitude() + "");
Log.d("LOCATION CHANGED", location.getLongitude() + "");
Toast.makeText(MainActivity.this,
location.getLatitude() + "" + location.getLongitude(),
Toast.LENGTH_LONG).show();
}
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
}