Here is the simple gps application to
provide the current location coordinate by just a simple toasy .The
app also notifies the gps is on or off when the application searches
for the coordinates. This application has one java activity which
includes all the classes and modules for this application.
Application inlcudes:
- location manager: This class provides access to the system location services. These services allow applications to obtain periodic updates of the device's geographical location.IncludesgetLastKnownLocation(String provider)
Returns a Location indicating the
data from the last known location fix obtained from the given
provider.
requestLocationUpdates(long
minTime, float minDistance, Criteria criteria, PendingIntent
intent)
Register for location updates using a
Criteria and pending intent.
GPS_PROVIDER feature find the
location using satellite. This feature requires uses permission
ACCESS_FINE_LOCATION
.
- Location Listener: Used for receiving notifications from the LocationManager when the location has changed.
import android.app.Activity;import android.content.Context;import android.location.Location;import android.location.LocationListener;import android.location.LocationManager;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;
public class Gps extends Activity {
private static final long UPDATES = 1;private static final long TIME_FOR_UPDATE = 1000;
protected LocationManager locationManager;
protected Button retrieveLocationButton;
@Overridepublic void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);setContentView(R.layout.activity_gps);
retrieveLocationButton = (Button) findViewById(R.id.retrieve_location_button);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,UPDATES,TIME_FOR_UPDATES,new MyLocationListener());
retrieveLocationButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {showCurrentLocation();}});
}
protected void showCurrentLocation() {
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {String message = String.format("Current Location \n Longitude: %1$s \n Latitude: %2$s",location.getLongitude(), location.getLatitude());Toast.makeText(Gps.this, message,Toast.LENGTH_LONG).show();}else{Toast.makeText(Gps.this,"Signal is unclear",Toast.LENGTH_LONG).show();}
}
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {String message = String.format("New Location \n Longitude: %1$s \n Latitude: %2$s",location.getLongitude(), location.getLatitude());Toast.makeText(Gps.this, message, Toast.LENGTH_LONG).show();}
public void onStatusChanged(String s, int i, Bundle b) {Toast.makeText(Gps.this, "Provider status changed",Toast.LENGTH_LONG).show();}
public void onProviderDisabled(String s) {Toast.makeText(Gps.this,"......Gps is disabled.....\n Please Enable",Toast.LENGTH_LONG).show();}
public void onProviderEnabled(String s) {Toast.makeText(Gps.this,"......Gps is enabled.....",Toast.LENGTH_LONG).show();}
}
}
onProviderEnabled is called when gps is enabled
onProviderDisable is called when gps is disabled
When button pressed the desired message appeared