views:

55

answers:

2

hello,

i have a android service running in the background which receives the coordinates from the server every few seconds. i am not sure how do i display the coordinates on a map every time the service receives a response from the server. kindly help or give an idea.

thanks

+1  A: 

Your service could broadcast intents whenever it wants to update the displayed location on the map. The Activity displaying the map should register a receiver for that boradcast, and the boradcast's intent can hold the values for lat. and long.

Scythe
thank you for the concept Scythe. but i see that BroadCast is a class and not an interface. how will be able to use that in a service?
raqz
are you aware of any tutorial that does that
raqz
+1  A: 

I don't know any tutorials on this, but here's a version of mine:

To send a broadcast, you use the 'sendBroadcast(Intent i)' method of the Context class. The Service class extends Context, so you can access it from your implementation.

So in your Service goes:

public static final String BROADCAST_ACTION="com.yourservice.update";

public void onStart( Intent intent, int startId ) {
...
Intent broadcastIntent = new Intent(BROADCAST_ACTION); 
   sendBroadcast(broadcastIntent);
...
}

You have to register a receiver for this broadcast in you Activity (possibly before you start boradcasting them), like this:

private BroadcastReceiver receiver=new BroadcastReceiver() {
  public void onReceive(Context context, Intent intent) {

                        //Here goes handling the stuff you got from the service
   Bundle extras = intent.getExtras();
   if(extras != null)processUIUpdate(extras);
  }
};

public void onResume() {
...
//Register for the update broadcasts from the torrent service
registerReceiver(receiver, new IntentFilter(YourService.BROADCAST_ACTION));
...
}

Don't forget to deregister when the Activity goes background:

public void onPause() {
...
  //Deregister for the update broadcast from the torrent service
  unregisterReceiver(receiver);
...
}

This should work.

Scythe
Thank you so much for the sample code Scythe. now does the service automatically start the activity for the first time and start broadcasting the points? or the activity has to be started by us and later it waits to receive intent and display. also, how do i refresh the activity to display the new data. could you please explain.
raqz
When your Activity starts, you register the BroadcastReceiver. Then, you start the service which starts sending these broadcasts. So no, the service will not start your Activity, it's the opposite. (Actually, you could achieve that behaviour as well, but it's not recommended) Your custom BroadcastReceiver implementation (the one you register on Activity startup) can be an inner class in your Activity. So in it's onReceive() callback, you can update the Activity's UI based on the info you got from the broadcast message.
Scythe
thank you Scythe. I was able to implement that it works great. But how about broadcasting intents every ten seconds..the service has to poll a server every 10 seconds and show the points on the map. i am trying to using the AlarmManager class but not able to figure that out..any suggestion?
raqz