必须谨记的几点:
1. service的所有onXXXX( )都是运行在main thread,包括onStartCommand()。Because all service's onXXXX() functions are called via handleMessage() of handler which is created with the main thread's loop.
client的onServiceConnected()和onServiceDisonnected()也都是运行在main thread,because they are also called via handler which is created with the main thread's loop,
2. All IPC are executed via binder and executed in the binder thread. Android app framework will create binder thread during startup which is created using native code(such thread may be spawned if necessary,系统会维护一个binder thread的线程池,以保证所有的IPC都可以被handle).
3. onStartCommand()不需要考虑线程安全,因为onStartCommand() is executed in the main thread in turn。But if we use bound service, 需要考虑线程安全,因为all IPC are executed in the binder thread(binder thread maybe multiple).
4. ADIL是同步调用,而service的所有其它接口(bind, unbind, startService, stopService)都是异步接口,立即返回。
service有两种启动方式:Started和Bound。
Started方式:使用startService()启动。一旦启动,service will run indefinitely,unless stopSelf() or stopService() is called。
优点:使用简单,client端只需要简单的调用startService(),service端只需要在onStartCommand()进行处理。
缺点:
1. 必须调用stopSelf() or stopService()关闭service;
2. client端无法获取返回值(虽然可以通过向service传递PendingIntent for Broadcast实现);
3. 单向调用,只能由client端向service端发送intent。
Bound方式:使用bindService()启动。一旦启动,service will run indefinitely。 Multiple components can bind to the same service, but only when all of them unbind, the service is destroyed.
优点:
1. 可以通过AIDL或Messager实现IPC(实际都是通过Binder实现的);
2. 可以实现双向IPC,client可以通过onServiceConnected()取得service的Binder,之后也可以通过Binder将自己的Binde发送r给service,从而实现双向IPC。
混合方式:service can work both ways,it can be started and also allow binding.
onStartCommand()的返回值
Notice that onStartCommand() must return an integer, which describes how the system should re-start the service after it is killed:
START_STICKY:
service will be re-created(onCreate() is called), and system will guarantee onStartCommand() is called, but do not re-deliver the last intent. Instead, the system calls onStartCommand() with a null intent(?个人试验未成功?), unless there were pending intents to start the service.
START_STICKY_COMPATIBILITY
compatibility version of START_STICKY that does not guarantee that onStartCommand() will be called.
START_NOT_STICKY:
service will NOT be re-created unless there are pending intents to the service.
START_REDELIVER_INTENT
service will be re-created, and system will call onStartCommand() with the last intent that was delivered to the service. Any pending intents are delivered in turn.
不得不说的几点:
1. service可以声明为私有。you can declare the service as private in the manifest file by setting the android:exported to "false", and block access from other applications. Only components of the same application or applications with the same user ID can start the service or bind to it. You can also use the permission attribute to limit the external components that can interact with the service).
2. service可以运行在其它进程。Normally, all components of an application run in the default process created for the application. It has the same name as the application‘s package name. The <application> element's android:process attribute can set a different default name for all components. But component can override the default with its own process attribute, allowing you to spread your application across multiple processes.
If the name assigned begins with a colon (':'), a new process, private to the application, is created to run the service.(?个人试验未成功?)
If the name begins with a lowercase, the service will run in a global process of that name.
但是需要注意的是,一旦创建新的进程,不管是unbind还是stopService,即使service已经被destroy,系统也不会kill该进程!
2. AIDL(Android Interface Definition Language )文件中定义的接口会被编译生成对应的java文件,可以看到其中关键的
service启动流程
======== case 1. bind a service ============
TestAidl-Client: ------ =====> bindService()------
TestAidl-Client: ------ <===== bindService()------
TestAidl-Service: ------ service onCreate()------
TestAidl-Service: ------ service onBind()------ :onBind() is only called once, even we call bindService() multiple times, onBind() wont't be invoked.
TestAidl-Client: ------ onServiceConnected()------
======== case 2. unbind a service ============
TestAidl-Client: ------ =====> unbindService()------:if unbindService() is called with an un-bound service, exception will occur : java.lang.IllegalArgumentException: Service not registered
TestAidl-Client: ------ <===== unbindService()------:NOTE: onServiceDisconnected() won't be called due to unbindService(), 只有当servie异常退出时,系统才会调用onServiceDisconnected()
TestAidl-Service: ------ service onUnbind()------:onUnbind() is only called once when the last bound component unbind from the service, usually the service onDestroy() will be called afterwards..
【TestAidl-Service: ------ service onDestroy()------】
======== case 3. start a service ============
TestAidl-Client: ------ =====> startService()------
TestAidl-Client: ------ <===== startService()------
TestAidl-Service: ------ service onCreate()------
TestAidl-Client: ------ service onStartCommand()------:You should never call onStartCommand() directly.
======== case 4. stop a service ============
TestAidl-Client: ------ =====> stopService()------:stopService() can be called multiple times, nothing happened.
TestAidl-Client: ------ <===== stopService()------
【TestAidl-Service: ------ service onDestroy()------】
所以,在Service,只有onStart( )可被多次调用(通过多次startService调用),其他onCreate( ),onBind( ),onUnbind( ),onDestory( )在一个生命周期中只能被调用一次。
service的生命周期
1. A Started service will be destroyed once stopService() is called, no matter which component calls.
2. A bound service will be destroyed only when the last bound component calls the unbindService().
3. if a service is both started and bound, it will be destroyed when stopService() is called and no one is bound to the service at that time,除此之外,不管是调用unbindService()或者stopService()都是不起作用的。
分享到:
相关推荐
Android Upload Service Easily upload files in the background with automatic Android Notification Center progress indication. Download the latest demo app APK which uses the library and try it ...
android-support-v4-v7-v13-v14-v17(官方最新完整版),官方最新版的,压缩包内包含android-support-v4、android-support-v7-appcompat,android-support-v7-cardview,android-support-v7-gridlayout,android-support-...
ksoap2-android-assembly-2.6.5-jar-with-dependencies.jar 要是需要最新的,下载地址: http://code.google.com/p/ksoap2-android/
android ActivityManagerService 学习资料
Android---Service Timer之执行周期任务
android,启动service,并且service把值传给Activity显示
Android中滑屏初探 ---- scrollTo 以及 scrollBy方法使用说明 Android中滑屏初探 ---- scrollTo 以及 scrollBy方法使用说明 Android中滑屏初探 ---- scrollTo 以及 scrollBy方法使用说明
Android核心分析(07)----Service深入分析 Android核心分析(08)----Android 启动过程详解 Android核心分析(09)----Zygote Service Android核心分析(10)----Android GWES之基本原理篇 Android核心分析(11)--...
Android-Service系列之断点续传下载
down2mobile.net_ksoap2-android-assembly-2.4-jar-with-dependencies.jar 用于方方便调用web service
Android Service Demo。包括activity-》service,service-》demo
android各组件详解- Service
android-sdk\platforms\android-21
android-sdk\platforms\android-20
后台service实现文件下载,通过广播前台显示进度,文件下载方法就不写进去了,网上都有
关于Android Service的简单实例:属于start service类型,而不是bound service。
Android自定义锁屏实现----仿正点闹钟滑屏解锁
核心分析之一 ----- 分析方法论探讨之设计意图 核心分析之二 ----- 方法论探讨之概念空间篇 核心分析之三 ----- 手机之硬件形态 核心分析之四 ----- 手机之软件形态 核心分析之五 ----- 基本空间划分 核心分析之六 -...
android service使用的小demo 包括startService stopService bindService unbindService 两种开启、关闭service的小demo