Android知识Android开发经验谈Android开发

IntentService源码解析

2017-06-24  本文已影响75人  一只好奇的茂

为什么我们需要IntentService ?

Android中的IntentService是继承自Service类的,在我们讨论IntentService之前,我们先想一下Service的特点: Service的回调方法(onCreate、onStartCommand、onBind、onDestroy)都是运行在主线程中的。当我们通过startService启动Service之后,我们就需要在Service的onStartCommand方法中写代码完成工作,但是onStartCommand是运行在主线程中的,如果我们需要在此处完成一些网络请求或IO等耗时操作,这样就会阻塞主线程UI无响应,从而出现ANR现象。为了解决这种问题,最好的办法就是在onStartCommand中创建一个新的线程,并把耗时代码放到这个新线程中执行。由此看来,创建一个带有工作线程的Service是一种很常见的需求(因为工作线程不会阻塞主线程),所以Android为了简化开发带有工作线程的Service,Android额外开发了一个类——–IntentService。

IntentService的特点

IntentService的使用

MainActivity

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent bindIntent = new Intent(this, MyIntentService.class);
        bindIntent.putExtra("name","task1");
        startService(bindIntent);
        bindIntent.putExtra("name","task2");
        startService(bindIntent);
        bindIntent.putExtra("name","task2");
        startService(bindIntent);
    }
}

MyIntentService

public class MyIntentService extends IntentService {
    String Tag = "MyIntentService";

    public MyIntentService() {
        super("MyIntentService");
        Log.i(Tag, "MyIntentService构造函数, Thread: " + Thread.currentThread().getName());
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(Tag, "MyIntentService -> onCreate, Thread: " + Thread.currentThread().getName());
    }

    @Override
    public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
        Log.i(Tag, "MyIntentService -> onStartCommand, Thread: " + Thread.currentThread().getName() + " , startId: " + startId);
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(Tag, "MyIntentService -> onDestroy, Thread: " + Thread.currentThread().getName());
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        String name = intent.getStringExtra("name");
        //执行下载任务,无法新开线程;
        Log.i(Tag, "MyIntentService -> onHandleIntent, Thread: " + Thread.currentThread().getName() + ", 《" + name + "》任务完成");
    }
}

结果

06-24 03:18:09.819 27760-27760/? I/MyIntentService: MyIntentService构造函数, Thread: main
06-24 03:18:09.819 27760-27760/? I/MyIntentService: MyIntentService -> onCreate, Thread: main
06-24 03:18:09.819 27760-27760/? I/MyIntentService: MyIntentService -> onStartCommand, Thread: main , startId: 1
06-24 03:18:09.819 27760-27760/? I/MyIntentService: MyIntentService -> onStartCommand, Thread: main , startId: 2
06-24 03:18:09.819 27760-27760/? I/MyIntentService: MyIntentService -> onStartCommand, Thread: main , startId: 3
06-24 03:18:09.820 27760-27781/? I/MyIntentService: MyIntentService -> onHandleIntent, Thread: IntentService[MyIntentService], 《task1》任务完成
06-24 03:18:09.820 27760-27781/? I/MyIntentService: MyIntentService -> onHandleIntent, Thread: IntentService[MyIntentService], 《task2》任务完成
06-24 03:18:09.820 27760-27781/? I/MyIntentService: MyIntentService -> onHandleIntent, Thread: IntentService[MyIntentService], 《task2》任务完成
06-24 03:18:10.166 27760-27760/? I/MyIntentService: MyIntentService -> onDestroy, Thread: main

通过以上的输出结果我们可以发现,MyIntentService的onCreate、onStartCommand、onDestroy回调方法都是运行在主线程main中的,而onHandleIntent是运行在工作线程IntentService[MyIntentService]中的,这验证了我们上面所说的IntentService的第一个和第二个特点。
通过上面的输出结果我们还会发现,在我们连续调用了三次startService(intent)之后,onStartCommand依次被调用了三次,然后依次执行了onHandleIntent三次,这样就依次完成了job,当最后一个job完成,也就是在最后一次onHandleIntent调用完成之后,整个IntentService的工作都完成,执行onDestroy回调方法,IntentService销毁。

IntentService源码分析

intentservice = HandlerThread + Service

 
public abstract class IntentService extends Service {
 private volatile Looper mServiceLooper;
 private volatile ServiceHandler mServiceHandler;
 private String mName;
 private boolean mRedelivery;
 
 private final class ServiceHandler extends Handler {
  public ServiceHandler(Looper looper) {
   super(looper);
  }
 
  @Override
  public void handleMessage(Message msg) {
   //在工作线程中调用onHandleIntent,确保onHandleIntent不会阻塞主线程
   onHandleIntent((Intent)msg.obj);
   //在执行完了onHandleIntent之后,我们需要调用stopSelf(startId)声明某个job完成了
   //当所有job完成的时候,Android就会回调onDestroy方法,销毁IntentService
   stopSelf(msg.arg1);
  }
 }
 
 public IntentService(String name) {
  //此处的name将用作线程名称
  super();
  mName = name;
 }
 
 public void setIntentRedelivery(boolean enabled) {
  mRedelivery = enabled;
 }
 
 @Override
 public void onCreate() {
  super.onCreate();
  //创建HandlerThread,利用mName作为线程名称,HandlerThread是IntentService的工作线程
  HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");
  thread.start();
 
  mServiceLooper = thread.getLooper();
  //将创建的HandlerThread所绑定的looper对象传递给ServiceHandler,
  //这样我们创建的Handler就和HandlerThread通过消息队列绑定在了一起
  mServiceHandler = new ServiceHandler(mServiceLooper);
 }
 
 @Override
 public void onStart(Intent intent, int startId) {
  //在此方法中创建Message对象,并将intent作为Message的obj参数,
  //这样Message与Intent就关联起来了
  Message msg = mServiceHandler.obtainMessage();
  msg.arg1 = startId;
  msg.obj = intent;
  //将关联了Intent信息的Message发送给Handler
  mServiceHandler.sendMessage(msg);
 }
 
 @Override
 public int onStartCommand(Intent intent, int flags, int startId) {
  //IntentService重写了onStartCommand回调方法:在内部调用onStart回调方法
  //所以我们在继承IntentService时,不应该再覆写该方法,即便覆盖该方法,我们也应该调用super.onStartCommand()
  onStart(intent, startId);
  return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY;
 }
 
 @Override
 public void onDestroy() {
  //在onDestroy方法中调用了Handler的quit方法,该方法会终止消息循环
  mServiceLooper.quit();
 }
 
 @Override
 public IBinder onBind(Intent intent) {
  return null;
 }

 protected abstract void onHandleIntent(Intent intent);
}
上一篇下一篇

猜你喜欢

热点阅读