ServiceConnection接口的使用
来源:互联网 发布:云编程 编辑:程序博客网 时间:2024/04/30 10:09
首先是Service的周期问题
The service will at this point continue running until Context.stopService() or stopSelf() is called. Note that multiple calls to Context.startService() do not nest (though they do result in multiple corresponding calls to onStartCommand()), so no matter how many times it is started a service will be stopped once Context.stopService() or stopSelf() is called
上面是android的HP上写的关于Service周期的一段话,是说,Service在开始以后,除非调用了Context.stopService()或者stopSelf(),否则不会停止。当然如果内存不足,系统可能自动杀死Service。
下面是关于ServiceConnection接口的:
人会想,如果执行unbindService之后不就自动调用ServiceConnection接口里的onServiceDisconnected method了吗。
其实不然,查看api,你会发现,ServiceConnection接口里德onServiceDisconnected method只会在Service被停止或者被系统杀死以后调用。
也就是说你执行unbindService 只是告诉系统你已经和这个服务没有关系了。系统在内存不足的时候可以优先杀死这个服务而已。
The service will at this point continue running until Context.stopService() or stopSelf() is called. Note that multiple calls to Context.startService() do not nest (though they do result in multiple corresponding calls to onStartCommand()), so no matter how many times it is started a service will be stopped once Context.stopService() or stopSelf() is called
上面是android的HP上写的关于Service周期的一段话,是说,Service在开始以后,除非调用了Context.stopService()或者stopSelf(),否则不会停止。当然如果内存不足,系统可能自动杀死Service。
下面是关于ServiceConnection接口的:
private ServiceConnection conn = new ServiceConnection() {@Overridepublic void onServiceDisconnected(ComponentName name) {// TODO Auto-generated method stubmService = null;}@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {// TODO Auto-generated method stubmService = IBindService.Stub.asInterface(service);}};貌似现在的主流写法是这样。下面的代码的bindService method执行之后会自动调用上面ServiceConnection接口里的onServiceConnected method
Intent intent = new Intent(IBindService.class.getName());bindService(intent, conn, BIND_AUTO_CREATE);
人会想,如果执行unbindService之后不就自动调用ServiceConnection接口里的onServiceDisconnected method了吗。
其实不然,查看api,你会发现,ServiceConnection接口里德onServiceDisconnected method只会在Service被停止或者被系统杀死以后调用。
也就是说你执行unbindService 只是告诉系统你已经和这个服务没有关系了。系统在内存不足的时候可以优先杀死这个服务而已。
0 0
- ServiceConnection接口的使用
- Android ServiceConnection的应用
- ServiceConnection
- 关于Service的生命周期,以及ServiceConnection接口时注意的东东
- 出现leaked ServiceConnection 的解决办法
- 出现leaked ServiceConnection 的解决办法
- 出现leaked ServiceConnection 的解决办法
- 调用了bindService()之后,程序执行了Service里的onBind(),但是为什么不执行ServiceConnection对象接口onServiceConnected()呢?
- 不回调ServiceConnection中onServiceConnected()方法的问题
- 在使用百度地图的SDK时,出现.LocationDemo has leaked ServiceConnection 异常并且无法定位的时候
- 解决百度地图使用出现的has leaked ServiceConnection com.baidu.location.b@4aa5a810 that was originally bound here
- 百度地图定位功能的错误has leaked ServiceConnection 解决
- Error:android.app.ServiceConnectionLeaked:就是leaked ServiceConnection的错误
- Android ServiceConnection
- Android-ServiceConnection
- Android ServiceConnection
- Android ServiceConnection
- andorid serviceconnection
- django 乱码 汉语支持
- IOS 之 KVC & KVO
- 第十二周项目一——阅读程序(2)
- 记录今天遇到的两个问题
- about your legs and feetThe accident ha
- ServiceConnection接口的使用
- 抽象类 & 接口
- VBScript近似Visio的自动适应画布功能
- android 动态布局
- 一体化点焊机将要取代分体式焊钳在汽车制造生产线上的使用
- Cocos2d-x 3.x开发——导入Cocostudio资源
- MATLAB Floyd算法
- Android Service的生命周期和IntentService的用法
- 谈下自己做的一个办公系统的感受