Deeply analyze the differences between service and intentservice in Android system

Service in Android is used for background service. When the application is hung in the background, the concept of service is introduced to ensure that some components of the application can still work. In this regard, it should be emphasized that service is not an independent process or an independent thread. It depends on the main thread of the application, that is, More often, it is not recommended to write time-consuming logic and operations in the service, otherwise anr will be caused.

When the time-consuming logic we write has to be managed by the service, we need to introduce intentservice. Intentservice inherits the service, so it includes all the features of the service and, of course, the service life cycle. Unlike the service, intentservice has an internal thread when performing oncreate operation, Go and perform your time-consuming operations.

The service itself has two problems:

(1) The service does not start a separate process. The service is in the same process as the application it is in.

(2) Service is not a new thread, and it should not handle time-consuming operations in the service.

Intentservice makes up for this:

(1) Intentservice creates a separate worker thread to handle all intent requests.

(2) Intentservice creates a separate worker thread to handle the code implemented by the onhandleintent () method.

(3) When all requests are processed, intentservice will stop automatically.

(4) A default implementation is provided for the onbind () method of the service, which returns null.

(5) A default implementation is provided for the onstartcommand () method of the service, which adds the request intent to the queue.

Therefore, the use of intentservice is to inherit intentservice and override onhandleintent() method.

Tips: (1) intentservice must also be declared in manifest. (2) The constructor that implements the class must implement the default constructor.

Here I need to explain the following methods, which may be very clear to everyone, but in order to attract jade, I still want to mention it.

Service provides a method:

The specific meaning of this method is that when you need the service to start or call the servcie, this method will be called back first.

Meanwhile, intentservice provides such a method:

This is an abstract method, that is, the specific implementation needs to be extended to subclasses.

Declaration of subclasses:

As mentioned above, intentservice inherits service, so this subclass must also inherit service. When was the onhandleintent () method called? Let's look at the internal implementation of intentservice:

Here we can clearly see that when intentservice executes the oncreate method, it actually opens a thread handler thread, obtains the looper managed by the current thread queue, and puts the message into the message queue when OnStart,

When the message is accepted and called back by the handler, the onhandlerintent method is executed, and the implementation of this method is done by subclasses.

Conclusion:

Intentservice implements a multi-threaded operation through handler looper message. At the same time, time-consuming operations can also be managed and executed by this thread without ANR.

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
分享
二维码
< <上一篇
下一篇>>