我定义了一个本地服务:
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;
}
}
相应的界面:
public interface IComService {
public void test(String msg);
public interface IServiceBinder {
IComService getService();
}
}
然后我尝试将服务绑定到另一个应用程序中的另一个活动中,其中可用的界面相同:
bindService(new Intent("ch.ifi.csg.games4blue.gamebase.api.ComService"), conn, Context.BIND_AUTO_CREATE);
和
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 ");
}
};
但行
comService = ((IComService.IServiceBinder)service).getService();
总是抛出一个
05-02 22:12:55.922: ERROR/AndroidRuntime(622): java.lang.ClassCastException: android.os.BinderProxy
上关注了应用示例
任何提示都会很好!