1.大致流程

  • 在服务端声明远程服务接口IRemoteService.aidl,并声明回调接口ICallback.aidl
  • 在服务端实现远程服务接口IRemoteService.Stub
  • 使用RemoteCallbackList保存回调接口列表
  • 发布服务
  • 在客户端实现回调接口 ICallback.Stub
  • 绑定服务,注册回调接口
  • 调用服务
  • 远程服务从RemoteCallbackList中找到回调,然后调用.

2.服务端

2.1 声明服务接口IRemoteService.aidl

 // IRemoteService.aidl
 package com.example.ee.aidl;

 import com.example.ee.aidl.ICallback;

 interface IRemoteService {

     int getPid();
     oneway void fun1();
     void registerCallback(ICallback cb);
     void unregisterCallback(ICallback cb);
 }

2.2 声明回调接口ICallback.aidl

 // ICallback.aidl
 package com.example.ee.aidl;

 interface ICallback {
     oneway void fun2();
 }

2.3 实现服务,用 RemoteCallbackList 保存回调接口.

 package com.example.ee.aidl;

 import android.app.Service;
 import android.content.Intent;
 import android.os.IBinder;
 import android.os.Process;
 import android.os.RemoteCallbackList;
 import android.os.RemoteException;
 import android.support.annotation.Nullable;
 import android.util.Log;

 public class RemoteService extends Service {
     final String TAG = "RemoteService";

     private RemoteBinder binder = new RemoteBinder();
     private RemoteCallbackList<ICallback>  callbackList = new RemoteCallbackList<>();

     @Nullable
     @Override
     public IBinder onBind(Intent intent) {
         return binder;
     }

     class RemoteBinder extends IRemoteService.Stub{

         void callback(){
             int count = callbackList.beginBroadcast();
             try {
                 ; i < count; i++) {
                     callbackList.getBroadcastItem(i).fun2();
                 }
             } catch (RemoteException e) {
                 Log.e(TAG, e.getMessage());
             } finally {
             }
             callbackList.finishBroadcast();
         }

         @Override
         public int getPid() throws RemoteException {
             return Process.myPid();
         }

         @Override
         public void fun1() throws RemoteException {
             Log.d(TAG, "fun1 in remote service ");
             callback();
         }

         @Override
         public void registerCallback(ICallback cb) throws RemoteException {
             callbackList.register(cb);
         }

         @Override
         public void unregisterCallback(ICallback cb) throws RemoteException {
             callbackList.unregister(cb);
         }
     }
 }

2.4 发布服务

 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.example.ee.aidl">

     <application
         android:allowBackup="false"
         android:icon="@mipmap/ic_launcher"
         android:label="@string/app_name"
         android:roundIcon="@mipmap/ic_launcher_round"
         android:supportsRtl="true"
         android:name=".AidlApp"
         android:theme="@style/AppTheme">
         <!--<meta-data
             android:name="APP_CHANNEL"
             android:value="${APP_CHANNEL}" />
 -->
         <activity android:name=".MainActivity">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />

                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
         </activity>
         <service android:name=".RemoteService" android:process=".RemoteService" android:exported="true"/>
     </application>

 </manifest>

3.客户端

3.1 实现回调接口

     private ICallback callback  = new ICallback.Stub(){

         @Override
         public void fun2() throws RemoteException {
             Log.d(TAG, "fun2 in client ");
         }
     };

3.2 绑定远程服务,注册回调

     private void bindRemoteService(){
         connection = new ServiceConnection() {
             @Override
             public void onServiceConnected(ComponentName name, IBinder service) {
                 remoteService = IRemoteService.Stub.asInterface(service);
                 try {
                     remoteService.registerCallback(callback);
                 } catch (RemoteException e) {
                     e.printStackTrace();
                 }
             }

             @Override
             public void onServiceDisconnected(ComponentName name) {

             }
         };

         Intent intent = new Intent(this,RemoteService.class);
         boolean ret = bindService(intent,connection, BIND_AUTO_CREATE);
         if (!ret ){
             Log.e(TAG, "bindRemoteService: bind failed.");
         }
     }

3.3 调用远程服务

     public void onClick(View view){
         if(view.getId() == R.id.btn_call_remote){
             AidlApp app = (AidlApp) getApplication();
             if (app.remoteService != null){
                 try {
                     int pid = app.remoteService.getPid();
                     Log.d(TAG, "onServiceConnected: remote pid = " + pid);
                     app.remoteService.fun1();
                 } catch (RemoteException e) {
                     e.printStackTrace();
                 }
             }else {
                 Snackbar.make(view,"remote service unbinded",Snackbar.LENGTH_SHORT).show();
             }
         }
     }

4. 远程服务调用回调

     void callback(){
             int count = callbackList.beginBroadcast();
             try {
                 ; i < count; i++) {
                     callbackList.getBroadcastItem(i).fun2();
                 }
             } catch (RemoteException e) {
                 Log.e(TAG, e.getMessage());
             } finally {
             }
             callbackList.finishBroadcast();
     }

5.下载

  https://gitee.com/xi/RemoteCallbackList.git

最新文章

  1. OLTP(on-line transaction processing)与OLAP(On-Line Analytical Processing)
  2. 制作dll自动注册工具
  3. Nginx 利用 X-Accel-Redirect response.setHeader 控制文件下载
  4. [Effective JavaScript 笔记]第63条:当心丢弃错误
  5. atitit.重装系统需要备份的资料总结 o84..
  6. SQLServer数据库字典维护方法
  7. OC中-方法到底是如何使用的?
  8. 04_XML_03_XMLDTD约束与校验
  9. Web常用函数介绍(LoadRunner相关)
  10. php二进制流文件
  11. Python实战之dict简单练习
  12. Oracle的窗口和自动任务
  13. hdu_1015(dfs)
  14. June. 26th 2018, Week 26th. Tuesday
  15. springboot自带定时任务和集成quartz
  16. centos7.4下Jira6环境部署及破解操作记录(完整版)
  17. php打乱数组二维数组、多维数组
  18. .net 高并发 多消费者模式处理订单
  19. Django - Python3 配置 MySQL
  20. day26-保护属性

热门文章

  1. Codeforces 427E Police Patrol
  2. SQL修改字段类型
  3. 2.8.2 并发下的ArrayList,以及源码分析
  4. Row_Number() OVER()函数使用举例
  5. TSQL--删除正在运行的数据库
  6. JAVA8 Lambda 表达式使用心得
  7. 基于CentOS6定制自己的ISO安装光盘
  8. System.Net.Http
  9. Android学习笔记 ImageSwitcher图片切换组件的使用
  10. 为什么 kubernetes 天然适合微服务 (1)