Frage

i try to bind a service to an activity. But i got the ClassCastException here:

pos.main.client.ClientBackground_Service.ClientBackground_Binder binder = (ClientBackground_Binder) service;

I don't really understand why i get this exception. The service is running as process. The activity want to fetch and use it.

Here are both classes:

public class ClientMain_Activity extends Activity
{
private boolean m_IsBound = false;


public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    if (isMyServiceRunning())
    {
        Intent intent = new Intent(this, ClientBackground_Service.class);
        bindService(intent, m_ServiceConnection, Context.BIND_AUTO_CREATE);

    }
}

private ServiceConnection m_ServiceConnection = new ServiceConnection()
{
    public void onServiceConnected(ComponentName name, IBinder service)
    {

        pos.main.client.ClientBackground_Service.ClientBackground_Binder binder = (ClientBackground_Binder) service;
        // m_Service = binder.getService();
        m_IsBound = true;
    }
};
}

Service Class:

public class ClientBackground_Service extends Service implements
    I_ClientBackground_Service
{

private final IBinder m_clientbackground_binding = new ClientBackground_Binder();


public class ClientBackground_Binder extends Binder
{

}

@Override
public IBinder onBind(Intent intent)
{
    return m_clientbackground_binding;
}

@Override
public void onStart(Intent intent, int startId)
{
    super.onStart(intent, startId);
    Log.i(this.getClass().getName(), " ClientBackground_Service started");
}
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top