Android official service details

Service class I. overview whether a service represents the desire of an application to perform long-running operations, rather than the application components used by other applications that interact with users or provide functions. Each service class must have a corresponding < Service > announcement in its wrapped androidmanifest.xml file. The service can start context. Startservice() and context. Bindservice().

Note that services, like other application objects, run in the main thread of their host process. This means that if your service is going to do any CPU intensive (such as MP3 playback) or blocking (such as network) operations, it should spawn its own threads in doing the work. For more information on this, you can find processes and threads. The intentservice class can be used as a standard implementation service and has its own thread to do its schedule work there.

The topic covered here: what is service? Service Lifecycle permission process lifecycle local service sample remote messenger service sample

What is service? The most confusing service classes actually revolve around what is not: whether a service is a separate process. The service object itself does not mean that it is running in its own process; Unless otherwise specified, an application running in the same process is part of the. The service is not a thread. It doesn't mean doing your own work, shutting down the main thread (to avoid an application unresponsive error). Therefore, a service itself is actually very simple and provides two main features: a tool for the application to tell the system about something is the background of doing (even if the user does not interact directly with the application). This is equivalent to calling context. Startservice (), which requires the system to arrange work for the service to run until the service or others explicitly stop it. A tool so that an application can expose some of its features to other applications. This corresponds to calling context. Bindservice (), which allows a long-term connection to the addressable service to interact with it. When actually creating a service component, for the following reasons, all the systems actually do is instantiate the component and call its oncreate() and any other appropriate callback on the main thread. It is implemented by services with appropriate behaviors, such as creating worker threads in which it works. It should be noted that because the service itself is so simple, you can use it as a simple or complex interaction. You want to take it as a direct method call you make (as shown in the figure by the local Java object local service example), provide a full remotable interface and use Aidl.

Service life cycle

It has a service that can be run by the system for two reasons. If someone calls Context.startService (), then the system will retrieve the service (create and call its onCreate (), if necessary), then call its onStartCommand (intent, INT, INT) and the method provided by the client. The service will continue to run at this point until context. Stopservice() or stopself() is called. Please note that multiple calls to context. Startservice () are not nested (although they lead to multiple corresponding calls to onstartcommand ()), so no matter how many times the service started will be stopped, context. Stopservice () or stopself () will be called once; However, the service can use its own stopself (int) method to ensure that the service will not stop until the intention to start has been processed. There are two main modes of operation attached to the starting service. They can decide to run, depending on the value they return from onstartcommand(): start_ Sticky is used to explicitly start and stop as required services, while start_ NOT_ Stick or start_ REDELIVER_ Intent usage services should only stay in processing any commands sent to them. See the semantics of linked documents for more details. Users can also use context. Bindservice() to obtain a continuous connection to the service. This also causes the service if it is not running (call oncreate() instead of doing so), but does not call onstartcommand(). The client will receive the ibinder object, which returns the onbind (intention) method from its service, so that the client can then make a call and return to the service. The service will remain running as long as a connection is established (whether the client remains in the ibinder benchmark of the service). Usually, the returned ibinder is the Aidl written by the complex interface. A service can start at the same time and has no connection bound to it. In such a case, the system will maintain the service as long as it either starts running or has one or more connected to it with context.bind_ AUTO_ Create flag. Once it is established without these conditions, the ondestroy () method of the service is called, and the service is effectively terminated. All cleanup (thread stop, receiver logoff) should be completed on ondestroy returned from ().

jurisdiction

Global access to services can be enforced when it is declared in its list of < Services > tags. By doing so, other applications will need to declare the corresponding < use, license > element in their own manifest so that they can start, stop, or bind to the service. Because of gingerbread, when using context.startservice (intention), you can also set intent.flag_ GRANT_ READ_ URI_ Permission and / or intent.flag_ GRANT_ WRITE_ URI_ Intention of permission. This will give the service temporary access to the intention in the specific URI. Access will remain until the service is called stopself (int) for the start command or higher, or until the service has been completely stopped. This applies to all other applications that authorize access but do not require a license to protect the service, even when the service is not exported. In addition, a service can protect the permissions of individual IPC calls to it by calling checkcallingpermission (string) to execute the execution mode before the call. See security and permissions file permissions and general information security.

Process lifecycle

The Android system will try to keep the process of hosting services everywhere as long as the service has been started or bound to its customers. When the memory is insufficient and the existing process needs to be killed, the priority of the managed service process will be the higher of the following:

If the service executes the oncreate(), onstartcommand(), or ondestroy() method of the current code, the host process will be a foreground process to ensure that the code can be executed without being killed.

If the service has been started, its host process is considered less important than any process of the user on the currently visible screen, but more important than any invisible process. Because only a few processes are generally visible to users, this means that the service should not be killed unless in extreme low memory situations.

If there is a client bound to the service, the hosting process of the service will never be less important than the most important client. That is, if one of its clients is visible to the user, then the service itself is considered visible.

The started service can use the startforegroup (int, notification) API to put the service foreground status, in which the system thinks what it is and the user is actively aware of it, so there is no candidate to kill low memory. (it is still theoretically possible for the service to be killed under extreme memory pressure from the current application prospect, but in practice, this should not be a problem.) note that this means that when most services are running, it may be killed by the system if it is under heavy memory pressure. If this happens, the system will try to restart the service later. An important consequence of this is that if you implement onstartcommand() to schedule work, asynchronously or in another thread, you may need to use start_ FLAG_ Redelivery allows the system to re provide you with an intent so that it won't get lost if your service is killed and deal with it. In the same processed service (for example, during the activity of other application components running), the importance of the overall process beyond the importance of the service itself can be increased.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>