本文主要是介绍bindService 实现音乐播放的功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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 {
这篇关于bindService 实现音乐播放的功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!