Alin
Alin

Reputation: 14571

Comunication between Service, Web and Activity, back and forth, in Android

I read most of the questions and answers around SO and I must say I still have no idea on which way to go, in my case. There are several ways to go with but I can't really decide which one is best suited for my case.

What I've got

I have an Service that gets started from my app. It checks the current location of the user and calls a web service with the read location. This occurs every x meters.

What I want to obtain

Having this said, what would you recommend me to use ? Would this be a good approach: Example: Communication between Activity and Service using Messaging ?

LE: I have switched from IntentService to simple Service + thread for sending data to the web service. My IntentService implementation started and quickly for destroyed. Probably I did something wrong....

Upvotes: 1

Views: 495

Answers (2)

stuckless
stuckless

Reputation: 6545

Another approach to take with this is using Handler's as per this response.

The article listed talks about how to pass data from a service to a UI other than the UI that started it... and while this isn't your question/problem, you can still use the same pattern.

Basically your Activity, when it starts, will register itself as the handler callback and start the location service. The location service will post messages to the handler callback and will be handled by your activity.

On the activity terminate, you stop the service and then remove yourself as the handler callback.

If the service is always running, but doesn't know if the UI is there or not, then this pattern can be used as well, since the proxy handler checks if there is a real callback, and if not, then the message is simply dropped.

Upvotes: 0

Simon Dorociak
Simon Dorociak

Reputation: 33505

This means my Activity needs to be informed on each location read.

I think that a clean and efficient solution for your goal is to use ResultReceiver.

Note: (Bellow i will show you example from my application).

Example:

Activity from its you call Service.

Intent i = new Intent(this, DownloadService.class);
i.putExtra("url", "http://dl.dropbox.com/u/67617541/2011-11-11_102220_nature.jpg");
i.putExtra("receiver", new DownloadReceiver(new Handler()));
startService(i);

Implemented ResultReceiver

private class DownloadReceiver extends ResultReceiver {

        public DownloadReceiver(Handler handler) {
            super(handler);
        }

        @Override
        public void onReceiveResult(int resultCode, Bundle resultData) {
           super.onReceiveResult(resultCode, resultData);
           if (resultCode == DownloadService.PROGRESS_UPDATE) {
              int progress = resultData.getInt("progress");
              pd.setProgress(progress);
           }
       }
}

And fragment of IntentService

protected void onHandleIntent(Intent intent) {  
   String urlLink = intent.getStringExtra("url");
   ResultReceiver receiver = intent.getParcelableExtra("receiver");
   // some body
   Bundle data = new Bundle();
   //publishing progress
   receiver.send(PROGRESS_UPDATE, data);
}

So simply by calling send (when you need) method will be called onReceiveResult in your ResultReceiver where you can do what you need.

Upvotes: 3

Related Questions