开发者

Android service client code

Greetings I am creating a service client and the following lines give me error. I was wondering how to fix these...

//  I have put in the service client stuff below...

private  CheckZone mBoundService;
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName CheckZone, IBinder service) {
    // This is called when the connection with the service has been
    // established, giving us the service object we can use to
    // interact with the service.  Because we have bound to a explicit
    // service that we know is running in our own process, we can
    // cast its IBinder to a concrete class and directly access it.
    mBoundService = ((CheckZone.LocalBinder)service).getService();

    //This line complians about binding, and states that it can not be resolved.  How do I fix this please?
    Toast.makeText(binding.this, "Connected to CheckZone", Toast.LE开发者_如何学PythonNGTH_SHORT).show();
}


I just ran into this issue using the same sample Local Service Sample code from http://developer.android.com/reference/android/app/Service.html, Toast.makeText wasn't happy with just this. You need to use classname.this

e.g.

public class MyClass extends Activity {
  private ServiceConnection mConnection = new ServiceConnection() {

    public void myMethod () {
                Toast.makeText(MyClass.this,
                R.string.local_service_connected,
                Toast.LENGTH_SHORT).show();
    }
  }
}

Without MyClass. you get the error:

The method makeText(Context, int, int) in the type Toast is not applicable for the arguments (new ServiceConnection(){}, int, int)

The reason is that you're actually in an inner class.

See: Using "this" with class name

And, of course, the classname left off of the sample code is Binder so if your class was called that, it would just work.


I think you are referring to a compilation error? Binding is not defined? Just get rid of binding.this and just use, "this"


Do you have a LocalBinder in your CheckZone service?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜