Files
jlengrand.github.io/_posts/2013-10-26-onlocationchanged-is-never-called-on-android.markdown
julien lengrand-lambert 64922f32be Highlights XML code
2014-02-09 17:22:09 +01:00

4.9 KiB

layout, status, published, title, author, author_login, author_email, author_url, wordpress_id, wordpress_url, date, categories, tags, comments
layout status published title author author_login author_email author_url wordpress_id wordpress_url date categories tags comments
post publish true onLocationChanged is never called on Android Julien Lengrand-Lambert jlengrand julien@lengrand.fr http://www.lengrand.fr 892 http://www.lengrand.fr/?p=892 2013-10-26 16:28:40.000000000 +02:00
Uncategorized
programming
android
programming
brestram
java
gps
id author author_email author_url date date_gmt content
21831 Robert robertbobrek@gmail.com MjAxMy0xMi0xMiAyMTo0NzozNyArMDEwMA== MjAxMy0xMi0xMiAyMDo0NzozNyArMDEwMA== Hi, thanks for the code but it is not working... private LocationInfo li; gives me: "LocationInfo can not be resolved to a type"
id author author_email author_url date date_gmt content
21886 Julien Lengrand-Lambert julien@lengrand.fr http://www.lengrand.fr MjAxMy0xMi0xNCAxMDo0OToyOSArMDEwMA== MjAxMy0xMi0xNCAwOTo0OToyOSArMDEwMA== Hey, You are totally right, sorry for that. LocationInfo is a class I use internally. I removed the references to LocationInfo, so things should be ok now :). Thanks for noticing!

I had problems with this while developing #BresTram.

I was developing a new feature, allowing my users to find bus stops nearby using their GPS location.

But whatever I was trying, my location was never set, and onLocationChanged was never called.

Here is what my Activity would look like :

{% highlight java %}

public class DisplayGPSInfoActivity extends BaseActivity implements LocationListener { private static final String TAG = "DisplayGPSInfoActivity";

private ViewFlipper vf;

private LocationManager locationManager; private String provider;

@SuppressLint("NewApi") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

// Initiating ViewFlipper setContentView(R.layout.activity_display_gpsinfo_request); vf = (ViewFlipper) findViewById(R.id.viewFlipper);

this.locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

//Choosing the best criteria depending on what is available. Criteria criteria = new Criteria(); provider = locationManager.getBestProvider(criteria, false); //provider = LocationManager.GPS_PROVIDER; // We want to use the GPS

// Initialize the location fields Location location = locationManager.getLastKnownLocation(provider); }

@Override public void onLocationChanged(Location location) { Log.d(TAG, "GPS LocationChanged"); double lat = location.getLatitude(); double lng = location.getLongitude(); Log.d(TAG, "Received GPS request for " + String.valueOf(lat) + "," + String.valueOf(lng) + " , ready to rumble!");

// Do clever stuff here } }

{% endhighlight %}

You can forget about the ViewFlipper, that is here only to show something to the user. Basically, I am letting android decide which provider he wants to use (GPS or Network), and request for the last known location. Then, I want to do something clever each time onLocationChanged is called.

Problem is, it is not. never. Ever. . .

After having verified hundred times that I had

{% highlight xml %}

{% endhighlight %}

correctly defined in my manifest, and that yes, other GPS based apps were working fine on my phone; I finally found the solution.

As stupid as it seems, I had forgotten to request the updates. . . Something like that would do the job :

{% highlight java %}

@Override protected void onResume() { super.onResume(); Log.v(TAG, "Resuming"); locationManager.requestLocationUpdates(provider, 400, 1, this); }

{% endhighlight %}

I was somehow expecting that is was automatic, implied by the fact that my activity implements LocationListener.

Well it is not. So, if any of you has the same problem, look whether you actually ask for something before getting angry because you don't receive it :D.

Have fun hacking around. Oh, and if you leave in Brest, give a shot to #BresTram; it is awesome!