管理后台下载:Android [英] Managing background download : Android

查看:74
本文介绍了管理后台下载:Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一个新闻应用程序,每当用户打开我的应用程序时,我都需要下载新鲜的文章及其详细的故事.我正在做所有这些后台线程.我主要关注的是后台线程应在用户退出应用后立即停止,以防止用户产生额外的下载费用.

I'm designing a news app where I need to download fresh articles and their detailed stories whenever user opens my app. I'm doing all of this a background thread. My prime focus was that the background thread should stop as soon as user exits the app so as to prevent user for incurring extra download charges.

要实现这一目标,我会在初始屏幕中启动后台下载,并继续检查是否有标志变量,以使后台进程知道该应用程序是否仍在运行.

To achieve this, I initiate background download in my splash screen and I keep on checking for flag variable that lets the background process know if the app is still running.

现在我的问题是:我很清楚此标志变量的初始化.我已经在Application子类的onCreate()中初始化了它,因为它是应用程序启动的地方.但是我不知道在哪里清除它.我尝试在MainActivity的onDestroy()中执行此操作.但是,我发现,如果

Now my question is: I'm very clear about initialization of this flag variable. I've initialized it in onCreate() of Application subclass since it is the point where application starts. But I've no idea where to clear it. I tried doing it in onDestroy() of my MainActivity. However, I found that onDestroy() is often called on transition between one activity to another if system needs to free memory. So, doing so there would stop my background thread even when I'm switching across screens and not actually closing the app. How should I deal with this scenario ? Is there a smarter way of handling this ?

推荐答案

But I've no idea where to clear it. I tried doing it in onDestroy() of my MainActivity.

为了知道该活动是否由于用户完成操作(使用后退")而被销毁,或者Android将重新创建该活动,您可以使用

In order to know if the activity is destroyed because the user finished it (with Back) or Android will re-create it, you could use isFinishing(); Something like:

protected void onDestroy() {
 super.onDestroy();
 if(isFinishing()) {
 // stop the news feed download
}
}

或者更好的是,停止在finish()中下载供稿:

Or better, stop the feed download in finish():

public void finish() {
  // stop the news feed download
  super.finish();
}

使用上面的内容回到您上面所说的内容:

To go back to what you said above with:

I'm very clear about initialization of this flag variable. I've initialized it in onCreate() of Application subclass since it is the point where application starts.

即使活动完成,该应用程序也很可能仍然可以运行. Android操作系统将决定何时杀死它.因此,一旦应用启动,您将初始化下载,然后根据需要,在Activity中的onDestroy()finish()上停止下载,但是如果应用没有停止(最有可能)并且您重新-再次进入新闻活动,您应该开始下载新闻.

Even if the activity is finished, the application is very probable to still live. The Android OS will decide when to kill it. So you will initialize the download once the app starts, then you will stop it on onDestroy() or on finish() within Activity, depending on your desire, but if the application doesn't stop (most probable) and you're re-entering again in the news activity you should be starting the news download.

我宁愿在onCreate(Bundle savedInstance)中在后台启动下载,但是当savedInstance为空时(因此我知道这是此活动的第一个创建)并停止它(如果尚未单独停止)在finish();

I would rather initiate the download in the background in onCreate(Bundle savedInstance), but when savedInstance is null (so I know this is the first create of this activity) and stop it (if hasn't stopped already by itself) in finish();

希望有帮助!

这篇关于管理后台下载:Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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