Android Service and UI Thread

后端 未结 2 824
情书的邮戳
情书的邮戳 2021-01-02 04:24

I have a Service running in my app..

This Service has an object for sending message to the server and a function to get that object and use it.

The object is

相关标签:
2条回答
  • 2021-01-02 04:30

    As far I know, Android service run on UI thread. If you want to make an asynchronous job, you should use Intent Service
    See: What is the difference between an IntentService and a Service?
    Here is an Intent service example you can try.

    0 讨论(0)
  • 2021-01-02 04:36

    Use IntentService instead of Service. If you do network calls in a Service, the UI will stuck. Therefore, use an IntentService since it uses a separate Thread.

    And you do not know when a server message will retrieve the data. A NullPointerException could be thrown if objects are accessed as long as network calls are still in progress. Use BroadcastReceivers to fire an Intent whenever a server responds.

    0 讨论(0)
提交回复
热议问题