Eighth Day of Android services

Source: Internet
Author: User

A service is not a separate thread. Service, like other components, by default, all code in the service is running in the main thread.
In Android, why use a service without a background thread? There are 3 main points:
1, the service can be placed in a separate process, so more secure;

2, the use of service can rely on the existing binder mechanism, do not need to deal with the complexity of thread synchronization at the application level;

3. The system can restart the service that died unexpectedly.

Description: A process is made up of several threads. (A thread is a part of a process that contains multiple threads running.) )

First, Service introduction

Service is one of the four components of the Android system (activity, Service, Broadcastreceiver, ContentProvider), which is similar to the level of activity, but cannot run only in the background, And can interact with other components. Service can be used in many applications, such as multimedia playback when the user started other activity this time the program to continue to play in the background, such as the detection of changes in the file on the SD card, or in the background to record your geographic information changes, etc., in short, the service is always hidden in the background.

Service launches are available in two ways:Context.startservice () and Context.bindservice ()

Second, service START process

Context.startservice () Start process:

Context.startservice (), OnCreate (), OnStart (), Service running, Context.stopservice (), ondes Troy (), Service stop

If the service is not running, Android calls OnCreate () first and then calls OnStart ();

If the service is already running, only OnStart () is called, so a service's OnStart method may be called repeatedly.

If the stopservice will be directly OnDestroy, if the caller is directly exiting without calling StopService, the service will be running 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

Context.bindservice () Start process:

Context.bindservice (), OnCreate (), Onbind (), Service running, Onunbind (), OnDestroy () Service stop

Onbind () returns a Ibind interface instance to the client, Ibind a method that allows the client to callback the service, such as getting an instance of the services, running state, or other operations. This time the caller (context, for example, activity) is bound together with the service, the context exits, Srevice will call Onunbind->ondestroy exit accordingly.

So the lifetime of the call Bindservice is: onCreate---Onbind (one time, not multiple bindings)--onunbind--and ondestory.

Only OnStart can be called multiple times (through multiple StartService calls) during each turn-off of the service, and the other oncreate,onbind,onunbind,ondestory can only be called once in a life cycle.

Third, service life cycle

Service life cycle is not as complex as activity, it only inherits OnCreate (), OnStart (), OnDestroy () three methods

When we first started the service, the OnCreate (), OnStart () methods were called, and the OnDestroy () method was executed when the service was stopped.

It is important to note that if the service is started, when we start the service again, the OnCreate () method is not executed, but the OnStart () method is executed directly.

It can stop itself through the service.stopself () method or the Service.stopselfresult () method, as long as the StopService () method is called to stop the service, regardless of how many times the startup service method is invoked.

More... http://blog.csdn.net/sunboy_2050/article/details/7364024

Eighth day of Android services

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.