开发者

Activity won't start until data is pulled from MySQL server

I have an Activity that displays a text based on data pulled from MySQL server. The problem is that the Activity won't load until data is pulled, which sometimes takes some long seconds or even doesn't load at all, and in the meantime the users gets a black screen.

I tried to pass the mission of getting the data from the server to a service, but also it waits for pulling the data and only then shows the layout of the Activity.

I also tried to make an activity with fixed text and then call the Activity that pulls the data from the server, but still the program wait for the data.

Can you think on a开发者_开发百科 creative solution for it? or maybe a non-creative one as well :)


you can use asynctask for this:

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

or you can show a waiting dialog to user until you get your data(do it in separate thread).....

or you can implement a splash screen and there you can fetch data.....


You need to do it inside another thread. Try using AsyncTask class.


The delay is probably due to the call to fetch the data being done on the main thread, also called the UI thread. Processes which take any significant amount of time, and by that I mean even a second or two should be done in a seperate thread. Android provides a class called AsyncTask to help make threading painless.


You mention you tried a service but did you take a look at an IntentService? (Can't link it yet but it's on d.android.com.) I like using them for these kind of tasks cause they handle the threading for you (like an AsyncTask) and it separates concerns better. The IntentService then sends a broadcast message that the activity picks up indicating that the data is available or not. Store the data locally in a sqlite db or as a json/xml file.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜