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

Hay Abadi
Hay Abadi
2,625 Points

stock on 2/2 Messengers and Handlers:services

in the second task i need to "Now create a Messenger using this Handler and return it in onBind."

i try without succses.

my code:

public class GeolocationService extends Service {
    int mLatitude;
    int mLongitude;
//private GeolocationService  mGeolocationService;
  private Messenger mMessenger = new Messenger(new LocationHandler(GeolocationService.this));


    // some methods omitted

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

  public class LocationHandler extends Handler {

}

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

and this what the compailer says:

./GeolocationService.java:28: error: class, interface, or enum expected } ^

./GeolocationService.java:5: error: constructor LocationHandler in class GeolocationService.LocationHandler cannot be applied to given types; private Messenger mMessenger = new Messenger(new LocationHandler(GeolocationService.this)); ^

required: no arguments found: GeolocationService reason: actual and formal argument lists differ in length ./GeolocationService.java:19: error: method does not override or implement a method from a supertype @Override ^

3 errors

1 Answer

Kourosh Raeen
Kourosh Raeen
23,733 Points

The handleMessage() method should be inside the LocationHandler class. Also, don't pass anything to the LocationHandler() constructor:

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

    private Messenger mMessenger = new Messenger(new LocationHandler());

    // some methods omitted

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

    public class LocationHandler extends Handler {
      @Override
      public void handleMessage(Message message){
        mLatitude = getLatitude();
        mLongitude = getLongitude();  
      } 
    } 
}