2hamed
2hamed

Reputation: 9057

How to retrieve running instances of an AsyncTask?

I'm implementing a DownloadManager-like feature in an application I'm developing. I use AsyncTask to push the download to the background.
This AsyncTask is inside a service named for example DownloadService which is instantiated on one of my activities. So this means multiple downloads could be started from different instances of an activity.
What I want to do is to have a particular activity in which I can have all the currently running downloads show their progress and have button to pause or resume the download.

Upvotes: 0

Views: 161

Answers (2)

Jeshurun
Jeshurun

Reputation: 23186

Instead of housing DownloadService inside an Activity, house it inside you app's Application class as a class member instead, as there will exist only one instance of this class per application. You can then retrieve it in you activity by calling ((YourApplication)getApplication()).getDownloadService(). You will have to:

  1. Create a class that extends Application, say YourApplication
  2. Declare it in the manifest file's <application> tag as android:name="YourApplication"
  3. Use it in your activity by invoking ((YourApplication)getApplication()).someMethodToGetDownloadService()

Upvotes: 0

Dimitris Makris
Dimitris Makris

Reputation: 5183

In case you want such thing, you can store in an Array all Async Tasks that get started in the service, and then check their status with http://developer.android.com/reference/android/os/AsyncTask.html#getStatus%28%29.

Hope this helps!

Upvotes: 1

Related Questions