Original reproduced from: http://www.cnblogs.com/bravestarrhu/archive/2012/05/02/2479461.html
Service Service:
A service is a long life cycle, without a user-interface program that can be used to develop programs such as monitoring classes.
A good example is a media player that is playing songs from a playlist. In a media player application, there should be multiple activity, allowing the user to select songs and play songs. However, there is no activity associated with the music replay because the user will of course think that the music should still be playing when navigating to other screens. In this example, the Media Player activity uses context.startservice () to start a service so that it can keep the music playing in the background. At the same time, the system will keep the service running until the end of the service run. In addition, we can also connect to a service by using the Context.bindservice () method (if the service is not running it will start it). When connected to a service, we can also communicate with it by the interface provided by the service. Take the Media Player example, we can also pause, replay and other operations.
Service usage steps are as follows
1> inheriting the service class
Configure the service in the <application> node in the 2>androidmanifast.xml configuration manifest file
<service name= ". SMSService "/>
Service does not run on its own and needs to be started by Contex.startservice () or Contex.bindservice ()
The service started by the StartService () method has no relationship with the caller, even if the caller shuts down and the service is still running to stop the service to call Context.stopservice (), at which point the system calls Ondestory (), when started with this method, The first time the service starts the system calls the service's onCreate ()-->onstart (), and if the service has started again the call will only trigger the OnStart () method
Services that are started with bindservice () are bound to the caller as long as the caller shuts down the service, and when started using this method, the first time the service starts the system calls the service's OnCreate ()-->onbind (), and if the service has started again, the call will no longer trigger these 2 methods , when the caller exits, the system invokes the service's Onunbind ()-->ondestory (), and the system calls Onunbind ()-->ondestory () in order to actively unbind the binding contex.unbindservice ();
A summary of the four basic components:
1> Registration of 4 major components
4 basic components need to be registered to use, each activity, service, Content Provider content providers need to be configured in the Androidmanifest file androidmanifest activity, services, and content providers that are not declared in the file are not visible to the system and are therefore not available. The registration of the Broadcastreceive broadcast receiver, which is configured in the Androidmanifest file, is dynamically created by the code and registered to the system in the form of a call to Context.registerreceiver (). It is important to note that the broadcast receivers configured in the Androidmanifest file will be active as soon as the system is started, as long as the received broadcast is triggered (even if the program is not running)
The registration format in the Androidmanifest file is as follows:
The name attribute of the <activity> element specifies the subclass of activity that implements the activity. The icon and label properties point to the resource file that contains the icons and labels for this activity that are presented to the user.
<service> Elements for declaring services
The <receiver> element is used to declare a broadcast sink
<provider> elements for declaring content providers
2> 4 activation of large components
• Capacity provider activation: When a request is received from the Contentresolver, the content provider is activated. The other three components ──activity, service, and broadcast receivers are activated by an asynchronous message called intent
Activation of activity is done by passing an intent object to context.startactivity () or Activity.startactivityforresult () to load (or designate a new job to) an activity. The corresponding activity can view the intent that activates it by calling the Getintent () method. If it expects the activity it initiates to return a result, it will replace startactivity () with the call to Startactivityforresult (). For example, if it launches another Activity to allow the user to pick a photo, it may want to know which photo is selected. The result is encapsulated in a intent object and passed to the Onactivityresult () method of the activity that made the call.
• The activation of the service can be done by passing a intent object to Context.startservice () or Context.bindservice () the former Android invoke service's OnStart () method and passing the intent object to it, The latter Android invokes the service's Onbind () method to pass the intent object to it
• Send broadcasts can be transmitted by passing a intent object to Context.sendbroadcast (),
Context.sendorderedbroadcast () or context.sendstickybroadcast () Android calls all broadcast receivers that are interested in this broadcast OnReceive () method, which will intent Passed to them
3> Closure of four components
The content provider is only activated when responding to contentresolver requests. A broadcast receiver is activated only when it responds to broadcast information. Therefore, it is not necessary to explicitly close these components.
Activity close: You can close an activity by calling its finish () method
Service shutdown: For services started by the StartService () method, to invoke the Context.stopservice () method to close the service, the service started with the Bindservice () method to invoke Contex.unbindservice () method to close the service
Android Service is the four-component summary