Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,790 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!