zoukankan      html  css  js  c++  java
  • Android中IntentService与Service的区别

    Android中的Service是用于后台服务的,当应用程序被挂到后台的时候,问了保证应用某些组件仍然可以工作而引入了Service这个概念,那么这里面要强调的是Service不是独立的进程,也不是独立的线程,它是依赖于应用程序的主线程的,也就是说,在更多时候不建议在Service中编写耗时的逻辑和操作,否则会引起ANR。

    那么我们当我们编写的耗时逻辑,不得不被service来管理的时候,就需要引入IntentService,IntentService是继承Service的,那么它包含了Service的全部特性,当然也包含service的生命周期,那么与service不同的是,IntentService在执行onCreate操作的时候,内部开了一个线程,去你执行你的耗时操作。

    这里我 需要解释以下几个方法,也许大家都已经很清楚了,不过为了抛砖引玉,我还是要提一嘴。

    Service中提供了一个方法:

    1. public int onStartCommand(Intent intent, int flags, int startId) {  
    2.      onStart(intent, startId);  
    3.      return mStartCompatibility ? START_STICKY_COMPATIBILITY : START_STICKY;  
    4.  }  
       public int onStartCommand(Intent intent, int flags, int startId) {
            onStart(intent, startId);
            return mStartCompatibility ? START_STICKY_COMPATIBILITY : START_STICKY;
        }

    这个方法的具体含义是,当你的需要这个service启动的时候,或者调用这个servcie的时候,那么这个方法首先是要被回调的。

    同时IntentService中提供了这么一个方法:

    1. protected abstract void onHandleIntent(Intent intent);  
    protected abstract void onHandleIntent(Intent intent);

    这是一个抽象方法,也就是说具体的实现需要被延伸到子类。

    子类的声明:

    1. public class ChargeService extends IntentService   
    public class ChargeService extends IntentService 

    上面提到过IntentService是继承Service的,那么这个子类也肯定继承service,那么onHandleIntent()方法是什么时候被调用的呢?让我们具体看IntentService的内部实现:

    1. private final class ServiceHandler extends Handler {  
    2.     public ServiceHandler(Looper looper) {  
    3.         super(looper);  
    4.     }  
    5.   
    6.     @Override  
    7.     public void handleMessage(Message msg) {  
    8.         onHandleIntent((Intent)msg.obj);  
    9.         stopSelf(msg.arg1);  
    10.     }  
    11. }  
    12.   
    13. /** 
    14.  * Creates an IntentService.  Invoked by your subclass's constructor. 
    15.  * 
    16.  * @param name Used to name the worker thread, important only for debugging. 
    17.  */  
    18. public IntentService(String name) {  
    19.     super();  
    20.     mName = name;  
    21. }  
    22.   
    23. /** 
    24.  * Sets intent redelivery preferences.  Usually called from the constructor 
    25.  * with your preferred semantics. 
    26.  * 
    27.  * <p>If enabled is true, 
    28.  * {@link #onStartCommand(Intent, int, int)} will return 
    29.  * {@link Service#START_REDELIVER_INTENT}, so if this process dies before 
    30.  * {@link #onHandleIntent(Intent)} returns, the process will be restarted 
    31.  * and the intent redelivered.  If multiple Intents have been sent, only 
    32.  * the most recent one is guaranteed to be redelivered. 
    33.  * 
    34.  * <p>If enabled is false (the default), 
    35.  * {@link #onStartCommand(Intent, int, int)} will return 
    36.  * {@link Service#START_NOT_STICKY}, and if the process dies, the Intent 
    37.  * dies along with it. 
    38.  */  
    39. public void setIntentRedelivery(boolean enabled) {  
    40.     mRedelivery = enabled;  
    41. }  
    42.   
    43. @Override  
    44. public void onCreate() {  
    45.     // TODO: It would be nice to have an option to hold a partial wakelock  
    46.     // during processing, and to have a static startService(Context, Intent)  
    47.     // method that would launch the service & hand off a wakelock.  
    48.   
    49.     super.onCreate();  
    50.     HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");  
    51.     thread.start();  
    52.   
    53.     mServiceLooper = thread.getLooper();  
    54.     mServiceHandler = new ServiceHandler(mServiceLooper);  
    55. }  
    56.   
    57. @Override  
    58. public void onStart(Intent intent, int startId) {  
    59.     Message msg = mServiceHandler.obtainMessage();  
    60.     msg.arg1 = startId;  
    61.     msg.obj = intent;  
    62.     mServiceHandler.sendMessage(msg);  
    63. }  
        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);
            }
        }
    
        /**
         * Creates an IntentService.  Invoked by your subclass's constructor.
         *
         * @param name Used to name the worker thread, important only for debugging.
         */
        public IntentService(String name) {
            super();
            mName = name;
        }
    
        /**
         * Sets intent redelivery preferences.  Usually called from the constructor
         * with your preferred semantics.
         *
         * <p>If enabled is true,
         * {@link #onStartCommand(Intent, int, int)} will return
         * {@link Service#START_REDELIVER_INTENT}, so if this process dies before
         * {@link #onHandleIntent(Intent)} returns, the process will be restarted
         * and the intent redelivered.  If multiple Intents have been sent, only
         * the most recent one is guaranteed to be redelivered.
         *
         * <p>If enabled is false (the default),
         * {@link #onStartCommand(Intent, int, int)} will return
         * {@link Service#START_NOT_STICKY}, and if the process dies, the Intent
         * dies along with it.
         */
        public void setIntentRedelivery(boolean enabled) {
            mRedelivery = enabled;
        }
    
        @Override
        public void onCreate() {
            // TODO: It would be nice to have an option to hold a partial wakelock
            // during processing, and to have a static startService(Context, Intent)
            // method that would launch the service & hand off a wakelock.
    
            super.onCreate();
            HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");
            thread.start();
    
            mServiceLooper = thread.getLooper();
            mServiceHandler = new ServiceHandler(mServiceLooper);
        }
    
        @Override
        public void onStart(Intent intent, int startId) {
            Message msg = mServiceHandler.obtainMessage();
            msg.arg1 = startId;
            msg.obj = intent;
            mServiceHandler.sendMessage(msg);
        }

    在这里我们可以清楚的看到其实IntentService在执行onCreate的方法的时候,其实开了一个线程HandlerThread,并获得了当前线程队列管理的looper,并且在onStart的时候,把消息置入了消息队列,

    1. @Override  
    2.        public void handleMessage(Message msg) {  
    3.            onHandleIntent((Intent)msg.obj);  
    4.            stopSelf(msg.arg1);  
    5.        }  
     @Override
            public void handleMessage(Message msg) {
                onHandleIntent((Intent)msg.obj);
                stopSelf(msg.arg1);
            }
    

    在消息被handler接受并且回调的时候,执行了onHandlerIntent方法,该方法的实现是子类去做的。

    结论:

    IntentService是通过Handler looper message的方式实现了一个多线程的操作,同时耗时操作也可以被这个线程管理和执行,同时不会产生ANR的情况。

  • 相关阅读:
    java基础(上)
    java前奏
    Spring框架介绍
    bootstrap简单学习
    存储过程和函数
    触发器
    视图
    索引
    mysql增、删、改数据
    子查询
  • 原文地址:https://www.cnblogs.com/baorantHome/p/8031838.html
Copyright © 2011-2022 走看看