绑定本地Service并与之通信

时间:2024-05-29 17:37:02

绑定Service需要调用

public boolean bindService (Intent service, ServiceConnection conn, int flags);

传入一个ServiceConnection 对象,该对象是一个接口,实例化时需要实现该接口,它的作用就是获得Service的IBinder对象,通过IBinder对象可以实现与Service的通信。

Service的的代码:

 package com.example.servicetest;

 import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log; public class BindServiceTest extends Service {
private int count = 0;
private boolean quit = false;
private MyBinder mBinder = new MyBinder(); public class MyBinder extends Binder {
public int count() {
return count;
}
} @Override
public IBinder onBind(Intent arg0) {
Log.i("csx", "onBind");
return mBinder;
} @Override
public void onCreate() { super.onCreate();
Log.i("csx", "onCreate");
new Thread() { @Override
public void run() {
while (!quit) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) { e.printStackTrace();
}
count++; } } }.start();
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("csx", "onStartCommand");
return START_STICKY;
} @Override
public boolean onUnbind(Intent intent) {
Log.i("csx", "onUnbind");
return true;
} @Override
public void onDestroy() {
super.onDestroy();
this.quit = true;
Log.i("csx", "onDestroy");
} }

组件的代码:

 package com.example.servicetest;

 import com.example.servicetest.BindServiceTest.MyBinder;

 import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast; public class MainActivity extends ActionBarActivity {
public static final String SERVICE_ACTION = "com.example.servicetest.BindServiceTest";
Button bindButton, unbindButton, stateButton;
ServiceConnection conn;
BindServiceTest.MyBinder mBinder;
boolean isBind = false; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bindButton = (Button) findViewById(R.id.button_bind);
unbindButton = (Button) findViewById(R.id.button_unbind);
stateButton = (Button) findViewById(R.id.button_get_state); conn = new ServiceConnection() { @Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i("csx", "onServiceConnected");
mBinder = (MyBinder) service; // 连接service之后将service中实现的Binder返回给本地声明的Binder } @Override
public void onServiceDisconnected(ComponentName name) {
Log.i("csx", "onServiceDisconnected");// 意外断开连接会调用该函数 } }; bindButton.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
Intent service = new Intent();
service.setAction(SERVICE_ACTION);
bindService(service, conn, BIND_AUTO_CREATE);// 绑定服务
isBind = true; }
}); unbindButton.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
if (!isBind) {
Toast.makeText(MainActivity.this, "请先绑定服务", Toast.LENGTH_SHORT).show();
return; } unbindService(conn);// 解除绑定服务
isBind = false; }
}); stateButton.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
if (!isBind) {
Toast.makeText(MainActivity.this, "请先绑定服务", Toast.LENGTH_SHORT).show();
return; }
Toast.makeText(MainActivity.this, "" + mBinder.count(), Toast.LENGTH_SHORT).show();// 通过mBinder获取service内部的数据 }
}); }
}