service与IntentService 区别

关系:

  IntentService继承service

区别:

  IntentService 是一个带有HandlerThread的线程的service,把任务执行完成以后IntentService自动销毁。

  Service要手动 调用stopSelf()来销毁。

 

  IntentService 运行在子线程中,Service运行在主线程中

作用:

   IntentService 用于执行一次复杂的场景使用IntentService相对好一点

  Service 用于重复执行的场景

 

代码分析:

  IntentService 运行在子线程中,Service运行在主线程中

  MyService 继承Service

 1 package com.example.activitynate;
 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 IBinder onBind(Intent intent) {
11         return null;
12     }
13 
14     @Override
15     public int onStartCommand(Intent intent, int flags, int startId) {
16         System.out.println("MyService ===== 所在的线程   "+Thread.currentThread());
17         return super.onStartCommand(intent, flags, startId);
18     }
19 }

 

MyIntentService 继承IntentService

 1 package com.example.activitynate;
 2 
 3 import android.app.IntentService;
 4 import android.content.Intent;
 5 
 6 public class MyIntentService extends IntentService {
 7 
 8     public MyIntentService(String name) {
 9         super(name);
10     
11     }
12     public MyIntentService() {
13         this("lihao");
14     }
15 
16     @Override
17     protected void onHandleIntent(Intent intent) {
18         System.out.println("MyIntentService ===== 所在的线程   "+Thread.currentThread());
19     }
20 
21     
22 
23 }

运行结果:

04-07 00:41:58.245: I/System.out(5107): MyService ===== 所在的线程 Thread[main,5,main]
04-07 00:41:58.255: I/System.out(5107): MyIntentService ===== 所在的线程 Thread[IntentService[lihao],5,main]

为什么MyIntentService  会在IntentService[lihao]这个线程中

 1 public abstract class IntentService extends Service {
 2     private volatile Looper mServiceLooper;
 3     private volatile ServiceHandler mServiceHandler;
 4     private String mName;
 5     private boolean mRedelivery;
 6 
 7     private final class ServiceHandler extends Handler {
 8         public ServiceHandler(Looper looper) {
 9             super(looper);
10         }
11 
12         @Override
13         public void handleMessage(Message msg) {
14             onHandleIntent((Intent)msg.obj);
15             stopSelf(msg.arg1);
16         }
17     }
18 
19     /**
20      * Creates an IntentService.  Invoked by your subclass's constructor.
21      *
22      * @param name Used to name the worker thread, important only for debugging.
23      */
24     public IntentService(String name) {
25         super();
26         mName = name;
27     }
28 
29  
30     public void setIntentRedelivery(boolean enabled) {
31         mRedelivery = enabled;
32     }
33 
34     @Override
35     public void onCreate() {
36         // TODO: It would be nice to have an option to hold a partial wakelock
37         // during processing, and to have a static startService(Context, Intent)
38         // method that would launch the service & hand off a wakelock.
39 
40         super.onCreate();
41         HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");
42         thread.start();
43 
44         mServiceLooper = thread.getLooper();
45         mServiceHandler = new ServiceHandler(mServiceLooper);
46     }
47 
48     @Override
49     public void onStart(Intent intent, int startId) {
50         Message msg = mServiceHandler.obtainMessage();
51         msg.arg1 = startId;
52         msg.obj = intent;
53         mServiceHandler.sendMessage(msg);
54     }
55 
56   
57     @Override
58     public int onStartCommand(Intent intent, int flags, int startId) {
59         onStart(intent, startId);
60         return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY;
61     }
62 
63     @Override
64     public void onDestroy() {
65         mServiceLooper.quit();
66     }
67 
68     /**
69      * Unless you provide binding for your service, you don't need to implement this
70      * method, because the default implementation returns null. 
71      * @see android.app.Service#onBind
72      */
73     @Override
74     public IBinder onBind(Intent intent) {
75         return null;
76     }
77 
78   
79     protected abstract void onHandleIntent(Intent intent);
80 }

从源代码的41-45行中 IntentService 中维护了一个HandlerThread 和一个Handler 很容易看到我们继承的IntentService的线程的名字

由于HandlerThread是个子线程start以后生成了loop传给Handler所以Handler运行在子线程里面然后,Handler发送消息,,接收到消息以后

会调用handleMessage()方法这个方法里面又调用了onHandleIntent(),这个方法执行完成后调用stopSelf() 13-14行代码,Service将自动销毁。

 

posted @ 2015-04-07 00:53  nihao1314520  阅读(751)  评论(0编辑  收藏  举报