views:

312

answers:

2

Hi,

I was wondering if it's possible to call specific methods defined within the AsynTask class from another class and/or service ? In my specific case I have a Service playing some sounds, but the sound is selected from a List with available sounds...

When a sounds is selected it is downloaded from my home server, this takes some time (not much, let's say around the 3-4 seconds, the sounds/effects aren't big in size)... So my problem at the moment is that I have a service to play those sounds, and when I select one I wanted to show a progressdialog... The way (if I understood correctly) is to use an AsyncTask, but the only thing the AsyncTask will do is telling my Service to play a specific sound from my server... So there is no "callback" from the service to the Asynctask...

How can I achieve that ? How can I call a running AsyncTask, which sits in another class, and tell him all work is done and thus he can stop showing the ProgressDialog ? Or am I over-engineering it and there are other ways ? Thanks in advance...

A: 

I would use one asyncTask for every download. The download work is done in the doInBackground method. You can override the onPostExecute method to do all the work that should be done after the sound is downloaded. In your case I would think that would be hiding the progressdialog and telling the playing service that new music is available.

For more info on the AsyncTask have a look at the android documentation.

Janusz
just wondering here, a service is a lonely thread right ?SO I would start an AsyncTask (which is a thread as well), this one should communicate to my service that it can start playing, when my service's MediaPlayer has reached the prepared state it should tell my AsyncTask so it could stop showing a ProgressDialog...Thats what I' like to implement, so now he question is : can my Service send commands/updates to an already running AsyncTask ?
TiGer
Why do you want to tell the async task something? The task should tell the service something.
Janusz
A: 

Actually, what I think you want is the opposite. The AsyncTask should be used to perform the long running process, not to display the progress dialog. In fact, you should not use the task's background process to access any part of the UI. So what you would do is display a dialog, start your task, then have the task dismiss the dialog in its onPostExecute method, which can manipulate UI components.

JRL
For my long running process I have a Service...Now I must be able to tell the AsyncTask my service is ready to do what it's supposed to do...
TiGer
@TiGer: an Asynctask is for performing long-running background work. If this is done elsewhere, you do not need an AsyncTask. The ProgressDialog is a UI component. If you want to communicate back to the UI Thread from another Thread that is not an AsyncTask, use a Handler.
JRL
ok, well to give you an idea of what I visually am trying to achieve :the user selects an sound from a list, then a ProgressDialog (or something similar) should be show, in the meantime my sound-playing service streams the sound from my server, which might take some time, when it's done and the MediaPlayer is prepared it can start playing and the ProgressDialog should simply be removed...
TiGer
@TiGer: you can register an onPreparedListener for the MediaPlayer, when it gets called dismiss your progressDialog from there. Be careful to do this from the UI Thread.
JRL
hhmmm ok... well the MediaPlayer sits within the Service, so how do I do it from the UI Thread ?owh and how do I keep a reference to the ProgressDialog which is started within the "calling" class ?
TiGer