1

I need to set ProgressDialog.dismiss from my Music Service. I have tried setting up AsyncTask with

final class TheTask extends AsyncTask<Void, Void, Void>{
                ProgressDialog dialog = ProgressDialog.show(SomafmActivity.this, "", 
                        "Loading. Please wait...", true);   
                @Override
                protected void onPreExecute() {

             dialog.show();

                }

@Override
protected Void doInBackground(Void... params) {
final Intent i = new Intent(MusicService.ACTION_URL);
Uri uri = Uri.parse("http://sfstream1.somafm.com:8880");
i.setData(uri);
startService(i);
return null;
}

@Override
protected void onPostExecute(Void result) {
dialog.dismiss();
}

and that works fine but it dismisses the dialog at the start of the startService(i) call so the dialog disappears immediately. So then I tried accessing my ProgressDialog from the MusicService Service:

The ProgressDialog declaration in my main activity:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.somafm);

        ProgressDialog dialog = ProgressDialog.show(SomafmActivity.this, "", 
                "Loading. Please wait...", true); 

}

and in my MusicService:

public void onPrepared(MediaPlayer player) {
    // The media player is done preparing. That means we can start playing!
    mState = State.Playing;
    updateNotification(mSongTitle + " (playing)");
    configAndStartMediaPlayer();
    ProgressDialog dialog = (ProgressDialog) SomafmActivity.dialog; //This line I believe is wrong
    dialog.dismiss();
}

but I'm getting a NullPointerException in LogCat. I'm pretty sure dismissing the dialog from this location will do the trick because I don't get the error until after the stream loads and starts playing.

My question is, how should I properly reference my ProgressDialog from my Music Service?

midiwriter
  • 426
  • 5
  • 12

2 Answers2

1

Use hadler http://developer.android.com/reference/android/os/Handler.html create handler in your activity, then start your dialog and pass handler to servise. When service is started call hanler.handleMessage(message). Something like this Progress dialog in Android

Community
  • 1
  • 1
Georgy Gobozov
  • 13,633
  • 8
  • 72
  • 78
0

You have some issues with how you are accessing the dialog. You are trying to access dialog which is a member of a specific method and therefore not accessible outside of that method. Further, you are trying to access it statically, rather than from a reference to the activity in question.

What you should probably do for better form, is set a broadcastreceiver in your activity and send a broadcast from your service when it is prepared. then, from your activity, shutdown the progress dialog. Your asynctask should have a constructor which takes in the progress dialog from your activity as a parameter.

You are placing your members in the wrong classes and trying to access them in ways that do not make sense.

Here's a tutorial for services and receivers http://www.vogella.com/articles/AndroidServices/article.html

I also recommend you brush up on your java and the various access/scope rules associated with object-oriented programming.

D Yao.
  • 391
  • 1
  • 6
  • Ok thanks I will try that. But in the second scenario I am moving the ProgressDialog to the main activity's onCreate method. That still won't work? – midiwriter Aug 22 '12 at 20:10
  • sorry, just noticed this response, hopefully you've moved on from this, but just in case, your "dialog" variable in your oncreate is only accessible within the scope of the onCreate method. You cannot access it statically like Activity.dialog like you've done. It isn't a static variable. You further can't even access it from an instance of your activity class since it isn't even a member of the activity class, rather only in your onCreate method. These are really the most basic java concepts that you need to brush up on before continuing. – D Yao. Sep 06 '12 at 17:29