The Difference between IntentService and Service in Android

Keywords: Android

Service in Android is used for background service. When an application is hung in the background, the concept of Service is introduced to ensure that some components of the application can still work. It is emphasized that Service is not a separate process, nor a separate thread. It depends on the main thread of the application. That is to say, it is not recommended in Server more often. Write time-consuming logic and operations in service, otherwise it will cause ANR.

So when we write time-consuming logic that has to be managed by Service, we need to introduce IntentService. IntentService inherits Service. Then it contains all the features of Service and the life cycle of Service. Unlike Service, when IntentService performs onCreate operation, it opens a thread to you. Do your time-consuming operation.

A method is provided in 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 the service to start, or when you call the servcie, the method is first called back.

At the same time, IntentService provides such a method:

protected abstract void onHandleIntent(Intent intent);  

This is an abstract method, which means that concrete implementations need to be extended to subclasses.

Declarations of subclasses:

public class ChargeService extends IntentService  

As mentioned above, IntentService inherits Service, so this subclass certainly inherits Service. When was the onHandleIntent() method called? Let's look specifically at the internal implementation of IntentService:

private final class ServiceHandler extends Handler {  
    public ServiceHandler(Looper looper) {  
        super(looper);  
    }  

    @Override  
    public void handleMessage(Message msg) {  
        onHandleIntent((Intent)msg.obj);  
        stopSelf(msg.arg1);  
    }  
}  

/** 
 * Creates an IntentService.  Invoked by your subclass's constructor. 
 * 
 * @param name Used to name 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)} will return 
 * {@link Service#START_REDELIVER_INTENT}, so if this process dies before 
 * {@link #onHandleIntent(Intent)} returns, the process will be restarted 
 * and the intent redelivered.  If multiple Intents have been sent, only 
 * the most recent one is guaranteed to be redelivered. 
 * 
 * <p>If enabled is false (the default), 
 * {@link #onStartCommand(Intent, int, int)} will 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 be nice to have an option to hold a partial wakelock  
    // during processing, and to have a static startService(Context, Intent)  
    // method 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) {  
    Message msg = mServiceHandler.obtainMessage();  
    msg.arg1 = startId;  
    msg.obj = intent;  
    mServiceHandler.sendMessage(msg);  
}  

Here we can clearly see that IntentService actually opens a thread Handler Thread when it executes the onCreate method, and obtains the looper of the current thread queue management, and puts the message into the message queue at onStart.

@Override  
       public void handleMessage(Message msg) {  
           onHandleIntent((Intent)msg.obj);  
           stopSelf(msg.arg1);  
       } 

When the message is accepted and callback by handler, the onHandler Intent method is executed, which is implemented by subclasses.

Conclusion:

IntentService implements a multi-threaded operation by means of Handler looper message. Meanwhile, time-consuming operations can be managed and executed by this thread without ANR.

Posted by milsaw on Fri, 19 Apr 2019 10:54:33 -0700