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

前端 未结 3 1710
遇见更好的自我
遇见更好的自我 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:31

    There is no ready-made callback for this in Java. You can easily implement one using the classes in the NIO package. You mostly need Selector and SocketChannel. If you are familiar with select(2), should be easy to follow. If not, try a NIO tutorial.

    http://download.oracle.com/javase/1.5.0/docs/api/java/nio/channels/SocketChannel.html http://download.oracle.com/javase/1.5.0/docs/api/java/nio/channels/Selector.html

    In any case, you will have to poll the socket to check if there is data, and you can't do this on the main thread. Unless your android app should manage multiple connections on the same thread, it might be a lot easier to just use blocking IO. Another thing to consider: if this has to run continuously/for a long time you need to move it to a service. Even then, the OS can kill it, if it runs out of resources, so be ready to handle reconnects.

    HTH

    0 讨论(0)
  • 2021-01-01 22:35

    Here is something you can try:

    Define a handler in java like this

    public interface NwkCallback
    {
        void handle(int code, Object obj);
    }
    

    Now define a class that handles networking operations:

    public class Nwk
    {
        static DefaultHttpClient    CLIENT  = null;
        static DefaultHttpClient c()
        {
            if (null == CLIENT)
            {
            // Create a httpclient
            }
            else
                return CLIENT;
        }
    }
    public static void onReceive(final Object data, final NwkCallback callback)
    {
        background(new Runnable()
        {
            @Override
            public void run()
            {
                try
                {
                    // Write to server and wait for a response
                    // Once you receive a response
                    // return result back using
                    // callback.handle(1,whatever_server_returned)
                }
                catch (Exception e)
                {
                    Logger.e(e);
                }
            }
        }
    });
    

    On your UI thread or elsewhere use the method onReceive as

    Nwk.onReceive(string_passed_to_server, new NwkCallback()
    {
        @Override
        public void handle(final int code, Object obj)
        {
            if(code==1)
            {
                // Print obj
            }
        }
    });
    
    0 讨论(0)
  • 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.

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