2

我想在服务类中添加进度对话框。我的应用程序正在播放广播直播,但应用程序加载频道,所以我想添加进度对话框。当启动广播进度对话框时关闭..有想法吗?提前致谢..

public class MusicService extends Service {

    private static final String TAG = "MyService";
    public static MediaPlayer mediaplayer;
    String musicfile;

    @Override
    public IBinder onBind(Intent intent) {
        Log.i(" ibinder ", "");
        return null;
    }

    @Override
    public void onCreate() {
        Toast.makeText(this, "My Service Created", 0).show();
        Log.i(TAG, "onCreate");

        mediaplayer = new MediaPlayer();
        mediaplayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    }

    @Override
    public void onDestroy() {
        Toast.makeText(this, "My Service Stopped", 0).show();
        Log.w(TAG, "onDestroy");
        mediaplayer.stop();
    }

    @Override
    public void onStart(Intent intent, int startid) {
        Toast.makeText(this,
                "My Service Started :" + intent + " start id :" + startid, 0)
                .show();

        Log.d(TAG, "onStart");
        if (intent.getExtras().getString("paths") == null) {
            Log.i("Intent Null", "Null");
        } else {
            Log.i("Intent Not Null", "Not Null");
            musicfile = intent.getExtras().getString("paths");
            Log.i("musicfile", "" + musicfile);
            startPlayer();
            Log.i("startplayer() called","");
        }
    }

    public void startPlayer() {
        new Thread(startPlayer).start();
    }

    private Runnable startPlayer = new Runnable() {
        public void run() {
            try {
                mediaplayer.reset();
                mediaplayer.setDataSource(musicfile);
                mediaplayer.prepare();
                mediaplayer.start();
                Log.i("playchannel", musicfile);
                Log.e("Media Player Create", "player Create");
                Log.i("End startpayer called","");
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    };
}

这是我的服务类,我想播放直播电台流,但直播电台流需要一些负载才能播放。所以我想在广播播放器加载一段时间时做一些处理。

4

1 回答 1

3

我想你想在媒体播放器启动时停止进度对话框,但媒体播放器在需要一些时间加载无线电频道后启动,所以只需使用 Main Activity 对象在服务类中关闭进度对话框。像这样

FMActivity.pDialogs.dismiss();

我认为这是您的问题解决方案。

于 2012-09-11T17:22:56.727 回答