Service的onUnbind方法不执行的问题

这里只讨论跨应用的bindService。首先说一下场景:
client在SDK,Service在Server APP(以下Server和Service意思相同,均指的是Server APP),并且Server APP某一时刻只允许与一个APP进行通讯,即:应用A通过bindService连接上Server APP之后,Server APP会认为只有应用A的请求为有效请求,直到应用A调用unbindService或者被强制杀掉,其他应用才可以向Server APP发出连接和请求。

我们都知道,当bindService的时候,如果client被强制杀死,或主动调用unbindService,那么Service端的onUnbind方法都会被调用,这是针对只有一个Client的情况,假设对同一个Service,client A调用了bindService,client B也调用了bindService,那么client A和client B都能和Service通信,但是其中一个client被杀掉,onUnBind方法并不会被调用,只有client A和client B都被杀掉,onUnbind方法才会回调。这样,我们就清楚了,调用过bindService方法连接同一个Service的client全部被杀掉,Service的onUnbind方法才会回调。

所以,我的解决方案是,client A通过bindService连接上Server之后,Server会记住当前client A的包名,只有client A的请求才会处理,其他都是非法客户端,当client B通过bindService,并调用自定义的connect方法,Server会校验client B的包名,发现和自己记住的包名不一致,就返回“连接失败”给client B。然后client B收到连接错误,要调用unbindService以断绝和Server的关系,注意:此时调用unbindService并不会使Server的onUnbind方法调用,因为client A还连接着呢。这样,当client A主动调用unbindService或者被强制杀掉的时候,onUnbind方法就会正确回调,并在这个回调里执行状态复位等操作。

public class EasyService extends Service {
    private static HomeKeyReceiver mHomeKeyReceiver = null;

    private IEasyLink.Stub mBinder = new IEasyLink.Stub() {

        @Override
        public byte[] sendAndReceive(ClientInfo clientInfo, byte[] sendData) throws RemoteException {
            LogUtils.d("Thread.currentThread().getId() sendAndReceive tid:" + Thread.currentThread().getId());
            return new MsgDispatcher().dispatchMsg(clientInfo, sendData);
        }

        @Override
        public boolean isConnected(ClientInfo clientInfo) throws RemoteException {
            return Validator.isConnected(clientInfo);
        }
    };


    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        registerHomeKeyReceiver(EasyService.this);
        return mBinder;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        // Restore the data to initial state, eg: Restore the connection state.
        EasyLinkApplication.getMsgProcessInfo().setConnectClientInfo(null);
        EasyLinkApplication.getMsgProcessInfo().setConnect(false);
        unregisterHomeKeyReceiver(EasyService.this);
        stopSelf();
        return super.onUnbind(intent);
    }

    // a broadcast receiver to listener home key done
    private static void registerHomeKeyReceiver(Context context) {
        mHomeKeyReceiver = new HomeKeyReceiver();
        final IntentFilter homeFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
        context.registerReceiver(mHomeKeyReceiver, homeFilter);
    }

    private static void unregisterHomeKeyReceiver(Context context) {
        if (null != mHomeKeyReceiver) {
            context.unregisterReceiver(mHomeKeyReceiver);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ithouse

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值