jorrebor
jorrebor

Reputation: 2232

Problems with binding to service in Android

I have created a service, called LocalService, and I want to bind to it so that it can perform some action and give the result back to the calling client. I've been trying to get the example of the android dev site to work but eclipse gives a compile time error on this method:

void doBindService() {
    // Establish a connection with the service. We use an explicit
    // class name because we want a specific service implementation that
    // we know will be running in our own process (and thus won't be
    // supporting component replacement by other applications).
    bindService(new Intent(Binding.this, LocalService.class), mConnection, Context.BIND_AUTO_CREATE);
    mIsBound = true;
}

There is a red line under "Binding.this" and eclipse gives me: Binding cannot be resolved to a type. What can I do?

Upvotes: 4

Views: 3405

Answers (2)

HitOdessit
HitOdessit

Reputation: 7256

Just remove Binding, code should look like this:

bindService(new Intent(this, LocalService.class), mConnection, Context.BIND_AUTO_CREATE);

First parameter of Intent constructor is Context. Code above will work if you calling it from your Activity class (any instance of Activity is a Context also). If not, you can always use application context

Upvotes: 5

Marcin Orlowski
Marcin Orlowski

Reputation: 75619

It looks like Binding is simply class name you put doBindService() in (most likely Activity?). If your class is named otherwise either rename it to Binding or replace all Binding.this with MyClass.this or just this.

Upvotes: 1

Related Questions