简体   繁体   English

管理后台下载:Android

[英]Managing background download : 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. 为此,我在初始屏幕中启动了后台下载,并继续检查标志变量,该变量使后台进程知道该应用程序是否仍在运行。

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. 我已经在Application子类的onCreate()中对其进行了初始化,因为它是应用程序启动的地方。 But I've no idea where to clear it. 但是我不知道在哪里清除它。 I tried doing it in onDestroy() of my MainActivity. 我尝试在MainActivity的onDestroy()中执行此操作。 However, I found that onDestroy() is often called on transition between one activity to another if system needs to free memory . 但是,我发现,如果系统需要释放内存,通常会在一个活动到另一个活动之间的转换中调用onDestroy() 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 ? 有没有更聪明的方式来解决这个问题?

I don't think you have to do that : either the user is pressing the "Home" button (which most people do) and then it's common for apps to keep running in background, and as so to still be easily accessible to the user in the state they left it. 我认为您不必这样做:要么用户按下“主页”按钮(大多数人都这样做),然后应用程序常在后台运行,因此仍然易于用户访问在他们离开的状态。 Either you provide a "close app" button which really kills the app, which will also kill every kind of thread created by the app and you don't have to worry. 您可以提供一个“关闭应用程序”按钮来真正杀死该应用程序,这也将杀死该应用程序创建的每种线程,而您不必担心。

If you really want, you could capture the "Home" clicks, and use those to kill the app before returning to home, which is a nice thing to do if your app has 0 initialization time. 如果确实需要,您可以捕获“主页”点击,然后使用这些点击杀死应用程序,然后再返回首页,如果您的应用程序的初始化时间为0,这是一件好事。

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

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() ; 为了知道活动是否由于用户完成操作(使用Back)而被销毁,或者Android将重新创建活动,可以使用isFinishing() ; Something like: 就像是:

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

Or better, stop the feed download in finish() : 或者,最好在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.

Even if the activity is finished, the application is very probable to still live. 即使活动完成,该应用程序也很有可能仍然可以运行。 The Android OS will decide when to kill it. Android操作系统将决定何时杀死它。 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. 因此,一旦应用启动,您将初始化下载,然后根据您的需求在Activity中的onDestroy()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() ; 我宁愿在onCreate(Bundle savedInstance)中在后台启动下载,但是当savedInstance为null(因此我知道这是此活动的首次创建)时,在finish()如果尚未自行停止)中停止下载。 finish() ;

Hope it helps! 希望能帮助到你!

To begin with for downloading datas from webservice (json or xml) you should use AsyncTask (easy to use) 首先从Web服务(json或xml)下载数据,您应该使用AsyncTask (易于使用)

so what i mean was, to clear your flag with ondestroy(), for when the application is exited, and maybe you can catch when the home button is pressed 所以我的意思是,用ondestroy()清除您的标志,以便退出应用程序,并且也许可以在按下主屏幕按钮时捕获

Override the below method in your Activity, 在您的活动中覆盖以下方法,

@Override
public void onAttachedToWindow() {
    super.onAttachedToWindow();
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);           
}

And now handle the key event like this, 现在处理这样的关键事件,

@Override

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(keyCode == KeyEvent.KEYCODE_HOME)
    {
        //do something
    }
    if(keyCode==KeyEvent.KEYCODE_BACK)
    {
        //do something
        finish();
    }
    return false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM