标签:
我们要绑定service的时候要实现ServiceConnection这个接口;这个接口有两个方法
@Override public void onServiceConnected(ComponentName arg0, IBinder service) {
// 服务被绑定成功后调用
}
@Override public void onServiceDisconnected(ComponentName arg0) {
//服务所在进程崩溃时调用,或者说杀掉时候调用
}
我们要传递数据就要通过onServiceConnected里面的ibinder对象,
这个对象访问到的是service里IBinder()的返回值;所以我们要新建一个类来继承系统下binder这个类,然后让IBinder()返回这个类,
这时候我们就可以访问这个新建的类了;
package com.example.service_senddata; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.Bundle; import android.os.IBinder; public class MyService extends Service { private String data = "这是默认信息"; private boolean flag = false; @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub //返回新建的类 return new Obinder(); } //这个类用来继承系统下的binder类, public class Obinder extends android.os.Binder { //这个类下面的设置数据的方法 void setData(String data) { MyService.this.data = data; } } @Override public int onStartCommand(Intent intent, int flags, int startId) { // TODO Auto-generated method stub Bundle b = intent.getExtras(); data = b.getString("msg"); return super.onStartCommand(intent, flags, startId); } @Override public void onCreate() { super.onCreate(); flag = true; new Thread() { public void run() { while (flag) { System.out.println("Service中获取到的数据:" + data); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }; }.start(); }; @Override public void onDestroy() { // TODO Auto-generated method stub flag = false; } }
activity代码
package com.example.service_senddata; import android.os.Bundle; import android.os.IBinder; import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity implements OnClickListener, ServiceConnection{ private Button btn_startservice; private Button btn_stopservice; private EditText edt_data; //声明service的内部类 private MyService.Obinder binder = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_startservice = (Button) findViewById(R.id.btn_startservice); btn_stopservice = (Button) findViewById(R.id.btn_stopservice); edt_data = (EditText) findViewById(R.id.edt_data); btn_startservice.setOnClickListener(this); btn_stopservice.setOnClickListener(this); findViewById(R.id.btn_bindservice).setOnClickListener(this); findViewById(R.id.btn_unbindservice).setOnClickListener(this); findViewById(R.id.btn_sycservice).setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_startservice: Intent i = new Intent(this,MyService.class); i.putExtra("msg", edt_data.getText().toString()); startService(i); break; case R.id.btn_stopservice: stopService(new Intent(this,MyService.class)); break; case R.id.btn_bindservice: bindService(new Intent(this,MyService.class), this, Context.BIND_AUTO_CREATE); break; case R.id.btn_unbindservice: unbindService(this); break; case R.id.btn_sycservice: if(binder!=null){ //调用内部类里的方法 binder.setData(edt_data.getText().toString()); } break; } } @Override public void onServiceConnected(ComponentName arg0, IBinder service) { // 这里的binder对象可以访问到myservice下onBind()方法的返回值 //进行初始化 binder = (com.example.service_senddata.MyService.Obinder) service; } @Override public void onServiceDisconnected(ComponentName arg0) { // TODO Auto-generated method stub } }
service代码
标签:
原文地址:http://www.cnblogs.com/84126858jmz/p/4890694.html