在从 MySQL 服务器提取数据之前,活动不会启动
我有一个 Activity
,它显示基于从 MySQL 服务器提取的数据的文本。问题是 Activity
在提取数据之前不会加载,有时需要很长时间甚至根本不会加载,同时用户会看到黑屏。
我尝试将从服务器获取数据的任务传递给服务,但它也等待拉取数据,然后才显示 Activity
的布局。
我还尝试创建一个具有固定文本的活动,然后调用从服务器提取数据的活动,但程序仍然等待数据。
你能想出一个有创意的解决方案吗?或者也可能是一个非创意的:)
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 :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以为此使用 asynctask:
http://developer.android.com/reference/ android/os/AsyncTask.html
或者您可以向用户显示一个等待对话框,直到您获取数据(在单独的线程中执行)......
或者您可以实现一个启动屏幕,您可以在那里获取数据……
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.....
您需要在另一个线程中执行此操作。尝试使用 AsyncTask 类。
You need to do it inside another thread. Try using AsyncTask class.
延迟可能是由于在主线程(也称为 UI 线程)上执行获取数据的调用所致。需要花费大量时间的进程,我的意思是即使是一两秒也应该在单独的线程中完成。 Android 提供了一个名为 AsyncTask 的类来帮助简化线程处理。
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.
您提到您尝试过一项服务,但您是否查看过 IntentService? (还无法链接它,但它位于 d.android.com 上。)我喜欢将它们用于此类任务,因为它们会为您处理线程(如 AsyncTask)并且可以更好地分离关注点。然后,IntentService 发送一条广播消息,该活动将接收该消息,指示数据可用或不可用。将数据本地存储在 sqlite 数据库中或作为 json/xml 文件。
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.