Service Related documents reproduced: http://www.cnblogs.com/lwbqqyumidi/p/4181185.html
http://blog.csdn.net/richnaly/article/details/7829501
Http://www.cnblogs.com/yejiurui/archive/2013/11/18/3429451.html
http://blog.csdn.net/yuzhiboyi/article/details/7558176
(1) Service
first, let us confirm what is the service?
Service is the services in the Android system, it has a few features: it can not interact directly with the user, it must be explicitly launched by the user or other programs, it has a higher priority, it is lower than the application in the foreground, but higher priority than other applications in the background, This determines that when the system destroys certain underutilized resources because of lack of memory, it is less likely to be destroyed.
second, so, when, we need to use the service?
We know that service is an application running in the background, and for users it loses the focus of attention. After we opened the music, we wanted to look at the pictures, we didn't want the music to stop, we used the service here, for example, after we opened a download link, we certainly didn't want to stare and wait for him to finish downloading and then do something else, right? At this time if we want to download the phone in the background, while you can let me go to see the news, it is necessary to use the service.
Third, service classification:
generally we think that service is divided into two categories,Local Serviceand theRemote service.
Local Service as the name implies, that is, and the current application in the same process of service, each other has a common memory area, so for some data sharing is particularly convenient and simple;
Remote service:mainly involved in service access between different processes. Because of the security of Android system, we cannot share data in a common way between different processes. Here Android provides us with aa aidl tool. (Android Interface Description Language) Android Interface Description Language. We will introduce them in detail in the rear.
Iv. service life cycle:
compared to the activity, the service life cycle is simply no longer simple, onlyonCreate ()->onstart ()->ondestroy ( )three methods.
activity and service-related methods:
StartService (Intent Intent): Start a service
StopService (Intent Intent): Stop a service
if we want to usesome of the data in the service, or access some of the methods, then we'll go through the following method:
Public Boolean Bindservice (Intent Intent, serviceconnection conn, int flags);
Public void Unbindservice (Serviceconnection conn);
Intent is the Intent that jumps to the service, such as Intent Intent = new Intent (); Intent.setclass (this,myservice.class);
Conn is a class that represents a connection state to a service, and when we connect to a service that succeeds or fails, it actively triggers its internalonserviceconnectedoronservicedisconnectedmethod. If we want to access the data in the service, it can be implemented in the onserviceconnected () method,
(2) Service life cycle in Android
Start service with Context.startservice ()
Its life cycle is Context.startservice ()->oncreate ()->onstart ()->service running-->context.stopservice () | ->ondestroy ()->service stop
If the service is not running, then Android calls OnCreate () and then calls OnStart ();
If the service is already running, only OnStart () is called, so a service's OnStart method may be called repeatedly.
StopService Direct OnDestroy,
If the caller exits directly without calling StopService, the service will run in the background.
The service can be shut down by StopService after the caller has started up again.
So the life cycle of calling StartService is: onCreate---OnStart (can be called multiple times)--OnDestroy
for Bindservice () Start service experience:
Context.bindservice ()->oncreate ()->onbind ()->service running-->onunbind (), OnDestroy () Service stop
Onbind will return a Ibind interface instance to the client, Ibind allow the client to callback the service's methods, such as getting service running status or other operations.
This time the caller (context, for example, activity) is bound to the service and the context exits,
Srevice will call Onunbind->ondestroy to exit accordingly.
So the lifetime of the call Bindservice is: onCreate---Onbind (one time, not multiple bindings)--onunbind--and ondestory.
Once the activity is destroyed it ends, and if you press home to put it backstage, then he doesn't quit.
Add:
Only OnStart can be called multiple times (through multiple StartService calls) during each turn-off of the service.
Other oncreate,onbind,onunbind,ondestory can only be called once in a life cycle.
______________________________________________________________________________________________________
Demo question:
(1)
Resolution: The service is bundled via broadcast!
(2) Life cycle://life cycle OnCreate ()-onbind ()-onstartcommand ()-onunbind ()-ondestroy ()
(3)
Solve the problem: MediaPlayer written in play will not error
(4) Exit procedure: When the last activity exits, the program will not exit, when it is called
System.exit (0);
Demo Link: http://download.csdn.net/detail/zqs62761130/8618017
Play m3u8 stream with Vitamio,mediaplayer background service