Bound Service 详解
一个bound service是一个存在于 client-server交互的service,,一般不会在后台无限期的运行,一个bound Service允许组件如activity去bound,发送请求,接收回应,甚至进行进程间的交流。
基础
为了提供一个bound service,你必须实现onBind()回调方法,这个方法返回一个定义了编程接口的IBinder对象,利用这个对象客户端可以和service进行service
一个client可以调用bindService()来绑定一个Service,如果这样做了,你必须实现ServiceConnection用于和Service进行交流,bindService()方法没有返回值,但当安卓系统创建了在客户端和Service的连接,它可以调用在ServiceConnection里的onServiceConnected()方法去发送可以供客户端与Service进行交流的IBinder。
多种客户端可以同时连接service,但是这个系统只调用你的Service类里的onBind()方法去检索第一个客户端绑定的IBinder,并不会每一次都调用onBind()方法
当最后一个客户端不再绑定service了,系统将会销毁这个Service(除非这个Service也被startService()方法启动)
创建一个Bound Service
有三中方法定义一个bound service,但我只介绍两种
- 继承Binder class
如果你的service是私有的并且和客户端运行在同一个进程里的话,你应该继承Binder类并且从onBind()返回一个他的实例,客户端接收这个Binder并且可以直接使用它在Binder实现里或者是Service调用公共方法
- 使用Message
如果你的接口需要在不同的进程里处理工作,你可以用Message创建一个接口,用这种方法,这个Service定义一个Handler去响应不同类型的Message,然后可以和客户端分享IBinder,允许客户端使用Message发送命令到这个Service,这个客户端可以定义一个自己的Messager,这样的话Service也可以发送信息
这是最简单的方法执行进程间的交流,因为Messenger将所有的请求都排列进入一个单个的线程,因此你不必考虑你的Service的线程安全
继承Binder class
步骤
1.在你的Service里,创建一个Binder实例
包含客户端可以调用的公共方法,
返回当前Service实例,或者返回被Service寄宿的其他类,同时也必须包含供客户端调用的公共方法
2.从onBind()方法里返回一个Binder实例
3.在客户端里,从onServiceConnectioned()方法里接收这个Binder
实例
这是MainActivity类
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Toast;
import com.example.boundservice.FirstService.LocalBinder;
public class MainActivity extends Activity {
FirstService mService;
boolean mBound = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onStart() {
super.onStart();
System.out.println("onStart1-------");
// Bind to LocalService
Intent intent = new Intent(this, FirstService.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
public void onButtonStop(View v)
{
System.out.println("onButtonStop------");
unbindService(mConnection);
}
@Override
protected void onStop() {
super.onStop();
System.out.println("onStop------");
// Unbind from the service
if (mBound) {
unbindService(mConnection);
mBound = false;
}
}
/** Called when a button is clicked (the button in the layout file attaches to
* this method with the android:onClick attribute) */
public void onButtonClick(View v) {
if (mBound) {
// Call a method from the LocalService.
// However, if this call were something that might hang, then this request should
// occur in a separate thread to avoid slowing down the activity performance.
System.out.println("onButton-----------");
int num = mService.getRandomNumber();
Toast.makeText(this, "number: " + num, Toast.LENGTH_SHORT).show();
}
}
/** Defines callbacks for service binding, passed to bindService() */
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className,
IBinder service) {
System.out.println("onServiceConnected-------");
// We've bound to LocalService, cast the IBinder and get LocalService instance
LocalBinder binder = (LocalBinder) service;
mService = binder.getService();
mBound = true;
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
System.out.println("onServiceDisconnected--------");
mBound = false;
}
};
}
这是MainActivity类的xml配置文件
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="start"
android:onClick="onButtonClick"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="89dp"
android:layout_marginTop="55dp"
android:onClick="onButtonStop"
android:text="stop" />
这是FirstService类
import java.util.Random;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
public class FirstService extends Service{
private final IBinder mBinder = (IBinder) new LocalBinder();
private final Random mGenerator = new Random();
public class LocalBinder extends Binder
{
FirstService getService()
{
System.out.println("LocalBinder------");
return FirstService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
System.out.println("onBind------");
return mBinder;
}
@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
System.out.println("onUnbind-------");
return super.onUnbind(intent);
}
public int getRandomNumber()
{
return mGenerator.nextInt(100);
}
}
使用Messager
如果你需要你的Service进行远程将的交流,那么你可以使用Messager提供一个接口,这个方法允许不使用AIDL你去执行进程间的交流
这里是一些关于使用Messager的总结
- 这个service实现一个可以接受来自客户端的每一个回调方法r的Handler
- 这个Handler被使用去创建一个Messager对象
- 这个Messager创建一个IBinder,这个Service通过onBind()返回给客户端
- 客户端使用这个IBinder去实例化一个Messager,客户端使用Messager发送message对象给service
- 通过这种方式,客户端没有在service调用方法,取而代之的是,客户端发送Messages,Service在handler里接收
这是一个使用Messager的简单例子
这是MainActivity类
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Messenger mService = null;
boolean mBound;
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
// 当这个连接在客户端和service被建立起来的时候,这个方法会被调用
// 提供给我们可以和Service交互的object,我们与Service使用Messenger进行交流,
System.out.println("onServiceConnected---------被调用");
mService = new Messenger(service);
mBound = true;
}
public void onServiceDisconnected(ComponentName className) {
// 当 连接断开的时候,这个方法被调用
System.out.println("onServiceDisconnected--------被调用");
mService = null;
mBound = false;
}
};
public void sayHello(View v) {
System.out.println("你按下了按钮");
if (!mBound) return;
// 创建并且发送一个message给Service
Message msg = Message.obtain(null, MessagerService.MSG_SAY_HELLO, 0, 0);
try {
mService.send(msg);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
protected void onStart() {
System.out.println("onStart--------被调用");
super.onStart();
// 绑定一个Service
bindService(new Intent(this, MessagerService.class), mConnection,
Context.BIND_AUTO_CREATE);
}
@Override
protected void onStop() {
System.out.println("onStop---------被调用");
super.onStop();
//撤销与一个Service的绑定
if (mBound) {
unbindService(mConnection);
mBound = false;
}
}
}
这MainActivity类的xml配置文件
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="29dp"
android:layout_marginTop="20dp"
android:text="send"
android:onClick="sayHello" />
这是MessagerService类
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.widget.Toast;
public class MessagerService extends Service{
// 控制Service展示一个message
static final int MSG_SAY_HELLO = 1;
// 处理来自客户端的Message
class IncomingHandler extends Handler {
@Override
public void handleMessage(Message msg) {
System.out.println("handleMessage-------被调用");
switch (msg.what) {
case MSG_SAY_HELLO:
Toast.makeText(getApplicationContext(), "hello!", Toast.LENGTH_SHORT).show();
break;
default:
super.handleMessage(msg);
}
}
}
// 我们给客户端提供的一个“靶子”,这个靶子发送Message给IncomingHandler
final Messenger mMessenger = new Messenger(new IncomingHandler());
// 当绑定到一个服务时,给我们的Messager返回一个接口
@Override
public IBinder onBind(Intent intent) {
System.out.println("onBind----------被调用");
Toast.makeText(getApplicationContext(), "binding", Toast.LENGTH_SHORT).show();
return mMessenger.getBinder();
}
}
记得加在<application>元素里加
<service android:name="MessagerService"></service>
这是在Logcat里的结果
这样我们就可以看清楚这些方法的执行过程,希望对你们有所帮助
</pre></div><div><pre name="code" class="java">