i_raqz
i_raqz

Reputation: 2959

service displays points on map android

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

Upvotes: 0

Views: 393

Answers (2)

Zsombor Erdődy-Nagy
Zsombor Erdődy-Nagy

Reputation: 16914

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.

Upvotes: 1

Zsombor Erdődy-Nagy
Zsombor Erdődy-Nagy

Reputation: 16914

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.

Upvotes: 1

Related Questions