异步任务停止时终止应用程序(onDestroyed叫) [英] Async task stopped when App terminates (onDestroyed is called)

查看:179
本文介绍了异步任务停止时终止应用程序(onDestroyed叫)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我做服务器同步使用asyctask的子类后端服务器的活动。

由于它是一个网络任务,可能需要几秒钟。恐怕以下情形的发生。

活动开始,并随时根据的AsyncTask应该开始运行,它这样做。
上的prexecute()被调用,执行和结束。比doInBackground()被调用,并且这样做,但是,正在执行方法只是当,用户presses home键,并挥笔从最近的APPS应用程序。 (这ofcourse会导致应用程序终止和所有的onDestroy方法被调用的生存活动。(纠正我,如果我错了,就这一个))。

在我onPostExecute()方法,我在插入数据到数据库和更新的意见。
但是,由于应用程序被终止的onPostExecute()方法永远不会运行。

我的问题是:


  1. 当用户presses home键和失控的应用程序,并挥笔的应用程序,是doInBackground停止在那一刻?也就是说,它被切断在中间,不会继续它做什么?

  2. 碰巧我正要从服务器获取并把数据库里面的数据是什么?它是最好做把数据在doInBackground?
  3. 里面的分贝

解决方案

  1. AsyncTask的是一个后台任务将继续运行,即使当应用程序被关闭和的onDestroy()被调用。当任务进入 onPostExecute()并尝试更新它绑定到活动相关的任何意见,该活动已不存在的问题是。如果你担心这个,有一件事我所做的是保持在调用的活动,AsyncTask的一个引用,然后调用该方法myAsyncTaskReference。取消(真)取消它在的onDestroy()调用活动。


  2. 是的,我会把数据库操作中的 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 :

  1. When the user presses the home button and gets out of the app and swipes the app, is doInBackground halted at that moment ? that is, it is cut in the middle and does not continue what it does ?
  2. What happens to the data that I was going to get from the server and put inside the DB ? Is it advisable to do put the data in the db inside the doInBackground ?

解决方案

  1. 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.

  2. 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叫)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆