1

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

  • Have an activity with a map and display the current location readings from the service. This means my Activity needs to be informed on each location read. Keep in mind that this will be use only when the user starts the activity so I don't think that broadcasting each location from the service would be a good idea.
  • An activity where I can ask the service for its last read location.
  • As I mentioned before, the service reads location and needs to send it to a web server every few seconds. What would be the best approach for this ? In Service make a new Thread() for each web service call ? Use AsyncTask ? Sometimes I may receive as a response some extra values, case in which I need to start an Activity from the webservice.

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....

Community
  • 1
  • 1
Alin
  • 14,809
  • 40
  • 129
  • 218

2 Answers2

3

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.

Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
  • I have switched from IntentService to simple Service. Maybe I did something wrong with my implementation, but my IntentService started and quickly got destroyed. So I switched to Service + thread and now it works. Thanks for your answer. – Alin Jun 21 '12 at 12:28
0

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.

Community
  • 1
  • 1
stuckless
  • 6,515
  • 2
  • 19
  • 27