使用AIDL实现IPC通信之——实现服务端主动发送数据给客户端(转)

作者: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


©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 202,980评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,178评论 2 380
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,868评论 0 336
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,498评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,492评论 5 364
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,521评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,910评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,569评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,793评论 1 296
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,559评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,639评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,342评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,931评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,904评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,144评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,833评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,350评论 2 342

推荐阅读更多精彩内容

  • 1.内核对象 操作系统创建的资源有很多种,如进程、线程、文件及信号量、互斥量等。其中大部分都是通过程序员的请求创建...
    砥弦阅读 474评论 0 0
  • 人生百态,每个人的生活方式都不一样。所遇到的事情和所追求的理想各有各的价值观点。 每天怀抱一颗善良平常心。认真对待...
    沐夕读书阅读 170评论 0 5
  • 10kv验电器在验电时安全是最重要的,那么我们怎么操作才能确保安全性呢? 第一步,在使用10kv验电器前做好一切检...
    雨雪霏霏44阅读 634评论 1 2
  • 以前总以为自己不说,不管,任凭别人怎么做,怎么想,做好自己就可以,后来才知道这样这样做最后伤害的只是自己,总有那么...
    踏雪寻梅_0afc阅读 233评论 0 0