接下来上activity代码:
1、通过Ibinder对象获取服务对象
2、获取到服务对象以后,再访问服务的方法。
3、通过receiver刷新页面
- public class MainActivity extends Activity implements OnClickListener{
- SeekBar seekBar; TextView curTime,totalTime;
- TextView title;
- private ServiceConnection sc; private MusicService ms;
- private boolean isStop; private double totalTimeInt;
- @Override protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
- IntentFilter filter = new IntentFilter(MusicService.MFILTER); registerReceiver(new MusicReceiver(),filter);
- sc = new ServiceConnection() {
- @Override public void onServiceDisconnected(ComponentName name) {
- // TODO Auto-generated method stub ms = null;
- }
- @Override public void onServiceConnected(ComponentName name, IBinder service) {
- // TODO Auto-generated method stub ms = ((MBinder)service).getService();//1
- }
- }; Button previous = (Button) findViewById(R.id.previous);
- Button next = (Button) findViewById(R.id.next); Button stop = (Button) findViewById(R.id.stop);
- Button stopService = (Button) findViewById(R.id.stopService); seekBar = (SeekBar) findViewById(R.id.mSeekbar);
- curTime = (TextView) findViewById(R.id.curTime); totalTime = (TextView) findViewById(R.id.totalTime);
- title = (TextView) findViewById(R.id.title);
- previous.setOnClickListener(this); next.setOnClickListener(this);
- stop.setOnClickListener(this); stopService.setOnClickListener(this);
- }
- @Override public void onClick(View v) {
- // TODO Auto-generated method stub switch (v.getId()) {










