using a Looper in a Service is the same as using a separate thread?

家住魔仙堡 提交于 2019-12-10 20:00:47

问题


In this example from the documentation (https://developer.android.com/guide/components/services.html#ExtendingService), we use the "looper" of a thread, and we use it in the Service class, and then the Service will be working as if it was in a separate thread?

public class HelloService extends Service {
  private Looper mServiceLooper;
  private ServiceHandler mServiceHandler;

  // Handler that receives messages from the thread
  private final class ServiceHandler extends Handler {
      public ServiceHandler(Looper looper) {
          super(looper);
      }
      @Override
      public void handleMessage(Message msg) {
          // Normally we would do some work here, like download a file.
          // For our sample, we just sleep for 5 seconds.
          long endTime = System.currentTimeMillis() + 5*1000;
          while (System.currentTimeMillis() < endTime) {
              synchronized (this) {
                  try {
                      wait(endTime - System.currentTimeMillis());
                  } catch (Exception e) {
                  }
              }
          }
          // Stop the service using the startId, so that we don't stop
          // the service in the middle of handling another job
          stopSelf(msg.arg1);
      }
  }

  @Override
  public void onCreate() {
    // Start up the thread running the service.  Note that we create a
    // separate thread because the service normally runs in the process's
    // main thread, which we don't want to block.  We also make it
    // background priority so CPU-intensive work will not disrupt our UI.
    HandlerThread thread = new HandlerThread("ServiceStartArguments",
            Process.THREAD_PRIORITY_BACKGROUND);
    thread.start();

    // Get the HandlerThread's Looper and use it for our Handler 
    mServiceLooper = thread.getLooper();
    mServiceHandler = new ServiceHandler(mServiceLooper);
  }

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
      Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();

      // For each start request, send a message to start a job and deliver the
      // start ID so we know which request we're stopping when we finish the job
      Message msg = mServiceHandler.obtainMessage();
      msg.arg1 = startId;
      mServiceHandler.sendMessage(msg);

      // If we get killed, after returning from here, restart
      return START_STICKY;
  }

  @Override
  public IBinder onBind(Intent intent) {
      // We don't provide binding, so return null
      return null;
  }

  @Override
  public void onDestroy() {
    Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show(); 
  }
}

Thanks


回答1:


The thread (a HandlerThread) is started in onCreate, when you call thread.start();, then you get a reference to the Looper of that thread (only one Looper is created per HandlerThread) to create a Handler and the Handler is used to post messages to the thread. The Looper is the object that waits for the messages in a while(true) loop.

Every time a command is sent to the Service, the Service posts a message to the HandlerThread through the Handler.

A closer look at the source code will help you understand better how it all works. There's an excelente post about Handlers and Loopers at Square Engineering Blog - A journey on the Android Main Thread - Part 1.

You can also use an IntentService to avoid instantiating your own threads.



来源:https://stackoverflow.com/questions/19825879/using-a-looper-in-a-service-is-the-same-as-using-a-separate-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!