Search code examples
androidandroid-asynctasklocationlistenerlooperdashclock

LocationListener onLocationChanged is not always called in DashClock extension


I have a DashClock extension that can be refreshed by clicking on it or by time interval so I'm using an AsyncTask. I also use a LocationListener since I need the user's location. Sometimes, onLocationChanged does not trigger causing my extension to not update, I worked on that for two weeks now trying different ways, but nothing worked like it should.

I have the whole code here.

onLocationChanged is on line 530

My AsyncTask is on line 290

The function that calls requestSingleUpdate is on line 197

I'm pretty sure it's a thread problem, but I don't know how to solve it...

If you need anymore details I'll be happy to provide them.

EDIT: I finally figured it out, it was my AsyncTask that died before the new location could be acquired, so I changed the weather update logic and it works now!


Solution

  • The problem may be related to this (from developer.android):

    When first introduced, AsyncTasks were executed serially on a single background thread. Starting with DONUT, this was changed to a pool of threads allowing multiple tasks to operate in parallel. Starting with HONEYCOMB, tasks are executed on a single thread to avoid common application errors caused by parallel execution.

    If you are trying to use multiple RefreshWeatherTasks at the same time you may run into problems. One way of solving that would be to overide the onPostExecute method and include some code that would guarantee that the AsyncTask was completed.