Deep analysis of the difference between service and Intentservice in the Android system _android

Source: Internet
Author: User
Tags message queue

The service in Android is for the backend service, and when the application is in the background and asks to ensure that some of the components are still working and introduces the concept of service, it should be emphasized that the service is not an independent process, nor a separate thread, It is dependent on the main thread of the application, which means that more times it is not recommended to write time-consuming logic and operations in the service, otherwise it can cause ANR.

So when we write the time-consuming logic that has to be managed by the service, we need to introduce Intentservice,intentservice to inherit the service, and it contains all the features of the service, Of course it also includes the service lifecycle, so unlike service, Intentservice is running a thread inside the OnCreate operation to perform your time-consuming operations.

The service itself has two problems:

(1) service does not specifically initiate a separate process, the service is located in the same process as the application in which it resides.

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

Intentservice a good remedy 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 have been processed, Intentservice will automatically stop.

(4) provides a default implementation for the service's Onbind () method, and returns NULL.

(5) provides a default implementation for the service's Onstartcommand () method, which adds the request intent to the queue.

So the use of Intentservice is: Inherit Intentservice, rewrite Onhandleintent () method.

Tips
(1) Intentservice must also be declared in manifest.
(2) The constructor method of the implementation class must implement the default construction method.

Here I need to explain the following methods, perhaps we have been very clear, but in order to make a reference, I would like to mention a mouth.

A method is provided in the service:

public int Onstartcommand (Intent Intent, int flags, int startid) { 
   OnStart (Intent, startid); 
   Return mstartcompatibility? Start_sticky_compatibility:start_sticky; 
 } 

The specific meaning of this method is that when you need this service to start, or call this servcie, then this method is first to be recalled.

At the same time Intentservice provides such a method:

protected abstract void Onhandleintent (Intent Intent); 

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

Declaration of subclasses:

public class Chargeservice extends Intentservice  

As mentioned above Intentservice is inherited service, then this subclass is also sure to inherit service, then Onhandleintent () method is called when it? Let us look specifically at the internal implementation of Intentservice:

Private Final class Servicehandler extends Handler {public Servicehandler (Looper looper) {super (Looper); 
    @Override public void Handlemessage (msg) {onhandleintent (Intent) msg.obj); 
  Stopself (MSG.ARG1); }/** * Creates an intentservice. 
 Invoked by your subclass ' s constructor. 
 * * @param name Used to name of the worker thread, important only for debugging. 
  */Public Intentservice (String name) {super (); 
Mname = name; }/** * Sets Intent redelivery preferences. 
 Usually called from the constructor * with your preferred semantics. * <p>if Enabled is true, * {@link #onStartCommand (Intent, int, int)} would return * {@link Service#start_rede 
 Liver_intent}, so if this process dies before * {@link #onHandleIntent (INTENT)} returns, the process would be restarted * and the intent redelivered. 
 If multiple Intents have been sent, only * The most recent one are guaranteed to be redelivered. * * <p>if ENABled is False (the default), * {@link #onStartCommand (Intent, int, int)} would return * {@link service#start_not_sticky 
 }, and if the process dies, the Intent * dies along with it. 
* * public void Setintentredelivery (Boolean enabled) {mredelivery = enabled; @Override public void OnCreate () {//Todo:it would is nice to have the option to hold a partial wakelock/d  Uring processing, and to have a static startservice (context, Intent)//How that would launch the service & hand 
 
  Off a wakelock. 
  Super.oncreate (); 
  Handlerthread thread = new Handlerthread ("intentservice[" + Mname + "]"); 
 
  Thread.Start (); 
  Mservicelooper = Thread.getlooper (); 
Mservicehandler = new Servicehandler (mservicelooper); 
  @Override public void OnStart (Intent Intent, int startid) {msg = Mservicehandler.obtainmessage (); 
  MSG.ARG1 = Startid; 
  Msg.obj = Intent; 
Mservicehandler.sendmessage (msg); 

 }

Here we can clearly see in fact intentservice in the implementation of the OnCreate method, actually opened a thread handlerthread, and obtained the current thread queue management Looper, and in OnStart, The message is placed in the message queue,

@Override public 
    void handlemessage (Message msg) { 
      onhandleintent (Intent) msg.obj); 
      Stopself (MSG.ARG1); 
    } 

When the message is handler accepted and recalled, the Onhandlerintent method is executed, and the implementation of the method is subclass.

Conclusion:

Intentservice implements a multithreaded operation by handler Looper message, while time-consuming operations can be managed and executed by this thread without generating ANR.

Related Article

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.