作者:liuyi1207164339
原文:https://blog.csdn.net/liuyi1207164339/article/details/51708025
前一篇文章讲了怎么在客户端使用AIDL实现IPC通信,调用远程服务端的方法。但是,远程服务端并不能主动给客户端返回信息。在很多情况下是需要远程服务端主动给客户端返回数据,客户端只需要进行监听即可,这是典型的观察者模式。这篇文章主要来解决一下这个问题。
代码主要来自ApiDemos/App/Service/Remote Service Binding,下面对代码进行说明。
1、首先是AIDL接口定义
这里定义了三个接口,首先是IRemoteService,这个接口主要是用于客户端注册和解注册回调接口,这样服务端就可以往客户端回传数据。
package com.easyliu.demo.aidl;
import com.easyliu.demo.aidl.IRemoteServiceCallback;
/**
* Example of defining an interface for calling on to a remote service
* (running in another process).
*/
interface IRemoteService {
/**
* Often you want to allow a service to call back to its clients.
* This shows how to do so, by registering a callback interface with
* the service.
*/
void registerCallback(IRemoteServiceCallback cb);
/**
* Remove a previously registered callback interface.
*/
void unregisterCallback(IRemoteServiceCallback cb);
}
然后是IRemoteServiceCallback,这个是回调接口,用于往客户端回传信息。由于AIDL接口中不支持一般的interface,所以接口也得是aidl接口类型,如下所示:
package com.easyliu.demo.aidl;
/**
* Example of a callback interface used by IRemoteService to send
* synchronous notifications back to its clients. Note that this is a
* one-way interface so the server does not block waiting for the client.
*/
oneway interface IRemoteServiceCallback {
/**
* Called when the service has a new value for you.
*/
void valueChanged(int value);
}
最后是另一个aidl接口ISecondary,接口中定义了两个方法,如下所示。有一个方法是获取服务端所在进程的PID,这样我们可以看到当客户端根据这个PID杀死服务端进程的时候会出现什么反应,这个后面再说。
interface ISecondary {
/**
* Request the PID of this service, to do evil things with it.
*/
int getPid();
/**
* This demonstrates the basic types that you can use as parameters
* and return values in AIDL.
*/
void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
double aDouble, String aString);
}
2、AIDL接口在服务端的实现
service端的代码如下所示。有几点需要讲解:
1、使用RemoteCallbackList来保存客户端传过来的回调接口,使用它可以保证服务端接收到的对象和客户端的是同一个对象。
2、IRemoteSevice的两个方法分别是注册接口和解注册接口,使用RemoteCallbackList的register和unregister方法。
3、ISecondary的getPid方法当中返回当前服务端进程的PID。
4、在service的oncreate方法中发送了一个消息给消息队列,Handler接收到这个消息之后给服务端发送一个值,发送完成之后每隔一秒发送一个消息,这样客户端每隔一秒就会收到服务端发来的值,这个值就是一个累加的数字。
5、调用RemoteCallbackList当中保存的回调接口发送数据有固定的写法,如下所示。首先得开始广播,然后得到list当中的每一项,然后调用此接口的方法。当所有注册的接口都回调完成之后,需要结束广播。
final int N = mCallbacks.beginBroadcast();
for (int i=0; i<N; i++) {
try {
mCallbacks.getBroadcastItem(i).valueChanged(value);
} catch (RemoteException e) {
// The RemoteCallbackList will take care of removing
// the dead object for us.
}
}
mCallbacks.finishBroadcast();
远程Service代码:
package com.easyliu.demo.aidldemo;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Process;
import android.os.RemoteCallbackList;
import android.os.RemoteException;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.easyliu.demo.aidl.IRemoteService;
import com.easyliu.demo.aidl.IRemoteServiceCallback;
import com.easyliu.demo.aidl.ISecondary;
public class RemoteService extends Service {
/**
* This is a list of callbacks that have been registered with the
* service. Note that this is package scoped (instead of private) so
* that it can be accessed more efficiently from inner classes.
*/
final RemoteCallbackList<IRemoteServiceCallback> mCallbacks
= new RemoteCallbackList<IRemoteServiceCallback>();
private int mValue = 0;
private static final int REPORT_MSG = 1;
@Override
public void onCreate() {
mHandler.sendEmptyMessage(REPORT_MSG);
}
@Override
public void onDestroy() {
// Tell the user we stopped.
Toast.makeText(this, R.string.remote_service_stopped, Toast.LENGTH_SHORT).show();
// Unregister all callbacks.
mCallbacks.kill();
// Remove the next pending message to increment the counter, stopping
// the increment loop.
mHandler.removeMessages(REPORT_MSG);
}
@Override
public IBinder onBind(Intent intent) {
// Select the interface to return. If your service only implements
// a single interface, you can just return it here without checking
// the Intent.
if (IRemoteService.class.getName().equals(intent.getAction())) {
return mBinder;
}
if (ISecondary.class.getName().equals(intent.getAction())) {
return mSecondaryBinder;
}
return null;
}
/**
* The IRemoteInterface is defined through IDL
*/
private final IRemoteService.Stub mBinder = new IRemoteService.Stub() {
public void registerCallback(IRemoteServiceCallback cb) {
if (cb != null) mCallbacks.register(cb);
}
public void unregisterCallback(IRemoteServiceCallback cb) {
if (cb != null) mCallbacks.unregister(cb);
}
};
/**
* A secondary interface to the service.
*/
private final ISecondary.Stub mSecondaryBinder = new ISecondary.Stub() {
public int getPid() {
return Process.myPid();
}
public void basicTypes(int anInt, long aLong, boolean aBoolean,
float aFloat, double aDouble, String aString) {
}
};
@Override
public void onTaskRemoved(Intent rootIntent) {
Toast.makeText(this, "Task removed: " + rootIntent, Toast.LENGTH_LONG).show();
}
/**
* Our Handler used to execute operations on the main thread. This is used
* to schedule increments of our value.
*/
private final Handler mHandler = new Handler() {
@Override public void handleMessage(Message msg) {
switch (msg.what) {
// It is time to bump the value!
case REPORT_MSG: {
// Up it goes.
int value = ++mValue;
// Broadcast to all clients the new value.
final int N = mCallbacks.beginBroadcast();
for (int i=0; i<N; i++) {
try {
mCallbacks.getBroadcastItem(i).valueChanged(value);
} catch (RemoteException e) {
// The RemoteCallbackList will take care of removing
// the dead object for us.
}
}
mCallbacks.finishBroadcast();
// Repeat every 1 second.
sendMessageDelayed(obtainMessage(REPORT_MSG), 1*1000);
} break;
default:
super.handleMessage(msg);
}
}
};
}
3、在Manifest文件里面注册Service
在Service当中加了几个action,用于别的组件通过Intent隐式启动此Service。
<service
android:name=".RemoteService"
android:process=":remote">
<intent-filter>
<action android:name="com.easyliu.demo.aidl.IRemoteService" />
<action android:name="com.easyliu.demo.aidl.ISecondary" />
<action android:name="com.easyliu.demo.aidldemo.RemoteService" />
</intent-filter>
</service>
4、客户端的实现
客户端界面主要是由三个按钮:绑定、解除绑定、杀死服务端进程,然后还有一个显示状态的文本控件。
布局文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<Button
android:id="@+id/bind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/bind_service">
<requestFocus />
</Button>
<Button
android:id="@+id/unbind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/unbind_service"></Button>
<Button
android:id="@+id/kill"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/kill_process"></Button>
<TextView
android:id="@+id/callback"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:gravity="center_horizontal"
android:paddingTop="4dip"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
主Activity代码如下所示。有几点需要说明:
1、点击BIND SERVICE按钮的时候,同时绑定ISecondary和IRemoteService,返回相应的接口。同时,给返回的IRemoteService接口注册一个回调接口,用于接收服务端发来的信息。IRemoteServiceCallback回调接口如下所示,在注释中已经有了说明,由于valuedChanged方法是运行客户端的Binder线程当中,是不能直接访问主UI当中的控件的,所以需要通过Handler切换到主UI线程中去执行。
注意:如果valuedChanged比较耗时的话,必须确保RemoteService当中的valueChanged方法不是运行在主UI当中,不然会导致服务端无法响应。
同理:在客户端调用服务端的方法的时候,如果服务端的方法比较耗时,我们就得避免在客户端的UI线程当中去访问远程方法,不然会导致客户端无响应。
private IRemoteServiceCallback mCallback = new IRemoteServiceCallback.Stub() {
/**
* This is called by the remote service regularly to tell us about
* new values. Note that IPC calls are dispatched through a thread
* pool running in each process, so the code executing here will
* NOT be running in our main thread like most other things -- so,
* to update the UI, we need to use a Handler to hop over there.
*/
public void valueChanged(int value) {
mHandler.sendMessage(mHandler.obtainMessage(BUMP_MSG, value, 0));
}
};
mHandler的实现如下所示:
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case BUMP_MSG:
mCallbackText.setText("Received from service: " + msg.arg1);
break;
default:
super.handleMessage(msg);
}
}
};
2、点击UNBIND SERVICE按钮的时候,需要先解注册之前注册的IRemoteServiceCallback回调接口,然后再unbindService。
3、在执行bindService的时候,代码如下所示,第三个参数有几个可选项,一般选Context.BIND_AUTO_CREATE,意思是如果在绑定过程中,Service进程被意外杀死了,系统还会自动重新启动被绑定的Service。所以当我们点击KILL PROCESS按钮的时候会杀死Service进程,但是马上又会自动重启,重新调用onServiceConnected方法重新绑定。当然,这个参数还有别的一些选择。
bindService(intent,
mConnection, Context.BIND_AUTO_CREATE);
主Activity代码:
package com.easyliu.demo.aidldemo;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Process;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.easyliu.demo.aidl.IRemoteService;
import com.easyliu.demo.aidl.IRemoteServiceCallback;
import com.easyliu.demo.aidl.ISecondary;
public class BindActivity extends AppCompatActivity {
IRemoteService mService = null;
ISecondary mSecondaryService = null;
Button mKillButton;
TextView mCallbackText;
private boolean mIsBound;
private static final int BUMP_MSG = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.remote_service_binding);
Button button = (Button) findViewById(R.id.bind);
button.setOnClickListener(mBindListener);
button = (Button) findViewById(R.id.unbind);
button.setOnClickListener(mUnbindListener);
mKillButton = (Button) findViewById(R.id.kill);
mKillButton.setOnClickListener(mKillListener);
mKillButton.setEnabled(false);
mCallbackText = (TextView) findViewById(R.id.callback);
mCallbackText.setText("Not attached.");
}
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case BUMP_MSG:
mCallbackText.setText("Received from service: " + msg.arg1);
break;
default:
super.handleMessage(msg);
}
}
};
/**
* Class for interacting with the main interface of the service.
*/
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className,
IBinder service) {
// This is called when the connection with the service has been
// established, giving us the service object we can use to
// interact with the service. We are communicating with our
// service through an IDL interface, so get a client-side
// representation of that from the raw service object.
mService = IRemoteService.Stub.asInterface(service);
mKillButton.setEnabled(true);
mCallbackText.setText("Attached.");
// We want to monitor the service for as long as we are
// connected to it.
try {
mService.registerCallback(mCallback);
} catch (RemoteException e) {
// In this case the service has crashed before we could even
// do anything with it; we can count on soon being
// disconnected (and then reconnected if it can be restarted)
// so there is no need to do anything here.
}
// As part of the sample, tell the user what happened.
Toast.makeText(BindActivity.this, R.string.remote_service_connected,
Toast.LENGTH_SHORT).show();
}
public void onServiceDisconnected(ComponentName className) {
// This is called when the connection with the service has been
// unexpectedly disconnected -- that is, its process crashed.
mService = null;
mKillButton.setEnabled(false);
mCallbackText.setText("Disconnected.");
// As part of the sample, tell the user what happened.
Toast.makeText(BindActivity.this, R.string.remote_service_disconnected,
Toast.LENGTH_SHORT).show();
}
};
/**
* Class for interacting with the secondary interface of the service.
*/
private ServiceConnection mSecondaryConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className,
IBinder service) {
// Connecting to a secondary interface is the same as any
// other interface.
mSecondaryService = ISecondary.Stub.asInterface(service);
mKillButton.setEnabled(true);
}
public void onServiceDisconnected(ComponentName className) {
mSecondaryService = null;
mKillButton.setEnabled(false);
}
};
/**
* 绑定
*/
private View.OnClickListener mBindListener = new View.OnClickListener() {
public void onClick(View v) {
Intent intent=new Intent(IRemoteService.class.getName());
intent.setPackage("com.easyliu.demo.aidldemo");
//注意这里的Context.BIND_AUTO_CREATE,这意味这如果在绑定的过程中,
//如果Service由于某种原因被Destroy了,Android还会自动重新启动被绑定的Service。
// 你可以点击Kill Process 杀死Service看看结果
bindService(intent,
mConnection, Context.BIND_AUTO_CREATE);
intent=new Intent(ISecondary.class.getName());
intent.setPackage("com.easyliu.demo.aidldemo");
bindService(intent,
mSecondaryConnection, Context.BIND_AUTO_CREATE);
mIsBound = true;
mCallbackText.setText("Binding.");
}
};
/**
* 解除绑定
*/
private View.OnClickListener mUnbindListener = new View.OnClickListener() {
public void onClick(View v) {
if (mIsBound) {
if (mService != null) {
try {
mService.unregisterCallback(mCallback);
} catch (RemoteException e) {
// There is nothing special we need to do if the service
// has crashed.
}
}
// Detach our existing connection.
unbindService(mConnection);
unbindService(mSecondaryConnection);
mKillButton.setEnabled(false);
mIsBound = false;
mCallbackText.setText("Unbinding.");
}
}
};
private View.OnClickListener mKillListener = new View.OnClickListener() {
public void onClick(View v) {
// To kill the process hosting our service, we need to know its
// PID. Conveniently our service has a call that will return
// to us that information.
if (mSecondaryService != null) {
try {
int pid = mSecondaryService.getPid();
// Note that, though this API allows us to request to
// kill any process based on its PID, the kernel will
// still impose standard restrictions on which PIDs you
// are actually able to kill. Typically this means only
// the process running your application and any additional
// processes created by that app as shown here; packages
// sharing a common UID will also be able to kill each
// other's processes.
Process.killProcess(pid);
mCallbackText.setText("Killed service process.");
} catch (RemoteException ex) {
// Recover gracefully from the process hosting the
// server dying.
// Just for purposes of the sample, put up a notification.
Toast.makeText(BindActivity.this,
R.string.remote_call_failed,
Toast.LENGTH_SHORT).show();
}
}
}
};
/**
* 远程回调接口实现
*/
private IRemoteServiceCallback mCallback = new IRemoteServiceCallback.Stub() {
/**
* This is called by the remote service regularly to tell us about
* new values. Note that IPC calls are dispatched through a thread
* pool running in each process, so the code executing here will
* NOT be running in our main thread like most other things -- so,
* to update the UI, we need to use a Handler to hop over there.
*/
public void valueChanged(int value) {
mHandler.sendMessage(mHandler.obtainMessage(BUMP_MSG, value, 0));
}
};
}
5、执行效果
当点击BIND SERVICE按钮,就会每隔一秒收到服务端发来的消息:
当点击KILL PROCESS按钮的时候,首先会杀死Service进程,然后马上就会重新启动Service进程,重新绑定。这是因为在bindService的时候,第三个参数设置为了Context.BIND_AUTO_CREATE,所以就会出现这样的效果。
此外:
1、在进行IPC通信的时候,还可以验证权限,只有具有某个权限的APP才能绑定此服务然后返回Binder,然后使用此Binder进行通信。关于权限验证这里不讲。
2、同时需要给返回的IBinder对象设置了一个死亡代理,当远端Service由于某种原因死亡的时候,就会调用此回调方法,我们就可以在此方法当中进行一些操作,比如,重新bindService等。这个在前一节有讲。
3、以上例程显示了两个AIDL接口均在同一个Service里面进行实现的情况。由于Service是四大组件之一,是一种系统资源,不适合无限制的增加Service,最好是把所有的AIDL放在同一个Service当中去管理。可以自己写一个工具专门用来管理所有的AIDL连接,把工具设置成单例,然后暴露给客户端一些接口即可。
代码下载地址:https://github.com/EasyLiu-Ly/AIDLDemo