我编写了一个流式传输 rtsp 链接的应用程序。我在我的自定义服务类中流式传输了 url。我想在开始音乐之前显示一个 proggresdialog,换句话说,在加载 url 时。这是我的代码;
public class MyMediaPlayerService extends Service implements OnCompletionListener{
private String path = "rtsp://someURL";
MediaPlayer mediaPlayer;
private ProgressDialog pd;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnCompletionListener(this);
}
public void surfaceChanged(SurfaceHolder surfaceholder, int i, int j, int k) {
}
public void surfaceDestroyed(SurfaceHolder surfaceholder) {
}
public void surfaceCreated(SurfaceHolder holder) {
}
public void onCompletion(MediaPlayer _mediaPlayer) {
stopSelf();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (!mediaPlayer.isPlaying()) {
pd = new ProgressDialog(getApplicationContext());
pd.setMessage("Loading...");
pd.setCancelable(false);
pd.show();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.reset();
mediaPlayer.setDataSource(path);
mediaPlayer.prepareAsync();
} catch (Exception e) {
e.printStackTrace();
}
mediaPlayer
.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
pd.dismiss();
mp.start();
}
});
}
return START_NOT_STICKY;
}
public void onDestroy() {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
mediaPlayer.release();
}
}
我也在日志上收到此错误;
08-15 22:42:15.384: E/AndroidRuntime(1090): FATAL EXCEPTION: main
08-15 22:42:15.384: E/AndroidRuntime(1090): java.lang.RuntimeException: Unable to start service com.applogist.servis.MyMediaPlayerService@42417340 with Intent { cmp=com.applogist.standartfm/com.applogist.servis.MyMediaPlayerService }: android.view.WindowManager$BadTokenException: Unable to add window --
token null is not for an application
现在如何显示进度对话框?