我尝试将服务绑定到活动。但我在这里得到了 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");
}
}