2

I have an abstract of my GPSTracker. It returns the location of the user. It's works.

public class GPSTracker extends Service implements LocationListener {


public GPSTracker(Context context) {
    this.mContext = context;
    getLocation();

}

public Location getLocation() {
    //GET LOCATION CODE
    return location;
}

So, I call this service in my MainActivity using this code:

gps = new GPSTracker(this, marker, googleMap, playbackService);

And it's getting the actual location of the user. I want to get this location on my Activity. The service need to send the Location to my activity. Can anyone helpme?

matiash
  • 53,931
  • 16
  • 121
  • 152
Guilherme
  • 89
  • 4
  • 9

2 Answers2

3

Depending on your requirements, an activity and a service can communicate in 3 ways, using:

1) Broadcast receiver

2) Aidl – Android Interface Definition Language

3) Messenger

Just click on each method and see which one meets your needs.

Ayaz Alifov
  • 7,574
  • 4
  • 54
  • 53
0

You need to create a BroadcastReceiver in your activity (be sure to register it in onResume() and unregister it in onPause()) and notify it via a broadcast, providing an Intent.

See the documentation for BroadcastReciever (and also for LocalBroadcastManager which is meant exactly for this scenario, where the broadcast is local to your own application).

You should also read this answer, for example.

Community
  • 1
  • 1
matiash
  • 53,931
  • 16
  • 121
  • 152