IntentService:


IntentService是一个通过Context.startService(Intent)启动可以处理异步请求的Service,使用时你只需要继承IntentService和重写其中的onHandleIntent(Intent)方法接收一个Intent对象,在适当的时候会停止自己(一般在工作完成的时候). 所有的请求的处理都在一个工作线程中完成,它们会交替执行(但不会阻塞主线程的执行),一次只能执行一个请求.(**本人修改了原文的一些翻译)


下面是要分析的源码:


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 实际上是Looper,Handler,Service 的集合体,他不仅有服务的功能,还有处理和循环消息的功能.


下面是onCreate()的源码:


@Override 
         public void onCreate() { 
                 super.onCreate(); 

                 HandlerThread thread = new HandlerThread("IntentService[" + mName + "]"); 
                 thread.start(); 

                 mServiceLooper = thread.getLooper(); 
                 mServiceHandler = new ServiceHandler(mServiceLooper); 
         }


分析:IntentService创建时就会创建Handler线程(HandlerThread)并且启动,然后再得到当前线程的Looper对象来初始化IntentService的mServiceLooper,接着创建mServicehandler对象.




下面是onStart()的源码:


@Override 
         public void onStart(Intent intent, int startId) { 
                 Message msg = mServiceHandler.obtainMessage(); 
                 msg.arg1 = startId; 
                 msg.obj = intent; 

                 mServiceHandler.sendMessage(msg); 
         }


分析:当你启动IntentService的时候,就会产生一条附带startId和Intent的Message并发送到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停下来.



通过对源码的分析得出:

    这是一个基于消息的服务,每次启动该服务并不是马上处理你的工作,而是首先会创建对应的Looper,Handler并且在MessageQueue中添加的附带客户Intent的Message对象,当Looper发现有Message的时候接着得到Intent对象通过在onHandleIntent((Intent)msg.obj)中调用你的处理程序.处理完后即会停止自己的服务.意思是Intent的生命周期跟你的处理的任务是一致的.所以这个类用下载任务中非常好,下载任务结束后服务自身就会结束退出.



Service:


 1.Service不是一个单独的进程 ,它和应用程序在同一个进程中。

Application Not Responding



IntentService使用队列的方式将请求的Intent加入队列,然后开启一个worker thread(线程)来处理队列中的Intent,对于异步的startService请求,IntentService会处理完成一个之后再处理第二个,每一个请求都会在一个单独的worker thread中处理,不会阻塞应用程序的主线程,这里就给我们提供了一个思路,如果有耗时的操作与其在Service里面开启新线程还不如使用IntentService来处理耗时操作



service和IntentService的区别的例子


 1.Service:




[java] view plaincopyprint?

1. package com.zhf.service;  
2.   
3. import android.app.Service;  
4. import android.content.Intent;  
5. import android.os.IBinder;  
6.   
7. public class MyService extends Service {  
8.   
9. @Override  
10. public void onCreate() {  
11. super.onCreate();  
12.     }  
13.       
14. @Override  
15. public void onStart(Intent intent, int startId) {  
16. super.onStart(intent, startId);  
17. //经测试,Service里面是不能进行耗时的操作的,必须要手动开启一个工作线程来处理耗时操作  
18. "onStart");  
19. try {  
20. 20000);  
21. catch (InterruptedException e) {  
22.             e.printStackTrace();  
23.         }  
24. "睡眠结束");  
25.     }  
26.       
27. @Override  
28. public IBinder onBind(Intent intent) {  
29. return null;  
30.     }  
31. }

           2.IntentService:





[java] view plaincopyprint?

1. package com.zhf.service;  
2.   
3. import android.app.IntentService;  
4. import android.content.Intent;  
5.   
6. public class MyIntentService extends IntentService {  
7.   
8. public MyIntentService() {  
9. super("yyyyyyyyyyy");  
10.     }  
11.   
12. @Override  
13. protected void onHandleIntent(Intent intent) {  
14. // 经测试,IntentService里面是可以进行耗时的操作的  
15. //IntentService使用队列的方式将请求的Intent加入队列,然后开启一个worker thread(线程)来处理队列中的Intent  
16. //对于异步的startService请求,IntentService会处理完成一个之后再处理第二个  
17. "onStart");  
18. try {  
19. 20000);  
20. catch (InterruptedException e) {  
21.             e.printStackTrace();  
22.         }  
23. "睡眠结束");  
24.     }  
25. }





测试主程序:



[java] view plaincopyprint?

1. package com.zhf.service;  
2.   
3. import android.app.Activity;  
4. import android.content.Intent;  
5. import android.os.Bundle;  
6.   
7. public class ServiceDemoActivity extends Activity {  
8. /** Called when the activity is first created. */  
9. @Override  
10. public void onCreate(Bundle savedInstanceState) {  
11. super.onCreate(savedInstanceState);  
12.         setContentView(R.layout.main);  
13. new Intent(this,MyService.class));//主界面阻塞,最终会出现Application not responding  
14. //连续两次启动IntentService,会发现应用程序不会阻塞,而且最重的是第二次的请求会再第一个请求结束之后运行(这个证实了IntentService采用单独的线程每次只从队列中拿出一个请求进行处理)  
15. new Intent(this,MyIntentService.class));  
16. new Intent(this,MyIntentService.class));  
17.     }  
18. }