Android 线程 &异步任务
我想在我的应用程序中实现线程的
第一个要求是:
每个活动都访问本地数据库,这需要一些时间来加载。所以我计划提供进度对话框。我想在线程中执行它。目前我使用AsynsTask
因为我不知道记录需要多长时间。除了AsynsTask
之外,我们如何使用Thread来实现?
多任务处理工具:
我想运行两个活动。一个在后台。IE 如果运行活动时有任何可用的上传(数据库 Syn Android 到 SQL Server),上传应该在后台开始。我们如何实现这一点?
请指导我
提前致谢
I want to implement the thread in my application
first requirement is:
Each activity access the local database that time it take some time to load.So I am planning to give progress-dialog.I want to do it in thread.Currently I did using the AsynsTask
because of I dodnt know how long will take for record. Other than AsynsTask
How we can implement using Thread?
Multitasking Facility :
I want to run two activity.One is in background.I.E If there are any upload available(Database Syn Android to SQL Server) while running activity ,Upload should start in background.How we can implement this?
Please guide me on this
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您想在后台同步数据库,所以我认为您必须使用
service
,其中您必须实现thread
并且必须在线程中编写代码。然后,您可以
安排服务启动时间
,并且当您想要保留启动服务的持续时间时,您可以重复您的服务
。对于上传,您必须选择
1) 使用
AysncTask
2) 使用带有线程的
Service
并且还要知道 Service 正在 main 中运行UI,所以如果你想使用服务来同步数据库,你必须实现线程
You want to synchronize your database in background so I think you have to use
service
in which you have to implementthread
and you have to write your code in thread.You can then
schedule your service startup time
and alsoyou can repeat your service
when ever you want to keep duration for start service.And for upload you have to options
1) Using
AysncTask
2) Using
Service with thread
And also know that Service is running in main UI so if you want to use service for synchronize database you have to implement thread
您可以在后台执行多个
AsyncTask
,但一次只能激活一个Activity
。AsyncTask
为您处理线程。you can execute many
AsyncTask
s in the background, but only oneActivity
can be active at a single time.AsyncTask
handles the threading for you.就您而言,
AysncTask
有一个好处,即当多用户(即多连接)连接到数据库时,AsyncTask
将串行执行。 (但是你应该注意到,这可能会随着android版本的改变而改变,例如:在Donut,他们会一起做,但在Honeycomb中,到现在为止,是串行的)In your case,
AysncTask
has one benefit, that when multi-user (other word, multi connection) connect to database,AsyncTask
will do in serial. (But you should notice, this might change through android version, for example: at Donut, they will do together, but in Honeycomb to now, serially)