Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Threads and Services But Wait There's More! Messengers and Handlers: Service

"Bummer: Error #14876 - wrong coordinates received!"

On the 2nd task of this code challenge, it's saying I'm receiving the wrong coordinates. They were correct after the 1st task and now they are incorrect. I didn't modify how I was setting the latitude or longitude since the 1st task so I'm not sure where I want wrong here.

Wondering where I went wrong?

GeolocationService.java
public class GeolocationService extends Service {
    int mLatitude;
    int mLongitude;

    private Messenger messenger = new Messenger(new LocationHandler(this));
    // some methods omitted

    @Override
    public IBinder onBind(Intent intent) {
      return messenger.getBinder();
    }

  class LocationHandler extends Handler {

    Service service;

    public LocationHandler(Service service) {
       this.service = service;
    }

    @Override
    public void handleMessage(Message msg) {
      mLatitude = getLatitude();
      mLongitude = getLongitude();
    }


  }


}

1 Answer

Tonnie Fanadez
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Tonnie Fanadez
UX Design Techdegree Graduate 22,796 Points

Hello patrick ridd

I worked with your code and it was perfect!! I only made 2 changes

First I removed the LocationHandler's constructor as inner class have direct access to the outer class (GeolocationService Class) so you don't need to initialize the Service.

Secondly, inside the handleMessage() method I called the superclass method for handleMessage()

Please see the below code which passed the test;

public class GeolocationService extends Service {
    int mLatitude;
    int mLongitude;
//created messenger and passed in an instance of LocationHandler with empty constructor
    private Messenger messenger = new Messenger(new LocationHandler());
    // some methods omitted

    @Override
    public IBinder onBind(Intent intent) {
      return messenger.getBinder();
    }

  class LocationHandler extends Handler {

    //I removed the constructor for Location Handler which is really not needed

    @Override
    public void handleMessage(Message msg) {

//I also called the superclass method for handleMessage
      super.handleMessage(msg);
      mLatitude = getLatitude();
      mLongitude = getLongitude();
    }
  }

}

Thanks Tonnie, that's perfect!