0

My app launches an AsyncTask thread that retrieves some information from a website. When the user exits the app while the thread is still running, the thread continues to run. When the user starts up the app, I wan't to be able to detect if that thread is still running. I am currently just using a variable to store the state of the system but this is unreliable.

This is the logcat for the thread:

03-19 08:08:31.209 18998-20719/com.hssw.hssw_petmatch I/*~FETCH DATA: doInBackground: start:

Can I get a list of threads running such that it identifies the above thread?

I have already tried this:

How to get all the tasks that are running currently

But it does not list it.

Samuel
  • 395
  • 2
  • 5
  • 20
  • Is there anything wrong with putting it in a static variable, and clearing the variable in `onPostExecute()`? – greeble31 Mar 19 '19 at 15:33
  • Yes because Android can kill any thread and sometimes onPostExecute or even onCancelled is not called – Samuel Mar 19 '19 at 15:39
  • If you're talking about app termination, yes, it can do that, but the next time the app is started, the static member will be reset to `null`. Therefor you can detect the condition. – greeble31 Mar 19 '19 at 15:41
  • If its reset to null then how do I know the thread is still running? The only way is to persist the variable – Samuel Mar 19 '19 at 15:46
  • 1
    I think you may have misunderstood; until the app is killed, you control all variables. You can set them, you can clear them. Also, threads run entirely within your control. They will die if they throw an exception, but you can detect that. The system does not arbitrarily terminate threads. When the app is terminated, it goes back to its initial state. From startup. It has no background threads, and all uninitialized static variables will be null. – greeble31 Mar 19 '19 at 15:54

0 Answers0