Reputation: 23
I am working on an Android project for school. This activity is supposed to get the GPS location, and be able to save it. Then, later the user can come back to the app and get directions to that location from their current location.
Pretty new to Android dev, and the below is still a work in progress. I need to save the pinLat and pinLong after exiting the app so they are available when the app opens again.
Here's the current activity:
public class LocationActivity extends Activity implements LocationListener{
// declare variables
float locLat = 0;
float locLong = 0;
float pinLat = 0;
float pinLong = 0;
TextView txtLat;
TextView txtLong;
TextView txtPinLat;
TextView txtPinLong;
LocationManager locationManager;
LocationListener locationListener;
Location location;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location);
// get a handle on the text views
txtLat = (TextView) findViewById(R.id.txtLat);
txtLong = (TextView) findViewById(R.id.txtLong);
txtPinLat = (TextView) findViewById(R.id.txtPinLat);
txtPinLong = (TextView) findViewById(R.id.txtPinLong);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//Why cant I instantiate the LocationListener?
locationListener = new LocationListener();
}
public void onLocationChanged(Location location) {
//if it finds the location
if (location != null){
// save the values to float variables
locLat = (float) location.getLatitude();
locLong = (float) location.getLongitude();
// set the text views to the lat and long values
txtLat.setText(String.valueOf(locLat));
txtLong.setText(String.valueOf(locLong));
}
}
//save the current location to be a pin for the map
public void pinLocation(View v) throws IOException {
// when the button is pressed, save the current location lat and long
pinLat = locLat;
pinLong = locLong;
// set the text views to the lat and long values
txtPinLat.setText(String.valueOf(pinLat));
txtPinLong.setText(String.valueOf(pinLong));
}
//launch google maps to navigate to the pin
public void navigate(View v) {
//Toast.makeText(getApplicationContext(), "Navigate Called!", Toast.LENGTH_LONG).show();
//build the google maps URL
String uri = ("geo:"+ pinLat + "," + pinLong);
//add the lat and long
String query = pinLat + "," + pinLong;
//encode the URI
String encodedQuery = Uri.encode(query);
//continue building the uri
uri += "?q=" + encodedQuery;
//start the intent with the Uri
Intent navigate = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
//start the activity
startActivity(navigate);
}
//when the app resumes, turn on the GPS locations
public void onResume(){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
5,5, locationListener);
super.onResume();
}
//on pause, stop the GPS
@Override
public void onPause() {
locationManager.removeUpdates(locationListener);
super.onPause();
}
//GENERATED CODE. Need to alert if disabled etc
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
In the onCreate method I am getting "Cannot instantiate the type LocationListener" error on the locationListener = new LocationListener(); line. I have seen others use this exact same line in examples. What am I missing?
I have permissions in my AndroidManifest, and the layout shouldn't be causing any problems..
Thank you!
Upvotes: 1
Views: 1951
Reputation: 779
This is more of a general Java problem. LocationListener
isn't a Java class, it is an interface. In pretty plain terms, this means that there are no implementations of the methods (i.e., onLocationChanged
, onProviderEnabled
, etc.). This means that you must define what happens when these methods are invoked.
If you are familiar with handling Button
click events, then you already know how to write an implementation, using an Anonymous Inner Class. (An AIC is like implementing the interface 'on the fly').
LocationListener listener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
//TODO -- Barney says what should happen
// when the framework invokes onLocationChanged
}
//TODO -- Barney implements the other three interface methods
)}; //Close AIC declaration, implementation and terminate line.
Upvotes: 0
Reputation: 404
Since you already implements LocationListener
your activity is already the LocationListener, so you don't have to create a new LocationListener.
Remove all the instantiation around LocationListener locationListener;
And on your onResume method try changing the request update line to:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
onLocationChanged
should be trigger and you can get location out of it.
Upvotes: 1