必须谨记的几点:
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那些不得不说的事儿》这本书深入浅出地探讨了Android平台开发的各种关键知识点和技术细节,对于想要深入了解Android开发的读者来说是一本不可多得的参考资料。以下是对书中的部分核心内容进行的详细解读: ...
在Android开发中, AccessibilityService 是一个非常重要的服务接口,它允许应用在用户与系统交互时获取到相关的事件,比如点击、触摸屏幕或者按下系统按键。这个功能被广泛应用于辅助功能应用,例如为视力障碍者...
这一联盟将会支持 Google 发布的 Android 手机操作系统或者应用软件,共同开发名为 Android 的 开 放源代码的移动系统。开放手机联盟包括手机制造商、手机芯片厂商和移动运营商几类。目前,联盟成员 数 量已经达到了...
总的来说,《名师讲坛:Android开发实战经典》是一本覆盖Android开发全方面的教材,通过理论结合实例的方式,帮助读者提升Android开发能力,从零基础到实战专家。不过,请注意,任何资源的使用都应尊重版权,仅限于...
那么我们当我们编写的耗时逻辑,不得不被service来管理的时候,就需要引入IntentService,IntentService是继承Service的,那么它包含了Service的全部特性,当然也包含service的生命周期,那么与service不同的是,...
在Android平台上,开发一款高级应用通常涉及到对系统API的深入理解和高效利用。在这个"Android高级应用源码-wifi信息扫描和rssi值检测"项目中,开发者关注的是如何获取并处理WiFi信息,特别是RSSI(Received Signal ...
在Android系统中,悬浮窗(Floating Window)是一种特殊类型的窗口,它可以在其他应用程序之上显示,让用户在不切换应用的情况下查看或操作悬浮窗内容。本压缩包"Android应用源码之悬浮窗 监视内容.zip"可能包含了一...
《Android Apprentice 第二版》是Razeware LLC出版的一本专为Android开发初学者编写的书籍,使用Kotlin语言来构建四个完整的Android应用程序。作者包括Darryl Bayliss、Tom Blankenship、Fuad Kamal和Namrata ...
在安卓(Android)平台上,开发推送消息服务是一个常见的任务,涉及到多方面的技术知识。"安卓Android源码——pushMessage.zip" 提供的源码可能是一个简单的推送消息实现,或者是一个完整的推送服务框架。这里我们将...
本项目“扬子地板仓库管理系统”是基于Android平台的一个实例,旨在为仓库管理提供一个便捷、高效的解决方案。值得注意的是,该项目的资源来源于网络和部分开源社区,其目的是为了供学习和参考,不得用于商业用途,...
在本项目"Android应用源码之【手机安全卫士02】连接服务器获取更新信息"中,我们将深入探讨Android应用程序如何实现与服务器的通信来获取更新数据,这在开发手机安全卫士类应用中是非常关键的功能。手机安全卫士通常...
我认识到自己的技术还有提升空间,特别是在XXX在线咨询系统的开发中,由于之前未接触过此类项目,我不得不从零开始学习。这个过程让我深刻体会到持续学习的重要性。我计划通过深入研究新技术,如Kotlin、MVVM架构...
对于开发者来说,利用Android SDK提供的API,可以创建应用程序来扩展或自定义这些功能。本项目"sms.zip"提供了一个简单的源代码示例,展示了如何在Android平台上编写一个发送短信的应用。 1. **Android SMS API**: ...
- **初学者**:对于刚刚接触Android开发的学习者来说,《Android™ A Programmer’s Guide》是一本极佳的入门教材,能够帮助他们快速上手并掌握核心概念。 - **中级开发者**:对于已有一定开发基础的人来说,这...
为了应对重打包攻击等安全威胁,Android系统和应用开发者将不得不采取更为复杂的防护措施,比如加强代码混淆、动态加载代码和对关键操作使用更强的认证机制。 3. 预防机制的创新 研究和开发新的防护机制,如使用...
在Android平台上,远程控制软件(Remote Access Tool,RAT)是一种技术复杂的程序,它允许一个设备(客户端)通过网络对另一个设备(服务器端)进行控制。"PCRat"是这类远程控制工具的一种,其源码公开,供爱好者们...
2. **基本组件**:接下来,读者将学习到Android应用的基本组成部件,如Activity、Service、Broadcast Receiver和Content Provider。 3. **UI设计**:本书详细讲解了如何使用XML布局文件来设计用户界面,并提供了大量...