当前位置: 代码迷 >> 综合 >> bindService 实现音乐播放的功能
  详细解决方案

bindService 实现音乐播放的功能

热度:69   发布时间:2023-12-16 16:33:16.0


1:需要Activity实现ServiceConnection的接口

2:需要Service 提供一个继承了Binder的类来提供内部的方法。

3:进度条的设置

4:注册清单文件


public class MyActivity extends Activity implements ServiceConnection,Runnable {private Thread thread;
    private TextView textView;
    private Handler handler;
    private boolean flag;
    private ProgressBar progressBar;
    /**  * Called when the activity is first created.  */  @Override
    public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Intent service = new Intent(this, PlayService.class);
        bindService(service,this,BIND_AUTO_CREATE);
        textView = (TextView)findViewById(R.id.main_progress_textView);
        progressBar = (ProgressBar)findViewById(R.id.main_progressbar);
        thread = new Thread(this);
        handler = new Handler(){@Override
            public void handleMessage(Message msg) {super.handleMessage(msg);
                int position =msg.arg1;
                int total = msg.arg2;
                textView.setText(""+position+total);
            }};
        thread.start();
    }public void run(){while(true){try {