0
我嘗試將服務綁定到活動。但我在這裏得到ClassCastException:ServiceConnection.onServiceConnected()中的ClassCastException。綁定服務
pos.main.client.ClientBackground_Service.ClientBackground_Binder binder = (ClientBackground_Binder) service;
我不明白爲什麼我得到這個異常。該服務按流程運行。該活動想要獲取並使用它。
這裏有兩個類:
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;
}
};
}
服務類:
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");
}
}
服務作爲一個自己的過程是一個壞主意:) – zir