简体   繁体   English

我怎么知道我在onResume上启动的任务正在运行?

[英]How can I know that a task I started onResume is running?

If I run an async call onResume is there a good way to detect that if the async call is running to not call it again? 如果我在onResume运行异步调用,是否有一种很好的方法来检测异步调用是否正在运行以不再再次调用它?
Because I am doing a network call using an AsynTask in onResume of a Fragment and it seems to be called too often 因为我正在使用片段的onResume中的AsynTask进行网络调用,所以它似乎经常被调用

Update 更新
THe problem is that when onResume is called there are no variables available. 问题是,当调用onResume时,没有可用的变量。 So I don't have a valid reference of an async task anymore to check the status. 因此,我不再具有异步任务的有效引用来检查状态。
The code is like: 代码如下:

public void onResume()  {  
   AsynTask<Void, Void, String> theTask = new AsyncTask<>() {
   //code to run  
   };
   theTask.execute();  
//code  
}    

So how am I suppose to do this since I don't have a reference? 那么,由于没有参考资料,我应该怎么做? I mean where should I keep theTask in order to be able to cancel it onResume ? 我的意思是我应该将theTask保留在哪里才能在onResume上取消它?

You can getStatus to get the status of the AsyncTask . 您可以使用getStatus获取AsyncTask的状态。 To check if its running you can compare it to AsyncTask.Status.RUNNING . 要检查它是否正在运行,可以将其与AsyncTask.Status.RUNNING进行比较。

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

相关问题 我怎么知道 CamelContext 何时启动? - How can i know when CamelContext was started? 如何触发onPause和onResume? - How can I trigger onPause and onResume? 如何检查可执行 JAR 是否已开始/完成运行? - How can I check if executable JAR has started/finished running? 如何以编程方式在启动它的同一脚本中终止正在运行的进程? - How can I programmatically terminate a running process in the same script that started it? 如何知道提交给执行人的任务是否引发异常? - How can I know if a task submitted to executor threw exception? 我可以在正在运行的任务上更改任务父级吗? - Can I change Task parent on running task? Android - 如何访问在 onResume 中的 onCreate 中实例化的 View 对象? - Android - How can I access a View object instantiated in onCreate in onResume? 如何在Android中通过onResume()恢复下载? - How can I resume my download by onResume() in android? 我的onResume依赖于`onViewCreated`。 我该如何重新设计呢? - My onResume depends on `onViewCreated`. How can I redesign it? 如何在onCreate()和onResume()方法中调用相同的函数? - How can I call the same functions in onCreate() and onResume() methods?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM