AIDL基本使用4—- 4、linkToDeath和unlinkToDeath
来源:互联网 发布:怎么写好网络小说知乎 编辑:程序博客网 时间:2024/06/05 22:37
完全参考 任玉刚老师的书籍案例案例
Binder运行在服务端进程,如果服务端进程由于某些原因异常终止,这个时候我们到服务端的Binder连接断裂,会导致我们的远程调用失败。Binder提供了两个配对的方法linkToDeath和unlinkToDeath,通过linkToDeath我们可以给Binder设置一个死亡代理,当Binder死亡时,我们会收到通知,这个时候我们就可以重新发起连接请求从而恢复连接。
package com.ucoupon.client2;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.os.RemoteException;import android.support.v7.app.AppCompatActivity;import android.util.Log;import com.ucoupon.myservice.IMyAidlInterface;public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //连接管理 Myconnect myconnect = new Myconnect(); //意图 Intent intent = new Intent();// intent.setPackage("com.ucoupon.myservice"); intent.setClassName("com.ucoupon.myservice", "com.ucoupon.myservice.MyService"); startService(intent); //开始绑定服务 bindService(intent, myconnect, BIND_AUTO_CREATE); } private static final String TAG = "client2"; //死亡接受者 IBinder.DeathRecipient deathRecipient = new IBinder.DeathRecipient() { @Override public void binderDied() { if (iMyAidlInterface != null) { //注销监听和回收资源 iMyAidlInterface.asBinder().unlinkToDeath(this, 0); iMyAidlInterface = null; } } }; private IMyAidlInterface iMyAidlInterface; class Myconnect implements ServiceConnection { //连接的成功的时候回调 @Override public void onServiceConnected(ComponentName name, IBinder service) { Log.d(TAG, "onServiceConnected() called with: name = [" + name + "], service = [" + service + "]"); iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service); try { //连接死亡监听 service.linkToDeath(deathRecipient, 0); } catch (RemoteException e) { e.printStackTrace(); } try { String msg = iMyAidlInterface.basicTypes(1, "客户端传入的"); } catch (RemoteException e) { e.printStackTrace(); } //解绑服务// unbindService(myconnect); } //断开连接的时候回调 @Override public void onServiceDisconnected(ComponentName name) { Log.d(TAG, "onServiceDisconnected() called with: name = [" + name + "]"); } }}
tip:大家bind服务的时候,进入设置去停止服务就可以看到回调
阅读全文
0 0
- AIDL基本使用4—- 4、linkToDeath和unlinkToDeath
- android 之 linkToDeath和unlinkToDeath。(死亡代理)
- linkToDeath机制了解和使用
- linkToDeath机制了解和使用
- AIDL基本使用2— 传递对象
- AIDL的基本使用
- AIDL的基本使用
- AIDL基本使用(二)
- AIDL基本使用3—-in out inout的用
- aidl使用和注意事项
- AIDL基本使用1----调用服务端方法
- AIDL的基本使用(一)
- AIDL 跨进程通信方式,的基本使用和源码理解
- 使用IBinder的linkToDeath机制来释放资源
- Binder反向调用和linkToDeath来实现资源清理
- 使用aidl设计和使用远程接口
- AIDL——使用步骤
- aidl 的简单介绍和使用
- lintcode 在二叉查找树中插入节点
- 制作mac系统正式版U盘USB启动安装盘方法教程 (全新安装 Mac 系统)
- CentOS6.7 上面安装Docker 1.7.1
- zeppelin spark SparkContext问题 Cannot call methods on a stopped SparkContext
- 静态编译Qt
- AIDL基本使用4—- 4、linkToDeath和unlinkToDeath
- the object has no symbol
- LDD3源码分析之调试技术
- Windows下设置nginx开机自启
- POJ2411
- 两数组的交
- freemarker项目实践
- Android kotlin开发基础篇 基础语法详解--pdf
- unit2-高级网络配置【bond,team,br0,ipv6】