3

我正在尝试绑定到自定义服务但没有成功。

我的定制服务

public class CustomService extends Service {
    private CustomReceiver mReceiver;
    private final CustomBinder mBinder = new CustomBinder();

    public class CustomBinder extends Binder implements ICustomService {
        // Implementation of the interface
    }

    public interface ICustomService {
        // Public interface to comunicate to the service
    }

@Override
public IBinder onBind(Intent intent) {
    Log.d("customservice", "Service bound.");
    mReceiver = intent.getParcelableExtra("receiver"); // ERROR HERE!
    return mBinder;
}

// Other methods

我的自定义接收器

public class CustomReceiver extends ResultReceiver {    
    public interface IReceiver {
        public void onReceiveResult(int resultCode, Bundle resultData);
    }

    private IReceiver mReceiver;

    public CustomReceiver(Handler handler) {
        super(handler);
    }

    public void setReceiver(IReceiver receiver) {
        mReceiver = receiver;
    }

    @Override
    protected void onReceiveResult(int resultCode, Bundle resultData) {
        if (mReceiver != null) {
            mReceiver.onReceiveResult(resultCode, resultData);
        }
    }
}

我的活动

public class MainActivity extends Activity implements CustomReceiver.IReceiver {
    IRegressiveService mService;
    CustomReceiver mReceiver;
    boolean mBound = false;

    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName name, IBinder service) {
            mService = (ICustomService) service;
            mBound = true;
        }

        public void onServiceDisconnected(ComponentName name) {
            mBound = false;
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        //...
        mReceiver = new CustomReceiver(new Handler());
        mReceiver.setReceiver(this);

        Intent bindServiceIntent = new Intent(this, CustomService.class);
        bindServiceIntent.putExtra("receiver", mReceiver);
        bindService(bindServiceIntent, mConnection, Context.BIND_AUTO_CREATE);
    }

    public void onReceiveResult(int resultCode, Bundle resultData) {
        //...
    }

    //...
}

堆栈跟踪(不完整)

04-29 20:23:55.851: E/AndroidRuntime(690): FATAL EXCEPTION: main
04-29 20:23:55.851: E/AndroidRuntime(690): java.lang.RuntimeException: Unable to bind to service [package].CustomService@44f38cd8 with Intent { cmp=[package].CustomService (has extras) }: java.lang.ClassCastException: android.os.ResultReceiver
04-29 20:23:55.851: E/AndroidRuntime(690): at android.app.ActivityThread.handleBindService(ActivityThread.java:2996)
[...]
04-29 20:23:55.851: E/AndroidRuntime(690): Caused by: java.lang.ClassCastException: android.os.ResultReceiver
04-29 20:23:55.851: E/AndroidRuntime(690): at [package].CustomService.onBind(CustomService.java:37)
04-29 20:23:55.851: E/AndroidRuntime(690): at android.app.ActivityThread.handleBindService(ActivityThread.java:2983)
[...]

简而言之

在活动中,我使用“接收者”键将CustomReceiver(子类)的实例传递给服务。ResultReceiver但是当我试图恢复这个对象时,在服务中,ClassCastException被提升了。为什么?我怎样才能解决这个问题?

我的目标是 API 级别 8。

而且我在任何其他 SO 问题中都找不到这种行为。

4

3 回答 3

2

我刚刚检查了我的代码。尝试mReceiver在您Service的基础上声明ResultReceiver如下...

public class CustomService extends Service {
    private ResultReceiver mReceiver;
    ...
}

这就是我所做的,当我从(我的也是一个自定义的)获取接收器时,ClassCastException我没有得到一个。ServiceIntentResultReceiver

于 2012-04-30T00:36:08.533 回答
1

我遇到了完全相同的问题,ClassCastException这对我来说没有任何意义。我在这里看到了关于将 设置mReceiver为仅使用 aResultReceiver而不是CustomReceiver在服务中的答案。我这样做了,但仍然遇到同样的错误,但我注意到我的importStatements 不一样。

在我的 MainActivity 我导入了语句:

import android.support.v4.os.ResultReceiver;

但是在我的服务中,我已经导入了声明:

import android.os.ResultReceiver;

我确保在我的服务和我的 MainActivity 中使用了以下内容:

import android.support.v4.os.ResultReceiver;

它现在可以正常工作了!希望这对某人有帮助!

我也晚了几年。。

于 2016-04-25T04:32:48.630 回答
-1

ABroadcastReceiver是(默认情况下)不是Parcelable。所以你会期望得到一个 ClassCastException。如果您需要传递特定实例,请向您的 Service 添加一个 setter 方法,然后使用该对象从您的 ServiceConnection 实现中调用该方法。

于 2012-04-30T00:18:36.330 回答