Java – Uses Android services to handle network connections

Uses Android services to handle network connections… here is a solution to the problem.

Uses Android services to handle network connections

I’m working on an Android app that needs to maintain a network connection to a chat server. I know I can create a service to initiate a connection to the server, but how does the service notify Android Activity of new incoming messages? The activity needs to update the View to display a new message. I’m new to Android, so thank you very much for your help. Thank you!

Solution

Can you pass handlers to your service?

First, define your handler as an interface. This is an example, so yours may be more complex.

public interface ServerResponseHandler {

    public void success(Message[] msgs); // msgs may be null if no new messages
    public void error();

}

Define your handler instance in your activity. Because it is an interface, you will provide the implementation in the activity, so you can reference the fields and methods of the enclosing activity from the handler.

public class YourActivity extends Activity {

// ... class implementation here ...

    updateUI() { 
        // TODO: UI update work here
    }

    ServerResponseHandler callback = new ServerResponseHandler() {

        @Override
        public void success(Message[] msgs) {
            // TODO: update UI with messages from msgs[]

            YourActivity.this.updateUI();
        }

        @Override
        public void error() { 
            // TODO: show error dialog here? (or handle error differently)
        }

    }

    void onCheckForMessages() { 
        networkService.checkForMessages(callback);
    }

NetworkService will contain the following:

void checkForMessages(ServerResponseHandler callback) { 

    // TODO: contact server, check for new messages here

    // call back to UI
    if (successful) { 
        callback.success(msgs);
    } else {
        callback.error();
    }
}  

Also, as Aleadam says, you should also stay away from situations where services run on the same thread by default. This is usually not the preferred behavior such as networking. Android Fundamentals Page on Services explicitly warns against networking without a separate thread:

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.

For more information on using threads in your service, check out the SO articles Application threads vs Service threads and How to start service in new thread in android

Related Problems and Solutions