Android 在服务中使用活套与使用单独的线程相同吗?

Android 在服务中使用活套与使用单独的线程相同吗?,android,multithreading,service,looper,Android,Multithreading,Service,Looper,在文档()中的这个示例中,我们使用线程的“looper”,并在服务类中使用它,然后服务将像在单独的线程中一样工作 public class HelloService extends Service { private Looper mServiceLooper; private ServiceHandler mServiceHandler; // Handler that receives messages from the thread private final class

在文档()中的这个示例中,我们使用线程的“looper”,并在服务类中使用它,然后服务将像在单独的线程中一样工作

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(); 
  }
}
公共类HelloService扩展服务{
专用活套;
私有服务处理程序mServiceHandler;
//从线程接收消息的处理程序
私有最终类ServiceHandler扩展处理程序{
公共服务处理程序(活套-活套){
超级(活套);
}
@凌驾
公共无效handleMessage(消息消息消息){
//通常我们会在这里做一些工作,比如下载一个文件。
//对于我们的示例,我们只需睡眠5秒钟。
long-endTime=System.currentTimeMillis()+5*1000;
while(System.currentTimeMillis()
谢谢

调用
thread.start()时,线程(一个
HandlerThread
)在
onCreate
中启动
,然后您将获得对该线程的
活套
的引用(每个
HandlerThread
仅创建一个
活套
),以创建
处理程序
,并且
处理程序
用于向线程发布消息。
循环器是在
while(true)
循环中等待消息的对象

每次向
服务
发送命令时,
服务
都会通过
处理程序
句柄线程
发送消息

仔细看一下源代码将帮助您更好地理解它是如何工作的。有一篇关于
Handler
s和
Looper
s的精彩文章

您还可以使用实例来避免实例化自己的线程