如何在Service类中添加进度对话框?

时间:2012-09-11 12:40:45

标签: android android-service

我想在Service类中添加Progress对话框。我的应用程序正在播放Radio直播但应用程序加载频道所以我想添加进度对话框。当启动无线电进度对话框解雇时...有想法吗? 在此先感谢..

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();
            }
        }
    };
}

这是我的服务类,我想播放实时广播流,但直播无线电流需要一些负载。所以我想在广播播放器加载一段时间后做一些事情。

1 个答案:

答案 0 :(得分:3)

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

FMActivity.pDialogs.dismiss();

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