質問

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!

役に立ちましたか?

解決

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.

他のヒント

That's a lot of code (and I didn't read all of it), but I can tell you that the AsyncTask is not needed in this case. There's no need to "wait for updates in the background thread", that's why you use listeners. You also seem to have a lot of unnecessary flags and logic around the whole thing. My hunch is that you're disabling the listener before it has had a chance to update you.

I believe i would be simpler to just enable the location listener (not just be "one time"), wait for a "good enough" location, then just stop the listener. For all this you just need the reference to the listener to not be null (so you can unregister it when you're done).

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top