Android Service详解
来源:互联网 发布:对方qq网络状态准确吗 编辑:程序博客网 时间:2024/06/18 17:46
Service的基本概念和用途:
Android中的服务,它与Activity不同,它是不能与用户交互的,不能自己启动的,运行在后台的程序,如果退出应用时,Service进程并没有结束,它仍然在后台运行,那什么时候会用到Service呢?比如播放音乐的时候,有可能想边听音乐边干些其他事情,当退出播放音乐的应用,如果不用Service,就听不到歌了,所以这时候就得用到Service了。又比如当一个应用的数据是通过网络获取的,不同时间的数据是不同的时候可以用Service在后台定时更新,而无须每打开应用的时候在去获取。
Service在Manifest.xml文件中像Activity一样,需要进行配置一下。
Service的生命周期:
Service分为:startService、bindService.
1、startService:
onCreate()
onStartCommand()
onStart()
onDestroy()
2、bindService:
onCreate()
onBind()
onServiceConnected()
onUnbind();
onDestroy()
四个功能【startService(), stopService(), bindService(), unbindService()】
流程:
- startService ---> stopService 生命周期, onCreate() --> onStartCommand() --> onStart() --> onDestroy();
- bindService ---> unbindService 生命周期, onCreate() --> onBind() --> onServiceConnected() --> onUnbind() --> onDestroy();
- startService ---> bindService --> stopService ---> unBindService 生命周期, onCreate() --> onStartCommand() --> onStart() -->onBind() -->onServiceConnected() --> onUnbind() --> onDestroy();
- startService --> bindService --> unBindService --> stopService 生命周期,onCreate() --> onStartCommand() --> onStart() --> onBind() --> onServiceConnected() --> onUnbind() --> onDestroy();
BindService并且与Activity进行交互:
ServiceConnection serviceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { Log.e(KEY_TAG, "==================================onServiceConnected"); if(iBinder != null){ mBinderCreator = (ServiceCreator.BinderCreator)iBinder; } } @Override public void onServiceDisconnected(ComponentName componentName) { Log.e(KEY_TAG, "==================================onServiceDisconnected"); } };
前台Service:当内存不足时,不易被系统kill。
前台Service,相对与后台Service,优先级较高【当系统内存不足时,会优先Kill 后台Service】
阅读全文
0 0
- Android Service详解(1)
- Android Service 详解
- 【android】Service详解
- android之Service详解
- Android Service用法详解
- android service基础知识详解
- android service详解
- Android SERVICE(详解一)
- Android 中的 Service详解
- Android的Service详解
- android Service详解
- android service 详解
- android service详解!
- Android Service详解
- Android Service 详解
- android service详解
- Android-service详解
- Android Service 详解
- 15算法课程 27. Remove Element
- caffe 里eltwise layer 的用法
- 不借助任何变量进行变量交换
- Java课后思考题
- Glide 使用说明
- Android Service详解
- RTC技术栈
- Go1.9不使用http2提高http传输效率
- [自用门户]飘窗
- java面向对象编程(1)
- Androi Studio 3.0去掉方法参数提示
- windows7 nmap 命令行版本
- 机器视觉光源选型总结---同轴光原理与应用
- NDK开发——nginx-rtmp流媒体服务器搭建