【问题标题】:ClassCastException in ServiceConnection.onServiceConnected(). Bound ServicesServiceConnection.onServiceConnected() 中的 ClassCastException。绑定服务
【发布时间】:2012-05-16 12:46:10
【问题描述】:

我尝试将服务绑定到活动。但我在这里得到了 ClassCastException:

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

我真的不明白为什么我会得到这个异常。该服务作为进程运行。 Activity 想要获取并使用它。

这是两个类:

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");
}
}

【问题讨论】:

    标签: android binding service bind serviceconnection


    【解决方案1】:
    猜你喜欢
    • 2012-03-04
    • 1970-01-01
    • 1970-01-01
    • 2015-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多