Is there any callback mechanism in android when there is data available to read in socket

前端 未结 3 1709
遇见更好的自我
遇见更好的自我 2021-01-01 22:03

Well I am familiar with socket programming in c, iOS environment..But now trying to connect my android and my remote server via sockets...As a start up, I wrote a simple ser

3条回答
  •  爱一瞬间的悲伤
    2021-01-01 22:55

    If you're looking for a sample of an Android app that uses a service to manage background long-term connections, check out ConnectBot:

    http://code.google.com/p/connectbot/source/browse/

    It uses a service to manage terminal connections. It's a very complex set of code however, probably take some digging to figure out the bits you want to make use of.

    Most folks do server connections via HTTP, and there's a one-to-one mapping between request contexts and callbacks. So we can get away with an IntentService to handle backgrounding the requests, and pass in a ResultReceiver to get the service to call back to the UI thread when the request is completed/failed:

    http://developer.android.com/reference/android/os/ResultReceiver.html

    The same general technique should apply for long-running socket connections, though you'll probably have to opt for a different form of service so that you can manage the lifecycle on your own. But if you do, you should be able to take an intent in with a ResultReceiver as part of the requeue, queue it up for processing, and then send() on the ResultReceiver to get the callback out to the UI.

提交回复
热议问题