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?