开发者

Accessing Thread Started By Service

I'm having some trouble wrapping my head around this idea, and I wanted some input. I'm starting a service from my initial activity that runs a new thread (class that implements Runnable in this case, not extends Thread) in it's onCreate(). The thread first creates a TCP client socket, then begins sending requests and receiving responses every X seconds in the background as the app runs, activities switch, etc. I need to be able to access the thread from all of my activities however, to tell 开发者_运维百科it to send out custom requests when UI elements are triggered. The service can be accessed from a static context, but when I try to access the running thread instance, I get errors related to performing network operations on main thread, which means it's not the actual instance, but the UI thread I'm accessing. I thought about implementing a message queue in the shared Application class that the service could poll for new messages, but that seems ugly. Maybe I'm going about this wrong and there is a better/cleaner way to approach this problem? Any advice or help would be greatly appreciated.


You can use the Looper/Handler mechanism to send messages to the thread.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜