开发者

When should a (potentially) longer tasks take place?

开发者 https://www.devze.com 2023-02-10 00:01 出处:网络
I\'ve got 2 tabs in m开发者_JAVA技巧y app, one grabs my contacts and geocodes their postcodes, the other tab plots them on a map.

I've got 2 tabs in m开发者_JAVA技巧y app, one grabs my contacts and geocodes their postcodes, the other tab plots them on a map.

The geocoding process can be quite time consuming. What is the best practice for handling such length processes?

Should I have a loading bar when the app starts and do all of the geocoding then or should I force users to click a button to do the geocoding?


You should move any operation that takes more than about 200ms onto a separate thread, so the app doesn't lock up, and then from that thread update an indicator to show the user progress.

You need to learn about the AsyncTask class, it's absolutely central to writing responsive Android apps.

http://developer.android.com/reference/android/os/AsyncTask.html

It's a pretty straightforward wrapper than makes threading easy. Remember to STOP threads when they're not needed any more, e.g. in onPause().

I tend to put AsyncTask subclasses into their own class file (not as an inner class) and pass them an activity context as a constructor parameter, so the AsyncTask thread always has easy access to the activity to update the user interface (but NOT from doInBackground).

Some limitations of AsyncTask

http://foo.jasonhudgins.com/2010/05/limitations-of-asynctask.html

0

精彩评论

暂无评论...
验证码 换一张
取 消