Does Android Service run from a separated thread instead of UI?

后端 未结 3 1954
忘掉有多难
忘掉有多难 2020-12-01 07:37

I am currently using a alarmmanager to start a service for posting of location to http. The problem is when the manager starts and run the services, the ui seems to stop for

3条回答
  •  伪装坚强ぢ
    2020-12-01 08:03

    Copied From Android Docs :

    Caution: A service runs in the main thread of its hosting process—the service does not create its own thread and does not run in a separate process (unless you specify otherwise). This means that, if your service is going to do any CPU intensive work or blocking operations (such as MP3 playback or networking), you should create a new thread within the service to do that work. By using a separate thread, you will reduce the risk of Application Not Responding (ANR) errors and the application's main thread can remain dedicated to user interaction with your activities.

    Services overview

    Use IntentService if you don't want to fiddle with managing threads on your own. Or use AsyncTasks.

提交回复
热议问题