Android长更新活动
我的应用程序中有一个按钮可通往 UpdateActivity。此活动不显示任何内容,只是显示一个进度条,并且有一个正在运行更新的 AsyncTask。
AsyncTask 需要相当长的时间,因为我连接到服务器并从许多表中检索信息并将它们插入到 sqlite 数据库中并将数据从本地数据库发送到服务器。
我想知道使用服务是否不是更好?是吗?另一方面我也有疑问。当我按下后退按钮时,我离开该活动,但 AsyncTask 似乎在后面运行(因为我有一个祝酒词来通知用户工作已完成,并且它显示得很晚(即使我已经离开该活动!)。
然后我有另一个活动需要从数据库读取和写入数据,所以如果我这样做,而另一个线程(下载线程)也搞乱了数据库,是否可能出现问题?有什么方法可以防止这种情况发生? 提前致谢
I have a button in my app which leads to the UpdateActivity. This activity doesn't show anything just a progress bar and there is an AsyncTask running with the updates.
The AsyncTask takes quite a long time because I conect to a server and retrieve info from many tables and inserting them in the sqlite database and sending data from local database to the server.
I am wondring if it's not better to use a service for this?? is it? On the other hand I have a doubt. When I press the back button I leave that activivty but the AsyncTask seems to be running in the back (because I have a toast to inform the user the job's done and it shows much later (even if I have left that activity!).
Then I have another activity which requires data to be read and written to/from the db so if I am doing that while another thread (the downloading thread) is messing up with the database too, is it possible that something goes wrong? Is there a way to prevent that?
thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于任何长时间运行的操作来说,在服务中运行任务无疑是一个好主意
将比您的 Activity 更长久。
目前您的线程在离开活动后继续运行,
但在任何时候,当系统杀死进程时,该线程都可能被杀死。
另外,向用户呈现已经存在且已消失的 Activity 的 UI 对于用户体验来说并不好。
最好的办法是在服务中运行 AsyncTask 并使用 状态栏通知。这样用户就可以获得进度的更新
当他们需要的时候。
带有状态通知的服务被视为长时间运行,系统不会终止
进程(以及您的线程),除非您的内存确实不足。
至于并发访问数据库的问题,您可能应该编写一个 ContentProvider 来控制对数据库的访问。内容提供者必须是
线程安全,并将在应用程序的任何部分提供对数据的统一访问
如果您希望导出数据,甚至可以供其他应用程序使用。
Running your task in a service is defiantly a good idea for any long running operation that
will outlive your Activity.
At the moment your thread continues to run after leaving the activity,
but at any point that thread could be killed when the system kills the process.
Also presenting the user with UI from an Activity that has been and gone is not good for the UX.
The best thing to do is run your AsyncTask in a service and display status updates using a status bar notification. This way the user can get an update of the progress as and
when they need to.
A service with a status notification is seen as long running and the system won't kill
the process (and therefore your thread) unless you are really low on memory.
As to the question of concurrent access to a database, you should probably write a ContentProvider to control access to your database. ContentProviders must be
thread safe and will provide uniform access to your data within any part of your app
and can even be used by other apps should you wish to export your data.
如果您担心用户在同步运行时对数据进行更改,那么将此作为一项服务并不会真正帮助您。您可以做的是在 Activity onDestroy() 方法中(优雅地)终止 AsyncTask,这样,如果用户单击后退箭头,任务将不会继续运行。
If you're concerned about the user making changes to the data while the sync is running, then making this a service won't really help you. What you can do is kill the AsyncTask (gracefully) in the Activities onDestroy() method, so that if a user clicks the back arrow, the task won't continue to run.