打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
绑定service
当一个Activity绑定到一个Service上时,它负责维护Service实例的引用,允许你对正在运行的Service进行一些方法调用。
  • 1实现ServiceConnection.

    你的实现必须重写两个回调方法:

    • onServiceConnected()

      系统调用这个来传送在serviceonBind()中返回的IBinder

    • OnServiceDisconnected()

      Android系统在同service的连接意外丢失时调用这个.比如当service崩溃了或被强杀了.当客户端解除绑定时,这个方法不会被调用.


  • LocalService mService;  
  • private ServiceConnection mConnection = new ServiceConnection() {  
  •     // 当与service的连接建立后被调用  
  •     public void onServiceConnected(ComponentName className, IBinder service) {  
  •         // Because we have bound to an explicit  
  •         // service that is running in our own process, we can  
  •         // cast its IBinder to a concrete class and directly access it.  
  •         LocalBinder binder = (LocalBinder) service;  
  •         mService = binder.getService();  
  •         mBound = true;  
  •     }  
  •   
  •     // 当与service的连接意外断开时被调用  
  •     public void onServiceDisconnected(ComponentName className) {  
  •         Log.e(TAG, "onServiceDisconnected");  
  •         mBound = false;  
  •     }  
  • };  
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Activity 和Service绑定
关于Service的生命周期,以及ServiceConnection接口时注意的东东
Android中的Service的通信
5. 客户端与服务之间通信
android笔记
Android开发者指南 -Bound服务
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服