TheMohican
TheMohican

Reputation: 87

Binder method in java

Can anyone explain me this binder class and method

 public class LocalBinder extends Binder 
{
    CC2540Service getService() 
    {
        return CC2540Service.this;
    }
}

@Override
public IBinder onBind(Intent arg0) 
{
    return binder;
}

private final IBinder binder = new LocalBinder();

I don't really understand this code

Thanks in advance

Upvotes: 1

Views: 913

Answers (2)

EyalBellisha
EyalBellisha

Reputation: 1914

In order to send and receive data from a service, you need to use a Binder object. The service called CC2540Service simply creates this object and returns a token to it whenever an Activity tries to bind to this service.

The only way two processes can transfer data between one and the other is by using these Binder tokens. In your case the token is returned via return binder;

Upvotes: 3

Tesseract
Tesseract

Reputation: 8139

I'm guessing LocalBinder is an inner class of CC2540Service. So CC2540Service.this refers to the instance of CC2540Service. So it's equivalent to this code

class A {
  A a = this;
  class B {
    A getA() {
      return a;
    }
  }
  B getB() {
    return new B();
  }
}

Upvotes: 2

Related Questions