开发者

Call AsyncTask methods from another class/service (callbacks?)

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...


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜