Service精辟总结+理解(精)初级+中级
2013-05-22 12:17
381 查看
首先、我们来看下下面的流程图:
======
以上是两种生命周期,那我先不讲它们怎么实现。
首先写个类继承Service
public class Dujinyang extends Service {
int myStartMode; // indicates how to behave if the service is killed
IBinder myBinder; // interface for clients that bind
boolean myAllowRebind; // indicates whether onRebind should be used
@Override
public void onCreate() {
// The service is being created
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// The service is starting, due to a call to startService()
return mStartMode;
}
@Override
public IBinder onBind(Intent intent) {
// A client is binding to the service with bindService()
return mBinder;
}
@Override
public boolean onUnbind(Intent intent) {
// All clients have unbound with unbindService()
return mAllowRebind;
}
@Override
public void onRebind(Intent intent) {
// A client is binding to the service with bindService(),
// after onUnbind() has already been called
}
@Override
public void onDestroy() {
// The service is no longer used and is being destroyed
}
}
那这样我们就很好理解了。
---那我们可以看到的是,服务的生命周期很简单,里面包含了2个启动方式的生命周期,它只继承了onCreate(),onStart(),onDestroy()三个方法,当我们第一次启动Service时,先后调用了onCreate(),onStart()这两个方法,当停止Service时,则执行onDestroy()方法。
---这里需要注意的是,如果Service已经启动了,当我们再次启动Service时,不会在执行onCreate()方法,而是直接执行onStart()方法。
这2种启动模式,执行着上图的生命周期,只是启动的时候调用下面的2种不同方式便可。
1。startService()
2。bindService()
=========================================================================
本地服务 Local Service 用于应用程序内部。
--》调用Context.startService()启动,以调用Context.stopService()结束。它可以调用Service.stopSelf() 或 Service.stopSelfResult()来自己停止。值得一提的是: 不论你调用了多少次startService()方法,只需要调用一次stopService()就可以停止服务了。 (用于实现应用程序自己的一些耗时任务,比如查询升级信息,并不占用应用程序比如Activity所属线程,而是单开线程后台执行,这样用户体验比较好。)
远程服务 Remote Service 用于android系统内部的应用程序之间。
它通过自己定义并暴露出来的接口进行程序之间的操作。客户端建立一个到服务对象的连接,并通过这个连接来调用服务。这个连接以调用Context.bindService()方法建立,以调用 Context.unbindService()关闭,就像AIDL。多个客户端可以绑定至同一个服务。如果服务此时还没有加载,bindService()会先加载它。
(可被其他应用程序复用,调用已有的即可,就像第三方的应用)。
另:而启动service,根据onStartCommand的返回值不同,有两个附加的模式:
1. START_STICKY 用于显示启动和停止service。
2. START_NOT_STICKY或START_REDELIVER_INTENT用于有命令需要处理时才运行的模式。
============================================================================
上面讲到的2种启动模式:它们的不同
1. 使用startService()方法启用服务,调用者与服务之间没有关连,即使调用者退出了,服务仍然运行。
2。使用bindService()方法启用服务,调用者与服务绑定在了一起,调用者一旦退出,服务也就终止,大有“不求同时生,必须同时死”的特点。
---附:
官方文档告诉我们,Android系统会尽量保持拥有service的进程运行,只要在该service已经被启动(start)或者客户端连接(bindService)到它。当内存不足时,需要保持,拥有service的进程具有较高的优先级。
有4点:
1. 如果service正在调用onCreate,onStartCommand或者onDestory方法,那么用于当前service的进程则变为前台进程以避免被killed。
2. 如果当前service已经被启动(start),拥有它的进程则比那些用户可见的进程优先级低一些,但是比那些不可见的进程更重要,这就意味着service一般不会被killed.
3. 如果客户端已经连接到service (bindService),那么拥有Service的进程则拥有最高的优先级,可以认为service是可见的。
4. 如果service可以使用startForeground(int, Notification)方法来将service设置为前台状态,那么系统就认为是对用户可见的,并不会在内存不足时killed。
如果有其他的应用组件作为Service,Activity等运行在相同的进程中,那么将会增加该进程的重要性。
======
以上是两种生命周期,那我先不讲它们怎么实现。
首先写个类继承Service
public class Dujinyang extends Service {
int myStartMode; // indicates how to behave if the service is killed
IBinder myBinder; // interface for clients that bind
boolean myAllowRebind; // indicates whether onRebind should be used
@Override
public void onCreate() {
// The service is being created
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// The service is starting, due to a call to startService()
return mStartMode;
}
@Override
public IBinder onBind(Intent intent) {
// A client is binding to the service with bindService()
return mBinder;
}
@Override
public boolean onUnbind(Intent intent) {
// All clients have unbound with unbindService()
return mAllowRebind;
}
@Override
public void onRebind(Intent intent) {
// A client is binding to the service with bindService(),
// after onUnbind() has already been called
}
@Override
public void onDestroy() {
// The service is no longer used and is being destroyed
}
}
那这样我们就很好理解了。
---那我们可以看到的是,服务的生命周期很简单,里面包含了2个启动方式的生命周期,它只继承了onCreate(),onStart(),onDestroy()三个方法,当我们第一次启动Service时,先后调用了onCreate(),onStart()这两个方法,当停止Service时,则执行onDestroy()方法。
---这里需要注意的是,如果Service已经启动了,当我们再次启动Service时,不会在执行onCreate()方法,而是直接执行onStart()方法。
这2种启动模式,执行着上图的生命周期,只是启动的时候调用下面的2种不同方式便可。
1。startService()
2。bindService()
=========================================================================
本地服务 Local Service 用于应用程序内部。
--》调用Context.startService()启动,以调用Context.stopService()结束。它可以调用Service.stopSelf() 或 Service.stopSelfResult()来自己停止。值得一提的是: 不论你调用了多少次startService()方法,只需要调用一次stopService()就可以停止服务了。 (用于实现应用程序自己的一些耗时任务,比如查询升级信息,并不占用应用程序比如Activity所属线程,而是单开线程后台执行,这样用户体验比较好。)
远程服务 Remote Service 用于android系统内部的应用程序之间。
它通过自己定义并暴露出来的接口进行程序之间的操作。客户端建立一个到服务对象的连接,并通过这个连接来调用服务。这个连接以调用Context.bindService()方法建立,以调用 Context.unbindService()关闭,就像AIDL。多个客户端可以绑定至同一个服务。如果服务此时还没有加载,bindService()会先加载它。
(可被其他应用程序复用,调用已有的即可,就像第三方的应用)。
另:而启动service,根据onStartCommand的返回值不同,有两个附加的模式:
1. START_STICKY 用于显示启动和停止service。
2. START_NOT_STICKY或START_REDELIVER_INTENT用于有命令需要处理时才运行的模式。
============================================================================
上面讲到的2种启动模式:它们的不同
1. 使用startService()方法启用服务,调用者与服务之间没有关连,即使调用者退出了,服务仍然运行。
2。使用bindService()方法启用服务,调用者与服务绑定在了一起,调用者一旦退出,服务也就终止,大有“不求同时生,必须同时死”的特点。
---附:
官方文档告诉我们,Android系统会尽量保持拥有service的进程运行,只要在该service已经被启动(start)或者客户端连接(bindService)到它。当内存不足时,需要保持,拥有service的进程具有较高的优先级。
有4点:
1. 如果service正在调用onCreate,onStartCommand或者onDestory方法,那么用于当前service的进程则变为前台进程以避免被killed。
2. 如果当前service已经被启动(start),拥有它的进程则比那些用户可见的进程优先级低一些,但是比那些不可见的进程更重要,这就意味着service一般不会被killed.
3. 如果客户端已经连接到service (bindService),那么拥有Service的进程则拥有最高的优先级,可以认为service是可见的。
4. 如果service可以使用startForeground(int, Notification)方法来将service设置为前台状态,那么系统就认为是对用户可见的,并不会在内存不足时killed。
如果有其他的应用组件作为Service,Activity等运行在相同的进程中,那么将会增加该进程的重要性。
相关文章推荐
- Service精辟总结+理解(精)初级+中级
- Service精辟总结+理解(精)初级+中级
- Service精辟总结+理解(精)初级+中级
- Service精辟总结+理解(精)初级+中级
- Service精辟总结+理解(精)初级+中级
- ASP.NET初级>传智播客.net>第十一季asp.net中级 文字总结(未完)
- 总结java从初级到中级最后到高级所要掌握的知识点
- Atitit 泛型原理与理解attilax总结
- 对struts一点理解总结
- Linux用户空间与内核空间 理解和总结
- Binder,Service,进程间通信学习总结
- 我对Java Serializable(序列化)的理解和总结
- SWIG学习总结(翻译,自己理解为辅)
- Java常量池理解和经典总结
- java初级酒店客房管理图形化界面总结
- 总结下java中abstract,interface,final,static,加深理解
- 关于js设计模式的一些总结和理解
- MySQL安装总结:Start service没有响应(Win7 64位)
- 《深入理解JVM》--JVM类加载机制总结
- 深入理解Java虚拟机【总结】