IntentService

簡介:繼承自Service,可以做耗時任務的Service。

  1. 使用
    • 創建Service繼承IntentService

      創建 MyIntentService 繼承IntentService ,實現 onHandleIntent方法。

      public class MyIntentService extends IntentService {
      
          private String TAG = getClass().getSimpleName();
      
      
          public MyIntentService() {
              super("MyIntentService");
          }
      
          @Override
          protected void onHandleIntent(Intent intent) {
              Log.d(TAG, "onHandleIntent BEGIN");
              try {
                  Thread.sleep(11000);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
             String value= intent.getStringExtra("key");
              Log.d(TAG, "onHandleIntent " +value);
              Log.d(TAG, "onHandleIntent END");
          }
      
          @Override
          public void onCreate() {
              super.onCreate();
              Log.d(TAG, "onCreate");
          }
      
          @Override
          public void onStart(Intent intent, int startId) {
              super.onStart(intent, startId);
              Log.d(TAG, "onStart");
          }
      
          @Override
          public int onStartCommand(Intent intent, int flags, int startId) {
              Log.d(TAG, "onStartCommand");
              return super.onStartCommand(intent, flags, startId);
          }
      
          @Override
          public void onDestroy() {
              super.onDestroy();
              Log.d(TAG, "onDestroy");
          }
      
      
    • 開啓Service

      public class MainActivity extends AppCompatActivity {
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              Intent intent=new Intent(this,MyIntentService.class);
              intent.putExtra("key","hello");
              startService(intent);
          }
      }
      
  2. 源碼解析

​ 首先看一個問題,在 onHandleIntent中我先 sleep 了11秒,然後處理intent,爲什麼沒有Anr呢。

​ 第二問題,爲什麼會出現IntentService呢,IntentService與Service的區別。接下來通過源碼去看上面兩個問 題。當開啓Service時會先調用 onCreate 方法,接下來就先查看onCreate的源碼 看看究竟做了什麼

 @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);
    }

​ 在onCreate中當創建了HandlerThread,HandlerThread繼承自Thread,然後開啓了線程,接下來調用了HandlerThread的getLooper方法,獲取Looper,然後創建ServiceHandler,ServiceHandler是一個Handler類。

到這裏,看到Handler,能想到IntentService內部封裝了Handler的消息機制。接下來分析上述代碼。

開啓線程會調用HandlerThread的run方法。

  @Override
    public void run() {
        mTid = Process.myTid();
        Looper.prepare();
        synchronized (this) {
            mLooper = Looper.myLooper();
            notifyAll();
        }
        Process.setThreadPriority(mPriority);
        onLooperPrepared();
        Looper.loop();
        mTid = -1;
    }

在方法中,首先調用Looper.prepare()方法,該方法創建Looper類和消息隊列,然後獲取鎖,獲取Looper對象,並調用notifyAll(),喚醒在等待池中的等待的線程。接着調用Looper.loop()方法,開啓消息隊列循環處理消息。

接下來分析thread.getLooper()這句代碼。

   public Looper getLooper() {
        if (!isAlive()) {
            return null;
        }
        
        // If the thread has been started, wait until the looper has been created.
        synchronized (this) {
            while (isAlive() && mLooper == null) {
                try {
                    wait();
                } catch (InterruptedException e) {
                }
            }
        }
        return mLooper;
    }

在該方法內,獲取鎖,並添加條件Looper是爲null的話,調用wait,進入等待。剛在上述的線程的run方法中,獲取到looper對象時,調用了notifyAll方法。總結上述代碼。就是創建了Looper對象和消息隊列,創建Handler,接下來要找到是如何調用sendMessage方法來添加消息。

接下來分析onStartCommand方法。

 @Override
    public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
        onStart(intent, startId);
        return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY;
    }

在該方法內調用了onStart方法。

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

在該方法內終於找到了添加消息的方法。在這個方法內發送消息,然後然後在消息隊列中一直在循環消息,若有消息便會處理,並最終調用Handler的handlerMessage方法。

   public ServiceHandler(Looper looper) {
            super(looper);
        }

        @Override
        public void handleMessage(Message msg) {
            onHandleIntent((Intent)msg.obj);
            stopSelf(msg.arg1);
        }
    }

剛方法內調用了onHandleIntent這個抽象方法。並最終由我們自己的書寫的Service實現。

然後回頭看我們開頭的第一個問題,問什麼沒有anr呢,因爲我們的looper.loop()方法是在HandlerThread中的run方法調用的,looper.loop()最終會調用handler的handlerMessage方法,所以handlerMessage是在HandlerThread這個工作線程中調用的,不是在主線程中調用。所以不會出現anr。

IntentService與service的區別:Service是運行在主線程中的,所以Service的方法不能做耗時任務,不然會出現anr,IntentService是專門處理在Service中做耗時任務的類。因此,若以後需要早Service中處理耗時任務不必在Service中再開線程處理,可以直接用IntentService處理。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章