- IntentService is a base class for Services that handle asynchronous requests (expressed as Intents) on demand. Clients send requests through android.content.Context.startService(Intent) calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work.
- This "work queue processor" pattern is commonly used to offload tasks from an application's main thread. The IntentService class exists to simplify this pattern and take care of the mechanics. To use it, extend IntentService and implement onHandleIntent(Intent). IntentService will receive the Intents, launch a worker thread, and stop the service as appropriate.
- All requests are handled on a single worker thread -- they may take as long as necessary (and will not block the application's main loop), but only one request will be processed at a time.
复制代码意思是说:IntentService是一个通过Context.startService(Intent)启动可以处理异步请求的Service,使用时你只需要继承IntentService和重写其中的onHandleIntent(Intent)方法接收一个Intent对象,在适当的时候会停止自己(一般在工作完成的时候). 所有的请求的处理都在一个工作线程中完成,它们会交替执行(但不会阻塞主线程的执行),一次只能执行一个请求。
IntentService特征:
1. 会创建独立的worker线程来处理所有的Intent请求;
2. 会创建独立的worker线程来处理onHandleIntent()方法实现的代码,无需处理多线程问题;
3. 所有请求处理完成后,IntentService会自动停止,无需调用stopSelf()方法停止Service;
4. 为Service的onBind()提供默认实现,返回null;
5. 为Service的onStartCommand提供默认实现,将请求Intent添加到队列中;
IntentService的使用:
1. 继承IntentService类,并重写onHandleIntent()方法
- public class MyIntentService extends IntentService {
- public MyIntentService() {
- super("MyIntentService");
- }
- @Override
- protected void onHandleIntent(Intent intent) {
- // IntentService会使用单独的线程来执行该方法的代码
- // 该方法内执行耗时任务,在这简单的让线程等待20秒
- System.out.println("开始耗时操作....");
- SystemClock.sleep(200000);
- System.out.println("耗时操作结束....");
- }
- }
复制代码 2.在Activity中通startService(Intent)启动
- public class MainActivity extends Activity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- public void startIntentService(View source) {
- // 创建需要启动的IntentService的Intent
- Intent intent = new Intent(this, MyIntentService.class);
- startService(intent);
- }
- }
复制代码
IntentService源码分析:
- 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((Intent)msg.obj);
- stopSelf(msg.arg1);
- }
复制代码通过源码也可以看出IntentService是Service的子,实际上是Looper,Handler,Service 的集合体,他不仅有服务的功能,还有处理和循环消息的功能。
- @Override
- public void onCreate() {
- super.onCreate();
- HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");
- thread.start();
- mServiceLooper = thread.getLooper();
- mServiceHandler = new ServiceHandler(mServiceLooper);
- }
复制代码从IntentService的onCreate()方法可以看出在创建的时候就会创建Handler线程(HandlerThread)并启动,然后再得到当前线程的Looper对象来初始化IntentService的mServiceLooper,接着创建mServicehandler对象。
- @Override
- public void onStart(Intent intent, int startId) {
- Message msg = mServiceHandler.obtainMessage();
- msg.arg1 = startId;
- msg.obj = intent;
- mServiceHandler.sendMessage(msg);
- }
复制代码在IntentService启动的时候,就会创建一个Message对象并且附带startId和Intent,最终发送到MessageQueue中,接下来Looper发现MessageQueue中有Message的时候,Handler就会进行处理消息,接下来处理的代码如下:
- @Override
- public void handleMessage(Message msg) {
- onHandleIntent((Intent)msg.obj);
- stopSelf(msg.arg1);
- }
复制代码onHandleIntent((Intent)msg.obj),这是一个抽象的方法就是我们要重写实现的方法,我们可以在这个方法里面处理我们的工作.当任务完成时就会调用stopSelf(msg.arg1)这个方法来结束指定的工作,当所有的工作执行完后:就会执行onDestroy方法
- @Override
- public void onDestroy() {
- mServiceLooper.quit();
- }
复制代码服务结束后调用这个方法 mServiceLooper.quit()使looper停下来。