我有我做服务器同步使用asyctask的子类后端服务器的活动。
由于它是一个网络任务,可能需要几秒钟。恐怕以下情形的发生。
活动开始,并随时根据的AsyncTask应该开始运行,它这样做。上的prexecute()被调用,执行和结束。比doInBackground()被调用,并且这样做,但是,正在执行方法只是当,用户presses home键,并挥笔从最近的APPS应用程序。 (这ofcourse会导致应用程序终止和所有的onDestroy方法被调用的生存活动。(纠正我,如果我错了,就这一个))。
在我onPostExecute()方法,我在插入数据到数据库和更新的意见。但是,由于应用程序被终止的onPostExecute()方法永远不会运行。
我的问题是:
AsyncTask的是一个后台任务将继续运行,即使当应用程序被关闭和的onDestroy()被调用。当任务进入 onPostExecute()并尝试更新它绑定到活动相关的任何意见,该活动已不存在的问题是。如果你担心这个,有一件事我所做的是保持在调用的活动,AsyncTask的一个引用,然后调用该方法myAsyncTaskReference。取消(真)取消它在的onDestroy()调用活动。
是的,我会把数据库操作中的 doInBackground()方法,如在一个单独的线程在后台运行,并且不需要引用到应用程序的活动。
你有没有考虑使用服务这一类型,而不是嘛?我会强烈建议的 IntentService ,这是服务的一个子类是专门设计用于在后台执行一个任务(类似AsyncTask的),然后杀了自己一次完成。这是很容易实现的,通常只涉及压倒一切的一种方法 - onHandleIntent() - 在服务启动时被称为
它可以处理所有的数据库操作,它有没有提到一个活动,所以这个问题你担心在#1将永远不会发生。如果你需要更新一个视图,你可以有一旦它完成了IntentService生成广播,你的活动可以为广播注册并在收到相应的它更新它的意见。如果你的活动不再身边,一旦广播发送那么它并不重要,什么都不会失败。
I have an activity in which I do server sync with a back end server using a subclass of asyctask.
Given it is a network task and might take couple of seconds. I am afraid of the following scenario to take place.
The activity starts, and whenever the asynctask should start to run, it does so. The onPrexecute() is called, executed, and over. Than the doInBackground() is called, and is done so, however, just when the method is being executed, the user presses the home button and swipes the app from the RECENT APPS. (this ofcourse causes the app to terminate and all the onDestroy methods get called of the alive activities..(Correct me if I'm wrong on this one)).
In my onPostExecute() method, I am inserting the data to DB and updating the VIEWs. But since the app is 'terminated' the onPostExecute() method never runs.
my questions are :
解决方案
AsyncTask is a background task it will continue to run even if the app is closed and onDestroy() is called. The problem would be when the task enters onPostExecute() and tries to update any views associated with the activity it is bound to, as that activity no longer exists. If you're concerned about this, one thing I've done is to keep a reference to that AsyncTask in the calling activity, and then call the method myAsyncTaskReference.cancel(true) to cancel it in the onDestroy() of the calling activity.
Yes, I would put the DB operations in the doInBackground() method, as that runs in the background on a separate thread, and doesn't require a reference to the app activity.
Have you considered using a service for this type of thing instead? I would strongly recommend an IntentService, which is a subclass of service which is specifically designed to perform a single task in the background (similar to AsyncTask), and then kill itself once completed. It's quite easy to implement and usually only involves overriding one method - onHandleIntent() - which is called when the service starts.
It can handle all your DB operations and it has no reference to an activity and so the problem you're worried about in #1 would never occur. If you need to update a view, you can have your IntentService generate a broadcast once it's completed, and your Activity can register for that broadcast and update it's views accordingly when it receives it. And if your activity is no longer around once the broadcast is sent then it doesn't matter and nothing will fail.
更多推荐
异步任务停止时终止应用程序(onDestroyed叫)
发布评论