First, let's confirm what is a service?
A service is a service in the Android system. It has the following characteristics: it cannot directly interact with users, it must be explicitly started by users or other programs, and its priority is relatively high, it has a lower priority than the foreground applications, but higher priority than other background applications. This determines that when the system destroys some unused resources due to the lack of memory, it has a low probability of being destroyed.
So when should we use the service?
We know that the service is an application running in the background, and the focus of attention is lost to users. After playing the music, we want to see the picture. At this time, we don't want to stop the music, so we will use the service. For example, after opening a download link, we certainly don't want to stare at it until he downloads it and does other things, right? At this time, if we want to download data from the background on our mobile phone, we need to use the service to let me see the news.
Service Category:
Generally, we think there are two types of services: local service and remote service.
The local service, as its name implies, is a service in the same process as the current application and has a common memory area with each other. Therefore, it is especially convenient and simple to share some data;
Remote service: mainly involves service access between different processes. Because of the security of the Android system, we cannot share data in common ways between different processes. Android provides an aidl tool. (Android Interface Description Language) Android Interface Description Language. We will introduce it in detail later.
Service lifecycle:
Compared with activity, the life cycle of a service is no longer simple. There are only three methods: oncreate ()-> onstart ()-> ondestroy.
Methods related to services in activity:
Startservice (intent): starts a service
Stopservice (intent): Stop a service
If we want to use some data in the service or access some of the methods, we need to use the following method:
Public Boolean bindservice (intent, serviceconnection Conn, int flags );
Public void unbindservice (serviceconnection conn );
Intent is the intent to jump to the service, such as intent = new intent (); intent. setclass (this, myservice. Class );
Conn is a class that represents the connection status with the Service. When we connect to the Service successfully or fails, it will actively trigger the onserviceconnected or onservicedisconnected method. If you want to access data in the service, you can implement it in the onserviceconnected () method, for example
/** <Br/> * triggered when the link is linked to the service. <Br/> * Name of the component linked to the service <br/> * ibinder returned by the service when onbund is called in the service, it is mainly used for information exchange <br/> */<br/> @ override <br/> Public void onserviceconnected (componentname, ibinder Service) {<br/> log. I ("notification", "link successful! "); <Br/> mybinder binder = (mybinder) service; <br/> myservice = binder. getmyservice (); <br/> int COUNT = myservice. getcount (); <br/> log. I ("notification", "Count =" + count); </P> <p>}
-------------------------------------------------------------------
Steps for using the service:
Step 1: We need to inherit the service class and implement our own service.
If you want to access some values in the service, we usually provide an internal class that inherits the binder and return it to the service request through the onbund () method. In fact, it cleverly utilizes the features that internal classes can access external class attributes.
Step 2: Register in androidmanifest. XML, for example:
<! --
Service configuration starts
-->
<Service android: Name = "myservice"> </service>
<! --
End of service configuration
-->
Step 3: Start, bind, unbind, or stop the service in the activity.
(In many books, services cannot interact with users. In fact, this is incorrect. We can interact with services through activity! I think the exact statement is that the Service and the user cannot directly interact ).
------------------------------------------------------
Below is an example of calling service to listen to music:
Activity Code:
Package cn.com. chenzheng_java; </P> <p> Import cn.com. chenzheng_java.myservice.mybinder; <br/> Import android. app. activity; <br/> Import android. content. componentname; <br/> Import android. content. intent; <br/> Import android. content. serviceconnection; <br/> Import android. OS. bundle; <br/> Import android. OS. ibinder; <br/> Import android. util. log; <br/> Import android. view. view; <br/> Import android. view. view. O Nclicklistener; <br/> Import android. widget. button; <br/>/** <br/> * @ description simple application of the Service <br/> * @ author chenzheng_java <br/> * @ since 2011/03/18 <br/> * <br/> */<br/> public class serviceactivity extends activity implements onclicklistener {<br/> private button button_start; <br/> private button button_bind; <br/> private button button_destroy; <br/> private button button_unbind; </P> <p> @ ov Erride <br/> protected void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. service); </P> <p> button_start = (button) findviewbyid (R. id. button1_service); <br/> button_bind = (button) findviewbyid (R. id. button2_service); <br/> button_destroy = (button) findviewbyid (R. id. button3_service); <br/> button_unbind = (button) findviewbyid (R. id. button4_se Rvice); </P> <p> button_start.setonclicklistener (this); <br/> button_bind.setonclicklistener (this); <br/> button_destroy.setonclicklistener (this ); <br/> button_unbind.setonclicklistener (this ); </P> <p >}</P> <p> private class myserviceconnection implements serviceconnection {</P> <p>/** <br/> * triggered when the link is linked to the service. <Br/> * Name of the component linked to the service <br/> * ibinder returned by the service when onbund is called in the service, it is mainly used for information exchange <br/> */<br/> @ override <br/> Public void onserviceconnected (componentname, ibinder Service) {<br/> log. I ("notification", "link successful! "); <Br/> mybinder binder = (mybinder) service; <br/> myservice = binder. getmyservice (); <br/> int COUNT = myservice. getcount (); <br/> log. I ("notification", "Count =" + count ); </P> <p >}</P> <p> @ override <br/> Public void onservicedisconnected (componentname name) {<br/> log. I ("notification", "link failed! "); <Br/>}</P> <p> private myserviceconnection serviceconnection = new myserviceconnection (); <br/> @ override <br/> Public void onclick (view v) {<br/> If (V = button_start) {<br/> intent = new intent (); <br/> intent. setclass (getapplicationcontext (), myservice. class); <br/> startservice (intent); <br/>}</P> <p> If (V = button_bind) {<br/> intent = new intent (); <br/> intent. setclass (getapplicationcontext (), myservice. class); <br/> bindservice (intent, serviceconnection, bind_auto_create); <br/>}< br/> If (V = button_destroy) {<br/> intent = new intent (); <br/> intent. setclass (getapplicationcontext (), myservice. class); <br/> stopservice (intent); <br/>}< br/> If (V = button_unbind) {<br/> unbindservice (serviceconnection ); <br/>}</P> <p >}< br/>
Inherit the service class:
Package cn.com. chenzheng_java; </P> <p> Import android. app. service; <br/> Import android. content. intent; <br/> Import android. media. mediaplayer; <br/> Import android. OS. binder; <br/> Import android. OS. ibinder; <br/> Import android. util. log; </P> <p>/** <br/> * @ description implements your own service <br/> * @ author chenzheng_java <br/> * @ since 2011/03/18 <br/> */<br/> public class myservice extends Service {<br/> mediapl Ayer mediaplayer; </P> <p>/** <br/> * when a user calls the bindservice method, this method is triggered to return an ibinder object. We can use this object, access certain data in the service <br/> */<br/> @ override <br/> Public ibinder onbind (intent) {<br/> log. I ("notification", "service bound successfully! "); <Br/> return New mybinder (); <br/>}</P> <p> @ override <br/> Public void oncreate () {<br/> log. I ("notification", "service created successfully! "); </P> <p> mediaplayer = mediaplayer. create (this, R. raw. aiweier); <br/> mediaplayer. setlooping (false); <br/> super. oncreate (); <br/>}</P> <p> @ override <br/> Public void ondestroy () {<br/> mediaplayer. stop (); <br/> log. I ("notice", "Service destroyed successfully! "); <Br/> super. ondestroy (); <br/>}</P> <p> @ override <br/> Public void onrebind (intent) {<br/> log. I ("notification", "service rebinding successful! "); <Br/> super. onrebind (intent); <br/>}</P> <p> @ override <br/> Public void onstart (intent, int startid) {<br/> mediaplayer. start (); <br/> log. I ("notification", "service start successful! "); <Br/> super. onstart (intent, startid); <br/>}</P> <p> @ override <br/> Public Boolean onunbind (intent) {<br/> mediaplayer. stop (); <br/> log. I ("notice", "service unbound! "); <Br/> return Super. onunbind (intent); <br/>}</P> <p> private int COUNT = 100; </P> <p> Public int getcount () {<br/> return count; <br/>}</P> <p> Public void setcount (INT count) {<br/> This. count = count; <br/>}</P> <p> public class mybinder extends binder {<br/>/** <br/> * @ return returns individual service objects <br /> */<br/> myservice getmyservice () {<br/> return myservice. this; <br/>}</P> <p >}< br/>
Service. XML code:
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout <br/> xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: layout_width = "match_parent" <br/> Android: layout_height = "match_parent"> <br/> <button Android: text = "" Android: Id = "@ + ID/button#service" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"> </button> <br/> <button Android: text = "binding" Android: Id = "@ + ID/button2_service" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"> </button> <br/> <button Android: text = "Destroy" Android: id = "@ + ID/button3_service" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"> </button> <br/> <button Android: TEXT = "Unbind" Android: Id = "@ + ID/button4_service" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"> </button> <br/> </linearlayout> <br/>
Androidmanifest. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <manifest xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> package = "cn.com. chenzheng_java "<br/> Android: versioncode =" 1 "<br/> Android: versionname =" 1.0 "> <br/> <uses-SDK Android: minsdkversion = "8"/> </P> <p> <application Android: icon = "@ drawable/icon" Android: label = "@ string/app_name"> <br/> <activity Android: Name = "serviceactivity" <br/> Android: label = "@ string/app_name"> <br/> <in Tent-filter> <br/> <action Android: Name = "android. intent. action. main "/> <br/> <category Android: Name =" android. intent. category. launcher "/> <br/> </intent-filter> <br/> </activity> <br/> <! -- <Br/> service configuration starts <br/> --> <br/> <service android: Name = "myservice"> </service> <br/> <! -- <Br/> service configuration is complete <br/> --> <br/> </Application> <br/> </manifest>
Finally:
---------------------------------------------------------------------