service浅解

来源:互联网 发布:程序员转行当老师 编辑:程序博客网 时间:2024/06/06 00:29

在项目中,我在service中作如下操作:

          从数据库中读取数据,然后把读取到的数据写入了另一个数据库表的同时,还把它通过串口传往另一个终端。

运行时发现log中有如下提醒:

      The application may be doing too much work on its main thread.

回头查看Android API,其中有对service如下解释:

Note that services, like other application objects, run in the main thread of their hosting process. This means that, if your service is going to do any CPU intensive (such as MP3 playback) or blocking (such as networking) operations, it should spawn its own thread in which to do that work.

Most confusion about the Service class actually revolves around what it is not:

  • A Service is not a separate process. The Service object itself does not imply it is running in its own process; unless otherwise specified, it runs in the same process as the application it is part of.
  • A Service is not a thread. It is not a means itself to do work off of the main thread (to avoid Application Not Responding errors).
豁然开朗,原来service也是运行在主线程中,那么这些耗时操作就不要放在service当中直接操作了。好办,起一个线程,运行起来之后再也没有类似提醒了!
0 0
原创粉丝点击