1.在 onCreate() 方法中,新建一个 HandlerThread 对象 handlerThread;新建 ServiceHandler 的对象 serviceHandler,同时将 handlerThread 的 looper 设置给 serviceHandler;
2.在 onStart() 方法中将 intent 作为 Message 对象的 obj 发送给 serviceHandler 处理,而 serviceHandler 的 handleMessage 方法中回调了 onHandleIntent ( Intent intent ) 方法。由于 serviceHandler 的 looper 是在子线程的,所以 onHandleIntent() 方法也是在子线程中执行的。因此,使用 IntentService 是可以在 onHandleIntent() 方法中做耗时操作。
3.在 onHandleIntent() 方法执行完后,又调用了 stopSelf() 方法停止service,所以 IntentService 在执行完耗时操作后会自动停止服务。
Android IntentService 执行流程
最新推荐文章于 2024-11-25 13:56:51 发布