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
  • 54,791
  • 16
  • 125
  • 154
Guilherme
  • 89
  • 4
  • 9

3 Answers3

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
  • 8,334
  • 4
  • 61
  • 56
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
  • 54,791
  • 16
  • 125
  • 154
0

All solution came up here are good options, If you want to make an interface there is another creative solution.

  1. Create SingleTone class with interface as a member, that class will be notified by the service and the SingleTone class will trigger an interface when notified.

  2. Observe that interface from when ever you want.

Basically the Idea is to use SingleTone class as a bridge.

Jesus Dimrix
  • 4,378
  • 4
  • 28
  • 62