开发者

Binding to a service which implements an interface

开发者 https://www.devze.com 2023-03-22 22:35 出处:网络
EDIT: The code below has been edited to show the correct solution to the problem. I have an app which uses a foreground service to perform network operations.

EDIT: The code below has been edited to show the correct solution to the problem.

I have an app which uses a foreground service to perform network operations.

Currently, the foreground service uses a bluetooth connection to perform the operations. I'm trying to implement a new version of the service which uses wifi instead, and allow the user to decide whether to use bluetooth or wifi through shared preferences.

I've implemented the wifi service, and now I need to bind to it. I created an interface, MyService, which defines all of the methods that both versions of the service require. However, when I try to bind to the service in my activity, I get a ClassCastException error.

Here are the relevant parts of my service interface:

MyService.java:

public interface MyService {
// constants

...

// method declarations
...

public interface LocalBinder {
        MyService getService(Handler handler);
    }
}

And here are the relevant methods which are present in both versions of the service:

MyBluetoothService.java:

public class MyBluetoothService extends Service implements MyService {

private final IBinder mBinder = new LocalBinder();

...

public class LocalBinder extends Binder implements MyService.LocalBinder {
    MyService getService(Handler handler) {
        mHandler = handler;

        // Return this instance of MyService so clients can call public methods
        return MyBluetoothService.this;
    }
}

@Override
public IBinder onBind(Intent intent) {
    Log.w(TAG, "MyBluetoothService bound");

    return mBinder;
}
}

MyWifiService.java: Exactly the same as MyBluetoothService.java except with class names changed as necessary.

And here is where I bind to the service in my activity:

MyService mChatService = null;
...

private ServiceConnection mConnection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName className,
            IBinder service) {
        // We've bound to MyService, cast the IBinder and get MyService instance
        LocalBinder binder = (LocalBinder)service;  <------- ClassCastException
        mChatService = binder.getService(mHandler);
        mBound = true;
    }

    @Override
    public void onServiceDisconnected(ComponentName argo) {
        mBound = false;         
    }
};

The ClassCastException occurs on the line indicated above.

Now that all of开发者_Go百科 that is out of the way... is it possible to bind to a service in this way? Alernatively, I could always check shared preferences every time I call a method from the service but I'd rather not.


I'm assuming that the code where it is throwing is a MyService.LocalBinder class and not a MyBluetoothService.LocalBinder class?

What I think you meant to do is define the MyBluetoothService.LocalBinder class to extend from the MyService.LocalBinder class?

e.g.

public class MyBluetoothService extends Service implements MyService {

private final IBinder mBinder = new LocalBinder();

...

public class LocalBinder extends MyService.LocalBinder {
    MyService getService(Handler handler) {
        mHandler = handler;

        // Return this instance of MyService so clients can call public methods
        return MyBluetoothService.this;
    }
}

@Override
public IBinder onBind(Intent intent) {
    Log.w(TAG, "MyBluetoothService bound");

    return mBinder;
}
}
0

精彩评论

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

关注公众号