IntentService: IntentService: IntentService: IntentService: IntentService: IntentService: IntentService: IntentService: IntentService: IntentService

1 the use of

First, define a TestIntentService inherited from IntentService, and rewrite onHandleIntent (Intent Intent) method, and then need to implement a no no-parameter constructor:

TestIntentService Public class TestIntentService extends IntentService {private static Final String TAG = "TestIntentService"; Public TestIntentService() {super("TestIntentService"); } @Override public void onCreate() { super.onCreate(); Log.d(TAG, "onCreate: "+ thread.currentThread ().getName())); } @override protected void onHandleIntent(@override Intent Intent) { Log.d(TAG, "onHandleIntent: "+ thread.currentThread ().getName())); if (intent == null) { return; Intent.getaction (); intent.getAction(); If ("download".equals(action)) {// Play String url = intent.getStringExtra("url"); Log.d(TAG, "onHandleIntent, url=[" + url + "]"); } else if ("play".equals(action)) {intEnt.getStringExtra ("id"); Override public void onDestroy() {super.ondestroy (); // Override public void onDestroy() {super.ondestroy (); Log.d(TAG, "onDestroy: "+ Thread.currentThread().getName()); Intent = new Intent(this, testIntentservice.class); intent.setAction("download"); Intent. PutExtra (" url ", "http://123.45.67.1/xxx/xxx"); startService(intent); OnHandleIntent = intEntService onHandleIntent = intEntService onHandleIntent = Intent IntentCopy the code

When we run through the above code, we will find the following log:

D/TestIntentService: onCreate: main D/TestIntentService: onHandleIntent: IntentService[TestIntentService] D/TestIntentService: OnHandleIntent, url = [http://123.45.67.1/xxx/xxx] D/TestIntentService: onDestroy: mainCopy the code

StartService () -> onCreate (main thread) -> onHandleIntent(worker thread) -> Run your own business code -> onDestroy()

We look at the log and see that IntentService is a smart move, so let’s take a look at the source code for why it’s so good

2 Source code Analysis

public abstract class IntentService extends Service { private volatile Looper mServiceLooper; // Internal Looper private Volatile ServiceHandler mServiceHandler; Private String mName; // Name, constructor passed private Boolean mRedelivery; Private Final Class ServiceHandler extends Handler {public ServiceHandler(Looper Looper) {Private Final Class ServiceHandler extends Handler {public ServiceHandler(Looper Looper) { super(looper); Override public void handleMessage(MSG) {Override public void handleIntent (MSG) {Override public void handleMessage(MSG); Intent Intent Intent Intent Intent Intent Intent Intent Intent Intent OnDestroy () stopSelf(msg.arg1); }} public IntentService(String name) {super(); mName = name; } // If set to true, if the process dies during onHandleIntent(), the process restarts and another Intent is sent. Public void setIntentRedelivery(Boolean enabled) {mRedelivery = enabled; } @Override public void onCreate() { super.onCreate(); // Create a HandlerThread here, HandlerThread thread = new HandlerThread("IntentService[" + mName + "]"); HandlerThread = new HandlerThread("IntentService[" + mName + "]"); // Start thread.start(); MServiceLooper = thread.getLooper(); mServiceHandler = new ServiceHandler(mServiceLooper); } @override public void onStart(@override Intent Intent Intent, int startId) { Originally constructed here Message concurrent out Message MSG = mServiceHandler. ObtainMessage (); msg.arg1 = startId; msg.obj = intent; / / obj is Intent, just turn the strong is safe mServiceHandler sendMessage (MSG); @override public int startCommand (@override Intent Intent, int flags, int startId) {Override public int startCommand (@override Intent Intent, int startId); // Different flag return mRedelivery? START_REDELIVER_INTENT : START_NOT_STICKY; } @override public void onDestroy() {// Stop Thread (); } @Override @Nullable public IBinder onBind(Intent intent) { return null; } // Use the template mode to define the top-level abstraction of onHandleIntent() for the lower implementation. The top layer decides when to call onHandleIntent. @workerthread protected abstract void onHandleIntent(@nullable Intent Intent); }Copy the code

3 summary

OnCreate () : IntentService() : onCreate() : IntentService() : onCreate() Create your own ServiceHandler 3 and then construct a message in onStart(Intent) and send it. OnHandleIntent (intent (msg.obj)); In an onHandleIntent(intent) that handles its own business logic, the parameters are taken from the intent. OnHandleIntent () runs out. 6. Call mServicelooper.quit () from onDestroy() and exit the worker thread


IntentService: HandlerThread: intentThread: thread: intentThread: thread: thread: thread: thread: thread: thread: thread: thread: thread: thread: thread: thread: thread: thread: thread: thread: thread