此文原创,转载请标明网址:http://blog.csdn.net/buyi006/archive/2011/04/27/6368070.aspx
近期由于家人生病,一直陪床,故未来更新博客。
陪床之余也在想此博客该如何组织,原意是要以APIdemo为轴展开,后来发现有些小技巧也可穿插其间,所以以后博文就随遇而安,不同类型文章会放在不同的分类之中。
废话少说,言归正传。俗话说Android有四大金刚,Activity,Service,ContentProvider,Broadcast。
我们就来好好认识下Service,为什么先认识这个呢,因为这个概念不好理解。稍有不慎就会觉得这是个后台的线程。殊不知Google的官方定义已经写的很是明确。
A Service is an application component representing either an application's desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use.
这句话很明显的说明了service有两个作用
1:在无用户交互的情况下执行一个长时间的操作。(例如后台播放mp3等)
2:为其他程序使用提供一些功能支持。
其实这就对应了service的两种使用方式:第一种就是用Context.startService() 来启动。
而第二种则是用 Context.bindService().来使用。
A Service is not a separate process. The Service object itself does not imply it is running in its own process; unless otherwise specified, it runs in the same process as the application it is part of. A Service is not a thread. It is not a means itself to do work off of the main thread (to avoid Application Not Responding errors).
还有这两句话可以很好地解释大多数人对service的疑惑
一个service不是一个单独的进程。一个service实体并不运行在其自己的进程,除非service自己指定,它会运行在程序所在的相同进程。
一个service也不是一个线程,它并不运行在主线程之外。
看看ApiDemo有几个service的实例:
LocalService,LocalServiceActivities$Controller,LocalServiceActivities$Binding
MessengerService
RemoteService,RemoteService$Controller,RemoteService$Binding
ServiceStartArguments
ForegroundService
这里的例子基本把service的用法全说明了,预知后文如何,且听下回分解。
此文原创,转载请标明网址:http://blog.csdn.net/buyi006/archive/2011/04/27/6368070.aspx