I am having trouble implementing a LocationListener
.
import androidx.appcompat.app.AppCompatActivity;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
TextView latitudeTV;
TextView longitudeTV;
TextView altitudeTV;
TextView locationNameTV;
double latitude;
double longitude;
double altitude;
String locationName;
LocationManager locationManager;
Location location;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
latitudeTV = (TextView) findViewById(R.id.latitude);
longitudeTV = (TextView) findViewById(R.id.longitude);
altitudeTV = (TextView) findViewById(R.id.altitude);
locationNameTV = (TextView) findViewById(R.id.locationName);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 0, locationListener);
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
onLocationChanged(location);
LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
longitude = location.getLongitude();
latitude = location.getLatitude();
altitude = location.getAltitude();
locationName = "Placeholder";
longitudeTV.setText("Longitude: \n" + longitude + "°");
latitudeTV.setText("Latitude: \n" + latitude + "°");
altitudeTV.setText("Altitude: \n" + altitude + "m");
locationNameTV.setText("Location: \n" + locationName);
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
};
}
}
I'm not sure if it's a syntax issue or a scoping issue, but I keep getting "unable to resolve symbol" errors. It's acting like I haven't implemented the LocationListener
methods. I have tried adding implements LocationListener
to MainActivity
and that shows an error saying I have not implemented the LocationListener
methods. I'm not sure where my mistake is.
EDIT:
I've changed my code per your answers. When I try to implement //locationManager.requestLocationUpdates(Context.LOCATION_SERVICE, 5000, 0, this);
I get the following when trying to launch the app: LocationTestApp keeps stopping
. I've commented out that line, and the app works again.
public class MainActivity extends AppCompatActivity implements LocationListener {
TextView latitudeTV;
TextView longitudeTV;
TextView altitudeTV;
double latitude;
double longitude;
double altitude;
LocationManager locationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
latitudeTV = (TextView) findViewById(R.id.latitude);
longitudeTV = (TextView) findViewById(R.id.longitude);
altitudeTV = (TextView) findViewById(R.id.altitude);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
//locationManager.requestLocationUpdates(Context.LOCATION_SERVICE, 5000, 0, this);
Location location = locationManager.getLastKnownLocation(locationManager.NETWORK_PROVIDER);
onLocationChanged(location);
}
@Override
public void onLocationChanged(Location location) {
longitude = location.getLongitude();
latitude = location.getLatitude();
altitude = location.getAltitude();
longitudeTV.setText("Longitude: \n" + longitude + "°");
latitudeTV.setText("Latitude: \n" + latitude + "°");
altitudeTV.setText("Altitude: \n" + altitude + "m");
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
}
A LocationListener
class, in this case an Activity, should be defined as below:
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
public class MainActivity extends AppCompatActivity implements LocationListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 0, this); // Note: You pass this, so the overridden methods
}
@Override
public void onLocationChanged(Location location) {
// Do your stuff here
}
@Override
public void onProviderDisabled(String provider) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
}
And this is the solution in jle's comments.
But if you want to use the locationListener
that you've declared in your onCreate, you should declare it before using it, in this way:
...onCreate(...) {
...
LocationListener locationListener = new LocationListener() {...} // Define this...
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 0, locationListener); // ...and then call this
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
onLocationChanged(location);
}