I would like to know if its possible to get the users location without the use of a LocationListener.
The reason why i ask is that my locationListener events are not being called.
public class LocationActivity extends Activity implements LocationListener{
private LocationManager m_locationManager;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout content = new LinearLayout(this);
content.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
content.setOrientation(LinearLayout.VERTICAL);
content.setBackgroundColor(Color.TRANSPARENT);
TextView infoLabel = new TextView(this);
infoLabel.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
infoLabel.setTextSize(20.0f);
infoLabel.setText("Initializing...");
content.addView(infoLabel);
try{
m_locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
String provider = null;
if ( m_locationManager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
provider = LocationManager.GPS_PROVIDER ;
Log.d("Unity", "Using GPS");
m_locationManager.requestLocationUpdates(provider, 0, 0, this);
} else if(m_locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
provider = LocationManager.NETWORK_PROVIDER;
Log.d("Unity", "Using Netword");
m_locationManager.requestLocationUpdates(provider, 0, 0, this);
} else {
Log.d("Unity", "Provider Not available");
}
}catch(Exception ex){
Log.d("Unity", "locatons error " + ex.getMessage());
}
setContentView(content);
}
public void onLocationChanged(Location location) {
Log.d("Unity", "UserLocation Lat:" + location.getLatitude() + " Long:" + location.getLongitude());
}
public void onStatusChanged(String provider, int status, Bundle extras) {Log.d("Unity", "onStatusChanged");}
public void onProviderEnabled(String provider) {Log.d("Unity", "onProviderEnabled");}
public void onProviderDisabled(String provider) {Log.d("Unity", "onProviderDisabled");}
}
See this post for more info:
Android: Can't create handler inside thread that has not called Looper.prepare()
Modified to reflect mmeyer comments
First, you can check the last known location when you select the provider with
LocationManager.getLastKnownLocation(provider)and see if it’s not null and not too old.Beyond that your code looks adequate. I suspect the issue might be that the 3rd param in your call to requestLocationUpdates says to only send updates if the location has changed by more than 100 meters. For most situations where youre running in debug and watching logcat, moving a hundred meters a second seems unlikely.
From the API docs: