ClassCastException while using service
Posted
by Sebi
on Stack Overflow
See other posts from Stack Overflow
or by Sebi
Published on 2010-05-02T20:27:41Z
Indexed on
2010/05/02
20:38 UTC
Read the original article
Hit count: 378
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!
© Stack Overflow or respective owner