startService和bindService
来源:互联网 发布:更改4g移动数据dns 编辑:程序博客网 时间:2024/06/07 20:07
startService和bindService同时调用后, 必须对应调用stop和unbind,Service才会destroy。
一个已经存活的Service被bind过,然后解绑后重新bind,Service的onBind方法可能不会再次被调用,因为intent相同:
public boolean filterEquals(Intent other) { if (other == null) { return false; } // 比较Intent中的各项指标:Action,Uri,MIME type,包名,Component,Category if (!Objects.equals(this.mAction, other.mAction)) return false; if (!Objects.equals(this.mData, other.mData)) return false; if (!Objects.equals(this.mType, other.mType)) return false; if (!Objects.equals(this.mPackage, other.mPackage)) return false; if (!Objects.equals(this.mComponent, other.mComponent)) return false; if (!Objects.equals(this.mCategories, other.mCategories)) return false; return true; }
官方文档中说,如果多个client绑定服务,那么服务只有在第一次会执行onBind,如下:
如果你的Service被start, 并且被bind,那么当解除所有绑定, 系统调用onUnbind的, 你可以在onUnbind中返回true, 这样你你可以在下次bindService的时候, 可以回调onRebind(不会回调onBind了, 放心吧), onRebind方法返回值void, 但是client仍然可以拿到IBinder对象(这是因为IBind对象被系统缓存起来了),onUnbind中返回false,那么依然会回调onBind.
public interface ServiceConnection { /** * Called when a connection to the Service has been established, with * the {@link android.os.IBinder} of the communication channel to the * Service. * * @param name The concrete component name of the service that has * been connected. * * @param service The IBinder of the Service's communication channel, * which you can now make calls on. */ public void onServiceConnected(ComponentName name, IBinder service); /** * Called when a connection to the Service has been lost. This typically * happens when the process hosting the service has crashed or been killed. * This does <em>not</em> remove the ServiceConnection itself -- this * binding to the service will remain active, and you will receive a call * to {@link #onServiceConnected} when the Service is next running. * * @param name The concrete component name of the service whose * connection has been lost. */ public void onServiceDisconnected(ComponentName name);}
onServiceDisconnected只有在Service链接异常丢失的时候才会调用,主动unbindService,不会调用onServiceDisconnected。
onServiceConnected和onServiceDisconnected都是在UI线程调用.
阅读全文
1 0
- startService和bindService区别
- startService和bindService
- startService和bindService区别
- startservice 和bindservice详解
- bindService()和startService()区别
- startService和bindService
- Android StartService和BindService
- Android startService 和 bindService
- 理解startservice和bindservice
- startService和bindService
- Android startservice和bindservice
- startService()和bindService()区别
- startservice()和bindservice()区别
- startService()和bindService()区别
- startService和bindService区别
- startService和bindService
- startService 和 bindService的区别
- android 开发StartService 和BindService
- 剑指Offer-57
- node.js模块之exports
- groovy
- 技术文章 | seo外链优化需要规避的那些"坑"
- 实体处理模块IEntityModule
- startService和bindService
- 阿里云大数据利器之-RDS迁移到Maxcompute实现自动分区
- cookie
- HTML5屏幕适配标签设置
- gulp常用插件整理
- github常用指令
- react moment 时间管理
- linux常见命令操作
- Kubernetes与分布式存储系统VeSpace结合实践