教你轻松制作Android音乐播放器

2019-12-10 19:54:21于海丽

接下来上activity代码:

1、通过Ibinder对象获取服务对象

2、获取到服务对象以后,再访问服务的方法。

3、通过receiver刷新页面

 

 
  1. public class MainActivity extends Activity implements OnClickListener{   
  2. SeekBar seekBar;  TextView curTime,totalTime; 
  3. TextView title;   
  4. private ServiceConnection sc;  private MusicService ms; 
  5. private boolean isStop;  private double totalTimeInt;  
  6. @Override  protected void onCreate(Bundle savedInstanceState) { 
  7. super.onCreate(savedInstanceState);  setContentView(R.layout.activity_main); 
  8. IntentFilter filter = new IntentFilter(MusicService.MFILTER);  registerReceiver(new MusicReceiver(),filter); 
  9. sc = new ServiceConnection() {   
  10. @Override  public void onServiceDisconnected(ComponentName name) { 
  11. // TODO Auto-generated method stub  ms = null; 
  12. }   
  13. @Override  public void onServiceConnected(ComponentName name, IBinder service) { 
  14. // TODO Auto-generated method stub  ms = ((MBinder)service).getService();//1 
  15.   } 
  16. };  Button previous = (Button) findViewById(R.id.previous); 
  17. Button next = (Button) findViewById(R.id.next);  Button stop = (Button) findViewById(R.id.stop); 
  18. Button stopService = (Button) findViewById(R.id.stopService);  seekBar = (SeekBar) findViewById(R.id.mSeekbar); 
  19. curTime = (TextView) findViewById(R.id.curTime);  totalTime = (TextView) findViewById(R.id.totalTime); 
  20. title = (TextView) findViewById(R.id.title);   
  21. previous.setOnClickListener(this);  next.setOnClickListener(this); 
  22. stop.setOnClickListener(this);  stopService.setOnClickListener(this); 
  23. }   
  24. @Override  public void onClick(View v) { 
  25. // TODO Auto-generated method stub  switch (v.getId()) {