android SerVice绑定。
public class MainActivity extends Activity {Intent intent;Intent intent2;MyBindService service;ServiceConnection sConnection = new ServiceConnection() { //服务连接函数重写public void onServiceDisconnected(ComponentName name) {// TODO Auto-generated method stub}public void onServiceConnected(ComponentName name, IBinder binder) { //此方法是为了获得继承MyBindService里面OnBind传递的参数// TODO Auto-generated method stubservice = ((MyBinder) binder).getService();}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);}public void doClick(View v) {switch (v.getId()) {case R.id.start:intent = new Intent(MainActivity.this, MyStartService.class);startService(intent);break;case R.id.play:service.play();break;case R.id.pause:service.pause();break;case R.id.next:service.next();break;case R.id.up:service.up();break;case R.id.stop:stopService(intent);break;case R.id.bind:intent2 = new Intent(MainActivity.this, MyBindService.class);bindService(intent2, sConnection, Service.BIND_AUTO_CREATE);break;case R.id.unbind:unbindService(sConnection);break;}}@Overrideprotected void onDestroy() {// TODO Auto-generated method stubunbindService(sConnection);super.onDestroy();}就爱阅读www.92to.com网友整理上传,为您提供最全的知识大全,期待您的分享,转载请注明出处。
来源: http://www.92to.com/bangong/2017/02-27/17760052.html