本文主要讲activity如何与service进行交互,这个交互很常见,如在一个音乐播放器中,service负责音乐的播放,activity负责前端UI,当你在前端对进度调节时,需要向servie发出一个命令,而service播放音乐的进度也需要及时的返回给activity,使得activity的进度条能够向后滑动。本文所参考的是官网。上面有详细的例子,这里不再详述。
activity的使用:
1 service和activity在同一进程时,使用IBinder
当activity需要和service交互时,采用service的第二种启动方式,即
bindService(),将avtivity(准确的说是clients,因为其他组件也可以和service绑定)和service绑定。一方面,service需要创建自己的IBinder类,继承Binder,override这个类的onBind()方法,在该方法中向clients返回这个IBinder实例。另一方面,clients需要实现一个ServiceConnection对象(是一个内部匿名类),clients可以利用这个ServiceConnection中的onServiceConnected()方法获取这个IBinder实例,在通过这个IBinder中的方法获取service的实例,从而访问service的public方法。
这种方法非常适合service只是activity的一个后台工作,再次强调,这种方法只适于
同一进程的activity和service,在默认情况下的service和activity是在同一个进程中的,除非在manifest中另外声明 android:process 属性。
而且在例子中也只有
activity向service
发送消息,service向activity发送消息还没有找到,学渣认为可以用广播。
这里有一个例子,activity可以通过IBinder调用service中的方法,大家可以参考,首先是service,在service中声明一个IBinder类的实例,在onBind时向clients返回这个IBinder对象,而clients可以通过onServiceConnected()获取IBinder实例,代码如下:
public class LocalService extends Service {
// Binder given to clients
private final IBinder mBinder = new LocalBinder();
// Random number generator
private final Random mGenerator = new Random();
/**
* Class used for the client Binder. Because we know this service always
* runs in the same process as its clients, we don't need to deal with IPC.
*/
public class LocalBinder extends Binder {
LocalService getService() {
// Return this instance of LocalService so clients can call public methods
return LocalService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
/** method for clients */
public int getRandomNumber() {
return mGenerator.nextInt(100);
}
}
activity的使用:
public class BindingActivity extends Activity {
LocalService mService;
boolean mBound = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
protected void onStart() {
super.onStart();
// Bind to LocalService
Intent intent = new Intent(this, LocalService.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
@Override
protected void onStop() {
super.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.
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) {
// 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) {
mBound = false;
}
};
}
2 service和activity不在同一进程
(1)使用Messager ,
这是最简单的IPC,在service中定义一个Handler用来处理不同的Message对象
(2)使用AIDL
由于学渣现在还没有用到这块,暂时停一下,以后学了会继续更新本博客。