绑定service并进行通信
虽然服务是在活动里面启动的,但一旦启动了服务,两者之间基本就没有关系了,我们启动了服务之后,服务就会一直处于运行状态,具体运行什么,活动就控 制不了。实际上我们可以绑定服务,并进行通信,然后可以实现在活动中去调用服务里提供的方法。
示例代码:
先是MainActivity中的代码
public class MainActivity extends AppCompatActivity implements View.OnClickListener, ServiceConnection {
private Button button,button2,button3,button4,button5;
private EditText editText;
private MyService.Binder binder=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=(EditText) findViewById(R.id.edt);
findViewById(R.id.button).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
findViewById(R.id.button3).setOnClickListener(this);
findViewById(R.id.button4).setOnClickListener(this);
findViewById(R.id.button5).setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.button:
Intent intent=new Intent(MainActivity.this,MyService.class);
intent.putExtra("data",editText.getText().toString());
startService(intent);
break;
case R.id.button2:
Intent intent1=new Intent(MainActivity.this,MyService.class);
stopService(intent1);
break;
case R.id.button3:
bindService(new Intent(MainActivity.this,MyService.class),this,Context.BIND_AUTO_CREATE);
//使用this的时候Alt+Enter
break;
case R.id.button4:
unbindService(this);
break;
case R.id.button5:
//同步数据
if(binder!=null){
binder.setData(editText.getText().toString());
}
break;
}
}
//整个程序主要目的是执行服务的内部代码
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
//在服务被绑定成功之后会执行
//Ibinder会将service中的 IBinder onBind类的返回值传进来,所以binder能调用setData方法
binder= (MyService.Binder) iBinder;
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
//在服务所在的进程崩或者杀掉的时候执行的
}
}
服务中的代码:
public class MyService extends Service {
private boolean running=false;
private String data="七夕快乐";
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
//onBind返回的Binder可以在onServiceConnected这个方法中接收到,通过这个对象调用setData方法
return new Binder(); //返回的整个Binder会作为下面方法的对象,调用setData方法
}
public class Binder extends android.os.Binder{
public void setData(String data){
MyService.this.data=data;
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//启动 startService()方法会自动回调onStartCommand方法,不过onCreate会先启动
data=intent.getStringExtra("data");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate() {//启动服务的时候会启动onCreate方法,启动绑定的时候一样会启动onCreate方法
running=true;
super.onCreate();
new Thread(){
@Override
public void run() {
super.run();
int i=0;
while (running){
i++;
System.out.println(i+":"+data);
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
@Override
public void onDestroy() {//关闭绑定的时候会启动onDestroy
super.onDestroy();
running=false;
}
}
总结:
我们先创建一个同步数据的按钮(button5),首先我们应该知道在service中含有一个函数onBind函数,看函数名 IBinder onBind这个函数名和MainActivity 中的onServiceConnected函数中的第二个参数 IBinder iBinder 是相关联的。一旦我们绑定服务,将会启动onServiceConnected函数,在onServiceConnected函数中 我们定义了一个binder,并且用(MyService.Binder)修饰,因为我们要调用service中的函数,所以用(MyService.Binder)修饰。而在service中的onBind函数中返回的 Binder可以在onServiceConnected这个方法中接收到,并可以用binder调用setData方法。binder可以调用setData方法,所以binder不为空,所以执行button5同步数 据的按钮,将在Editext上的数据输出到Logcat中


本文介绍如何在Android应用中实现Activity与Service之间的绑定及通信,包括通过按钮控制Service的启动、停止、绑定与解除绑定,以及如何在Activity中调用Service提供的方法来同步数据。
989

被折叠的 条评论
为什么被折叠?



