Step by Step _android Development Course [5]_service Study

Source: Internet
Author: User

Focus on technology, enjoy life! --Yang Zhou qq:804212028
Original link: http://blog.csdn.net/y18334702058/article/details/44624305

    • Topics: Service Services related content
      -android Development Service is very important, but also has a certain difficulty drops, not afraid, we step by step to see:

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, how to develop a service component?

First step: Inherit service class public class SMSService extends service {}
Step Two: Configure the service in the node in the Androidmanifest.xml file:
Step three: Start the service

Start Service method One: Context.startservice (): There is no connection between the caller and the service, even if the caller exits, the service is still running.
Steps:

context.startService() 启动流程: context.startService() -> onCreate()
-> onStart() -> Service running -> context.stopService() -> onDestroy() -> 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 lifetime of the call StartService is:oncreate–> OnStart (can be called multiple times) –> OnDestroy

Start Service method Two: Context.bindservice (): The caller and the service are bound together, once the caller exits, the service terminates, and there is a "no need to live at the same time Must Die" feature

Context.bindservice () Start-up 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 (only once, not multiple bindings) –> onunbind–>
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

The service life cycle is simple, it inherits only the 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, no matter how many times the service method is invoked, the service can be stopped once the StopService () method is called.

OnCreate ():
This method is called when the service is created, and the method is only called once, and the service is created only once, regardless of how many times the StartService () or Bindservice () method is called.
OnDestroy ():
This method is called when the service is terminated. Life cycle methods related to starting services with the Context.startservice () method
OnStart ():
This method is only invoked when the service is started with the Context.startservice () method. This method is called when the service starts running. The StartService () method is called multiple times although the service is not created more than once, but the OnStart () method is called multiple times.
Onbind ():
This method is only invoked when the service is started with the Context.bindservice () method. This method is called when the caller and the service are bound, and the Context.bindservice () method is called multiple times when the caller is tied to the service and does not cause the method to be called more than once.
Onunbind ():
This method is only invoked when the service is started with the Context.bindservice () method. This method is called when the caller and the service are unbound

Life cycle Graphs:

Four: What is the difference between service and thread?

A: Servie is a component of the system, which is managed by the system process (Service Manager), and the communication between them is similar to client and server, is a lightweight IPC communication, the carrier of this communication is binder, It is an IPC that exchanges information at the Linux layer. Thread is hosted by this application.

1). Thread:thread is the smallest unit of program execution, and it is the basic unit for allocating CPUs. You can use the Thread to perform some asynchronous operations.
2). Service:service is a mechanism for Android, and when it runs, if it is a local service, then the corresponding service is running on the main thread of the master process. such as: Oncreate,onstart These functions are run on the main thread of the master process when they are called by the system. In the case of remote service, the corresponding service is running on the main thread of the standalone process. So please do not interpret the Service as a thread, it has no relationship with the thread of half a dime!

In that case, why should we use service?

Actually this is related to the Android system mechanism, we first take the thread to say. The thread is run independently of the activity, which means that when an activity
After finishing, if you do not actively stop the thread or thread in the Run method is not completed, thread will also be executed. So there's a problem here: when the activity is finished, you no longer have a reference to that thread. On the other hand, you have no way in different
The same Thread is controlled in the Activity.

For example, if your thread needs to be connected to a server for some sort of synchronization over time, the thread needs to be in the Activity
It runs when there is no start. This time when you start an activity there is no way to control the activity inside of the created
Thread. So you need to create and start a service, create, run, and control it within the service.
Thread, which solves the problem (because any Activity can control the same service, and the system will only create a corresponding service
Instance).

So you can think of the service as a message, and you can call Context.startservice, Context.stopservice, Context.bindservice,context.unbindservice, to control it, you can also
The Service registers the Broadcastreceiver, in other places by sending broadcast to control it, of course these are Thread
Can't do that.

Focus on technology, enjoy life! --Yang Zhou qq:804212028
Original link: http://blog.csdn.net/y18334702058/article/details/44624305

Step-by-step _android Development Course [5]_service Learning

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.