ClassCastException while using service
- by Sebi
I defined a local Service:
public class ComService extends Service implements IComService {
private IBinder binder = new ComServiceBinder();
public class ComServiceBinder extends Binder implements IComService.IServiceBinder {
public IComService getService() {
return ComService.this;
}
}
public void test(String msg) {
System.out.println(msg);
}
@Override
public IBinder onBind(Intent intent) {
return binder;
}
}
The corresponding interface:
public interface IComService {
public void test(String msg);
public interface IServiceBinder {
IComService getService();
}
}
Then i try to bind the service in another activity in another application, where the same interface is available:
bindService(new Intent("ch.ifi.csg.games4blue.gamebase.api.ComService"), conn, Context.BIND_AUTO_CREATE);
and
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i("INFO", "Service bound " + name);
comService = ((IComService.IServiceBinder)service).getService();
serviceHandler.sendEmptyMessage(0);
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
Log.i("INFO", "Service Unbound ");
}
};
but the line
comService = ((IComService.IServiceBinder)service).getService();
always throws a
05-02 22:12:55.922: ERROR/AndroidRuntime(622): java.lang.ClassCastException: android.os.BinderProxy
I can't explain why, I followed the app sample on http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/LocalServiceBinding.html
Any hints would be nice!