当前位置:   article > 正文

使用AIDL实现IPC通信之——实现服务端主动发送数据给客户端_android aidl服务端主动向客户端发消息

android aidl服务端主动向客户端发消息

前一篇文章讲了怎么在客户端使用AIDL实现IPC通信,调用远程服务端的方法。但是,远程服务端并不能主动给客户端返回信息。在很多情况下是需要远程服务端主动给客户端返回数据,客户端只需要进行监听即可,这是典型的观察者模式。这篇文章主要来解决一下这个问题。


代码主要来自ApiDemos/App/Service/Remote Service Binding,下面对代码进行说明。

1、首先是AIDL接口定义

这里定义了三个接口,首先是IRemoteService,这个接口主要是用于客户端注册和解注册回调接口,这样服务端就可以往客户端回传数据。

  1. package com.easyliu.demo.aidl;
  2. import com.easyliu.demo.aidl.IRemoteServiceCallback;
  3. /**
  4. * Example of defining an interface for calling on to a remote service
  5. * (running in another process).
  6. */
  7. interface IRemoteService {
  8. /**
  9. * Often you want to allow a service to call back to its clients.
  10. * This shows how to do so, by registering a callback interface with
  11. * the service.
  12. */
  13. void registerCallback(IRemoteServiceCallback cb);
  14. /**
  15. * Remove a previously registered callback interface.
  16. */
  17. void unregisterCallback(IRemoteServiceCallback cb);
  18. }

 
 
然后是IRemoteServiceCallback,这个是回调接口,用于往客户端回传信息。由于AIDL接口中不支持一般的interface,所以接口也得是aidl接口类型,如下所示: 

  1. package com.easyliu.demo.aidl;
  2. /**
  3. * Example of a callback interface used by IRemoteService to send
  4. * synchronous notifications back to its clients. Note that this is a
  5. * one-way interface so the server does not block waiting for the client.
  6. */
  7. oneway interface IRemoteServiceCallback {
  8. /**
  9. * Called when the service has a new value for you.
  10. */
  11. void valueChanged(int value);
  12. }
最后是另一个aidl接口ISecondary,接口中定义了两个方法,如下所示。有一个方法是获取服务端所在进程的PID,这样我们可以看到当客户端根据这个PID杀死服务端进程的时候会出现什么反应,这个后面再说。

  1. interface ISecondary {
  2. /**
  3. * Request the PID of this service, to do evil things with it.
  4. */
  5. int getPid();
  6. /**
  7. * This demonstrates the basic types that you can use as parameters
  8. * and return values in AIDL.
  9. */
  10. void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
  11. double aDouble, String aString);
  12. }

2、AIDL接口在服务端的实现

service端的代码如下所示。有几点需要讲解:

1、使用RemoteCallbackList来保存客户端传过来的回调接口,使用它可以保证服务端接收到的对象和客户端的是同一个对象。

2、IRemoteSevice的两个方法分别是注册接口和解注册接口,使用RemoteCallbackList的register和unregister方法。

3、ISecondary的getPid方法当中返回当前服务端进程的PID。

4、在service的oncreate方法中发送了一个消息给消息队列,Handler接收到这个消息之后给服务端发送一个值,发送完成之后每隔一秒发送一个消息,这样客户端每隔一秒就会收到服务端发来的值,这个值就是一个累加的数字。

5、调用RemoteCallbackList当中保存的回调接口发送数据有固定的写法,如下所示。首先得开始广播,然后得到list当中的每一项,然后调用此接口的方法。当所有注册的接口都回调完成之后,需要结束广播。

  1. final int N = mCallbacks.beginBroadcast();
  2. for (int i=0; i<N; i++) {
  3. try {
  4. mCallbacks.getBroadcastItem(i).valueChanged(value);
  5. } catch (RemoteException e) {
  6. // The RemoteCallbackList will take care of removing
  7. // the dead object for us.
  8. }
  9. }
  10. mCallbacks.finishBroadcast();

远程Service代码:

  1. package com.easyliu.demo.aidldemo;
  2. import android.app.Activity;
  3. import android.app.Notification;
  4. import android.app.NotificationManager;
  5. import android.app.PendingIntent;
  6. import android.app.Service;
  7. import android.content.ComponentName;
  8. import android.content.Context;
  9. import android.content.Intent;
  10. import android.content.ServiceConnection;
  11. import android.os.Bundle;
  12. import android.os.Handler;
  13. import android.os.IBinder;
  14. import android.os.Message;
  15. import android.os.Process;
  16. import android.os.RemoteCallbackList;
  17. import android.os.RemoteException;
  18. import android.view.View;
  19. import android.view.View.OnClickListener;
  20. import android.widget.Button;
  21. import android.widget.TextView;
  22. import android.widget.Toast;
  23. import com.easyliu.demo.aidl.IRemoteService;
  24. import com.easyliu.demo.aidl.IRemoteServiceCallback;
  25. import com.easyliu.demo.aidl.ISecondary;
  26. public class RemoteService extends Service {
  27. /**
  28. * This is a list of callbacks that have been registered with the
  29. * service. Note that this is package scoped (instead of private) so
  30. * that it can be accessed more efficiently from inner classes.
  31. */
  32. final RemoteCallbackList<IRemoteServiceCallback> mCallbacks
  33. = new RemoteCallbackList<IRemoteServiceCallback>();
  34. private int mValue = 0;
  35. private static final int REPORT_MSG = 1;
  36. @Override
  37. public void onCreate() {
  38. mHandler.sendEmptyMessage(REPORT_MSG);
  39. }
  40. @Override
  41. public void onDestroy() {
  42. // Tell the user we stopped.
  43. Toast.makeText(this, R.string.remote_service_stopped, Toast.LENGTH_SHORT).show();
  44. // Unregister all callbacks.
  45. mCallbacks.kill();
  46. // Remove the next pending message to increment the counter, stopping
  47. // the increment loop.
  48. mHandler.removeMessages(REPORT_MSG);
  49. }
  50. @Override
  51. public IBinder onBind(Intent intent) {
  52. // Select the interface to return. If your service only implements
  53. // a single interface, you can just return it here without checking
  54. // the Intent.
  55. if (IRemoteService.class.getName().equals(intent.getAction())) {
  56. return mBinder;
  57. }
  58. if (ISecondary.class.getName().equals(intent.getAction())) {
  59. return mSecondaryBinder;
  60. }
  61. return null;
  62. }
  63. /**
  64. * The IRemoteInterface is defined through IDL
  65. */
  66. private final IRemoteService.Stub mBinder = new IRemoteService.Stub() {
  67. public void registerCallback(IRemoteServiceCallback cb) {
  68. if (cb != null) mCallbacks.register(cb);
  69. }
  70. public void unregisterCallback(IRemoteServiceCallback cb) {
  71. if (cb != null) mCallbacks.unregister(cb);
  72. }
  73. };
  74. /**
  75. * A secondary interface to the service.
  76. */
  77. private final ISecondary.Stub mSecondaryBinder = new ISecondary.Stub() {
  78. public int getPid() {
  79. return Process.myPid();
  80. }
  81. public void basicTypes(int anInt, long aLong, boolean aBoolean,
  82. float aFloat, double aDouble, String aString) {
  83. }
  84. };
  85. @Override
  86. public void onTaskRemoved(Intent rootIntent) {
  87. Toast.makeText(this, "Task removed: " + rootIntent, Toast.LENGTH_LONG).show();
  88. }
  89. /**
  90. * Our Handler used to execute operations on the main thread. This is used
  91. * to schedule increments of our value.
  92. */
  93. private final Handler mHandler = new Handler() {
  94. @Override public void handleMessage(Message msg) {
  95. switch (msg.what) {
  96. // It is time to bump the value!
  97. case REPORT_MSG: {
  98. // Up it goes.
  99. int value = ++mValue;
  100. // Broadcast to all clients the new value.
  101. final int N = mCallbacks.beginBroadcast();
  102. for (int i=0; i<N; i++) {
  103. try {
  104. mCallbacks.getBroadcastItem(i).valueChanged(value);
  105. } catch (RemoteException e) {
  106. // The RemoteCallbackList will take care of removing
  107. // the dead object for us.
  108. }
  109. }
  110. mCallbacks.finishBroadcast();
  111. // Repeat every 1 second.
  112. sendMessageDelayed(obtainMessage(REPORT_MSG), 1*1000);
  113. } break;
  114. default:
  115. super.handleMessage(msg);
  116. }
  117. }
  118. };
  119. }

3、在Manifest文件里面注册Service

在Service当中加了几个action,用于别的组件通过Intent隐式启动此Service。

  1. <service
  2. android:name=".RemoteService"
  3. android:process=":remote">
  4. <intent-filter>
  5. <action android:name="com.easyliu.demo.aidl.IRemoteService" />
  6. <action android:name="com.easyliu.demo.aidl.ISecondary" />
  7. <action android:name="com.easyliu.demo.aidldemo.RemoteService" />
  8. </intent-filter>
  9. </service>

4、客户端的实现
客户端界面主要是由三个按钮:绑定、解除绑定、杀死服务端进程,然后还有一个显示状态的文本控件。

布局文件如下所示:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:gravity="center"
  6. android:orientation="vertical">
  7. <Button
  8. android:id="@+id/bind"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:text="@string/bind_service">
  12. <requestFocus />
  13. </Button>
  14. <Button
  15. android:id="@+id/unbind"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:text="@string/unbind_service"></Button>
  19. <Button
  20. android:id="@+id/kill"
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:text="@string/kill_process"></Button>
  24. <TextView
  25. android:id="@+id/callback"
  26. android:layout_width="match_parent"
  27. android:layout_height="wrap_content"
  28. android:layout_weight="0"
  29. android:gravity="center_horizontal"
  30. android:paddingTop="4dip"
  31. android:textAppearance="?android:attr/textAppearanceMedium" />
  32. </LinearLayout>

主Activity代码如下所示。有几点需要说明:

1、点击BIND SERVICE按钮的时候,同时绑定ISecondary和IRemoteService,返回相应的接口。同时,给返回的IRemoteService接口注册一个回调接口,用于接收服务端发来的信息。IRemoteServiceCallback回调接口如下所示,在注释中已经有了说明,由于valuedChanged方法是运行客户端的Binder线程当中,是不能直接访问主UI当中的控件的,所以需要通过Handler切换到主UI线程中去执行。

注意:如果valuedChanged比较耗时的话,必须确保RemoteService当中的valueChanged方法不是运行在主UI当中,不然会导致服务端无法响应。

同理:在客户端调用服务端的方法的时候,如果服务端的方法比较耗时,我们就得避免在客户端的UI线程当中去访问远程方法,不然会导致客户端无响应。

  1. private IRemoteServiceCallback mCallback = new IRemoteServiceCallback.Stub() {
  2. /**
  3. * This is called by the remote service regularly to tell us about
  4. * new values. Note that IPC calls are dispatched through a thread
  5. * pool running in each process, so the code executing here will
  6. * NOT be running in our main thread like most other things -- so,
  7. * to update the UI, we need to use a Handler to hop over there.
  8. */
  9. public void valueChanged(int value) {
  10. mHandler.sendMessage(mHandler.obtainMessage(BUMP_MSG, value, 0));
  11. }
  12. };

mHandler的实现如下所示:

  1. private Handler mHandler = new Handler() {
  2. @Override
  3. public void handleMessage(Message msg) {
  4. switch (msg.what) {
  5. case BUMP_MSG:
  6. mCallbackText.setText("Received from service: " + msg.arg1);
  7. break;
  8. default:
  9. super.handleMessage(msg);
  10. }
  11. }
  12. };

2、点击UNBIND SERVICE按钮的时候,需要先解注册之前注册的I RemoteServiceCallback回调接口,然后再unbindService。

3、在执行bindService的时候,代码如下所示,第三个参数有几个可选项,一般选Context.BIND_AUTO_CREATE,意思是如果在绑定过程中,Service进程被意外杀死了,系统还会自动重新启动被绑定的Service。所以当我们点击KILL PROCESS按钮的时候会杀死Service进程,但是马上又会自动重启,重新调用onServiceConnected方法重新绑定。当然,这个参数还有别的一些选择。

  1. bindService(intent,
  2. mConnection, Context.BIND_AUTO_CREATE);

主Activity代码:

  1. package com.easyliu.demo.aidldemo;
  2. import android.app.Activity;
  3. import android.content.ComponentName;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.content.ServiceConnection;
  7. import android.os.Handler;
  8. import android.os.IBinder;
  9. import android.os.Message;
  10. import android.os.Process;
  11. import android.os.RemoteException;
  12. import android.support.v7.app.AppCompatActivity;
  13. import android.os.Bundle;
  14. import android.view.View;
  15. import android.widget.Button;
  16. import android.widget.TextView;
  17. import android.widget.Toast;
  18. import com.easyliu.demo.aidl.IRemoteService;
  19. import com.easyliu.demo.aidl.IRemoteServiceCallback;
  20. import com.easyliu.demo.aidl.ISecondary;
  21. public class BindActivity extends AppCompatActivity {
  22. IRemoteService mService = null;
  23. ISecondary mSecondaryService = null;
  24. Button mKillButton;
  25. TextView mCallbackText;
  26. private boolean mIsBound;
  27. private static final int BUMP_MSG = 1;
  28. @Override
  29. protected void onCreate(Bundle savedInstanceState) {
  30. super.onCreate(savedInstanceState);
  31. setContentView(R.layout.remote_service_binding);
  32. Button button = (Button) findViewById(R.id.bind);
  33. button.setOnClickListener(mBindListener);
  34. button = (Button) findViewById(R.id.unbind);
  35. button.setOnClickListener(mUnbindListener);
  36. mKillButton = (Button) findViewById(R.id.kill);
  37. mKillButton.setOnClickListener(mKillListener);
  38. mKillButton.setEnabled(false);
  39. mCallbackText = (TextView) findViewById(R.id.callback);
  40. mCallbackText.setText("Not attached.");
  41. }
  42. private Handler mHandler = new Handler() {
  43. @Override
  44. public void handleMessage(Message msg) {
  45. switch (msg.what) {
  46. case BUMP_MSG:
  47. mCallbackText.setText("Received from service: " + msg.arg1);
  48. break;
  49. default:
  50. super.handleMessage(msg);
  51. }
  52. }
  53. };
  54. /**
  55. * Class for interacting with the main interface of the service.
  56. */
  57. private ServiceConnection mConnection = new ServiceConnection() {
  58. public void onServiceConnected(ComponentName className,
  59. IBinder service) {
  60. // This is called when the connection with the service has been
  61. // established, giving us the service object we can use to
  62. // interact with the service. We are communicating with our
  63. // service through an IDL interface, so get a client-side
  64. // representation of that from the raw service object.
  65. mService = IRemoteService.Stub.asInterface(service);
  66. mKillButton.setEnabled(true);
  67. mCallbackText.setText("Attached.");
  68. // We want to monitor the service for as long as we are
  69. // connected to it.
  70. try {
  71. mService.registerCallback(mCallback);
  72. } catch (RemoteException e) {
  73. // In this case the service has crashed before we could even
  74. // do anything with it; we can count on soon being
  75. // disconnected (and then reconnected if it can be restarted)
  76. // so there is no need to do anything here.
  77. }
  78. // As part of the sample, tell the user what happened.
  79. Toast.makeText(BindActivity.this, R.string.remote_service_connected,
  80. Toast.LENGTH_SHORT).show();
  81. }
  82. public void onServiceDisconnected(ComponentName className) {
  83. // This is called when the connection with the service has been
  84. // unexpectedly disconnected -- that is, its process crashed.
  85. mService = null;
  86. mKillButton.setEnabled(false);
  87. mCallbackText.setText("Disconnected.");
  88. // As part of the sample, tell the user what happened.
  89. Toast.makeText(BindActivity.this, R.string.remote_service_disconnected,
  90. Toast.LENGTH_SHORT).show();
  91. }
  92. };
  93. /**
  94. * Class for interacting with the secondary interface of the service.
  95. */
  96. private ServiceConnection mSecondaryConnection = new ServiceConnection() {
  97. public void onServiceConnected(ComponentName className,
  98. IBinder service) {
  99. // Connecting to a secondary interface is the same as any
  100. // other interface.
  101. mSecondaryService = ISecondary.Stub.asInterface(service);
  102. mKillButton.setEnabled(true);
  103. }
  104. public void onServiceDisconnected(ComponentName className) {
  105. mSecondaryService = null;
  106. mKillButton.setEnabled(false);
  107. }
  108. };
  109. /**
  110. * 绑定
  111. */
  112. private View.OnClickListener mBindListener = new View.OnClickListener() {
  113. public void onClick(View v) {
  114. Intent intent=new Intent(IRemoteService.class.getName());
  115. intent.setPackage("com.easyliu.demo.aidldemo");
  116. //注意这里的Context.BIND_AUTO_CREATE,这意味这如果在绑定的过程中,
  117. //如果Service由于某种原因被Destroy了,Android还会自动重新启动被绑定的Service。
  118. // 你可以点击Kill Process 杀死Service看看结果
  119. bindService(intent,
  120. mConnection, Context.BIND_AUTO_CREATE);
  121. intent=new Intent(ISecondary.class.getName());
  122. intent.setPackage("com.easyliu.demo.aidldemo");
  123. bindService(intent,
  124. mSecondaryConnection, Context.BIND_AUTO_CREATE);
  125. mIsBound = true;
  126. mCallbackText.setText("Binding.");
  127. }
  128. };
  129. /**
  130. * 解除绑定
  131. */
  132. private View.OnClickListener mUnbindListener = new View.OnClickListener() {
  133. public void onClick(View v) {
  134. if (mIsBound) {
  135. if (mService != null) {
  136. try {
  137. mService.unregisterCallback(mCallback);
  138. } catch (RemoteException e) {
  139. // There is nothing special we need to do if the service
  140. // has crashed.
  141. }
  142. }
  143. // Detach our existing connection.
  144. unbindService(mConnection);
  145. unbindService(mSecondaryConnection);
  146. mKillButton.setEnabled(false);
  147. mIsBound = false;
  148. mCallbackText.setText("Unbinding.");
  149. }
  150. }
  151. };
  152. private View.OnClickListener mKillListener = new View.OnClickListener() {
  153. public void onClick(View v) {
  154. // To kill the process hosting our service, we need to know its
  155. // PID. Conveniently our service has a call that will return
  156. // to us that information.
  157. if (mSecondaryService != null) {
  158. try {
  159. int pid = mSecondaryService.getPid();
  160. // Note that, though this API allows us to request to
  161. // kill any process based on its PID, the kernel will
  162. // still impose standard restrictions on which PIDs you
  163. // are actually able to kill. Typically this means only
  164. // the process running your application and any additional
  165. // processes created by that app as shown here; packages
  166. // sharing a common UID will also be able to kill each
  167. // other's processes.
  168. Process.killProcess(pid);
  169. mCallbackText.setText("Killed service process.");
  170. } catch (RemoteException ex) {
  171. // Recover gracefully from the process hosting the
  172. // server dying.
  173. // Just for purposes of the sample, put up a notification.
  174. Toast.makeText(BindActivity.this,
  175. R.string.remote_call_failed,
  176. Toast.LENGTH_SHORT).show();
  177. }
  178. }
  179. }
  180. };
  181. /**
  182. * 远程回调接口实现
  183. */
  184. private IRemoteServiceCallback mCallback = new IRemoteServiceCallback.Stub() {
  185. /**
  186. * This is called by the remote service regularly to tell us about
  187. * new values. Note that IPC calls are dispatched through a thread
  188. * pool running in each process, so the code executing here will
  189. * NOT be running in our main thread like most other things -- so,
  190. * to update the UI, we need to use a Handler to hop over there.
  191. */
  192. public void valueChanged(int value) {
  193. mHandler.sendMessage(mHandler.obtainMessage(BUMP_MSG, value, 0));
  194. }
  195. };
  196. }

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/199136
推荐阅读
相关标签
  

闽ICP备14008679号