美文网首页android
IPC 设置死亡代理

IPC 设置死亡代理

作者: 静享时光 | 来源:发表于2020-05-05 23:45 被阅读0次

Binder是可能以外死亡的,这往往是由于服务端进程意外停止了,这是我们需要重新连接服务。有两种方法。

方法一

第一种方法时给Binder设置DeathReciient监听,当Binder死亡是,我们就会受到binderDied方法的回调,在binderDied方法中我们可以重连远程服务。
需要在Activity中进行修改

    /**
     * 设置死亡代理
     */
    private IBinder.DeathRecipient mDeathRecipient = new IBinder.DeathRecipient() {

        @Override
        public void binderDied() {
            if (iBookManager == null) {
                return;
            }
            iBookManager.asBinder().unlinkToDeath(mDeathRecipient, 0);
            iBookManager = null;
            //重新绑定服务
            bindMyService();
        }
    };

    /**
     * 绑定服务
     */
    private void bindMyService() {
        Intent serviceIntent = new Intent(this, BookManagerService.class);
        serviceConnection = new ServiceConnection() {

            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                iBookManager = IBookManager.Stub.asInterface(service);
                try {
                    //设置死亡代理
                    iBookManager.asBinder().linkToDeath(mDeathRecipient, 0);
                    //注册监听
                    iBookManager.registerListener(iOnNewBookArrvedListener);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                iBookManager = null;
            }
        };
        bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE);

    }
方法二

在onServiceDisconnected方法中重连远程服务

两种方法的区别

onServiceDisconnected运行在ui线程中被回调,而binderDied在客户端的Binder线程池中被回调。所以在binderDied方法中不能访问UI。

相关文章

  • IPC 设置死亡代理

    Binder是可能以外死亡的,这往往是由于服务端进程意外停止了,这是我们需要重新连接服务。有两种方法。 方法一 第...

  • 代理设置

    终端代理设置 wget设置代理 curl 设置代理 git 设置代理 ssh 代理 nc命令 网络工具 digns...

  • git ssh代理设置

    终端代理设置 wget设置代理 curl 设置代理 git 设置代理 ssh 代理 nc命令 网络工具dignsl...

  • git 设置代理,增加下载速度

    设置代理: 取消代理 Mac 终端设置代理:

  • Git 扩展之全局配置

    配置代理 设置代理 取消代理 查看当前已设置的代理

  • 【Python - 爬虫】爬虫代理

    设置 urllib 设置代理 Requests 设置代理 selenium 设置代理 获取免费IP 参考链接 Py...

  • git设置及取消代理

    git设置及取消代理//首先,设置默认代理,也可以理解为清除代理 //设置代理

  • git设置代理

    设置全局代理 取消全局代理 设置单独github代理

  • 2021-09-22

    Windows cmd 设置代理设置 HTTP 代理: socks5代理设置: 取消代理: Windows git...

  • UICollectionView Cell设置cell的行间距,

    记得设置代理,记得设置代理,记得设置代理..... 方法1.设置行间距 -(CGFloat )collecti...

网友评论

    本文标题:IPC 设置死亡代理

    本文链接:https://www.haomeiwen.com/subject/nzubghtx.html