如何在IntentService :: handleIntent中转换作为intent extra传递的CustomResultReceiver

时间:2011-09-29 14:30:05

标签: android

我正在尝试将收到的Intent :: extraParcelable转换为我的自定义RestResultReceiver

public class APIHttpV1Service
    extends IntentService
{

    ...

    protected void onHandleIntent (Intent intent)
    {
        final int apiCall = intent.getIntExtra("uri");
        final RestResultReceiver resultReceiver = intent.getParcelableExtra(RestResultReceiver.EXTRA_ID_RESULT_RECEIVER);

    //  check if the uri is correct
        if( apiCall == -1)
        {
        //  TODO call failed as we didn't provide any URI
            resultReceiver.send(RestStatus.ERROR, Bundle.EMPTY);
        }

        ...

    }
}

这是RestResultReceiver

public class RestResultReceiver
    extends ResultReceiver
{
    private static final String TAG                     = "com...RestResultReceiver";
    public static final String EXTRA_ID_RESULT_RECEIVER = "com...RestResultRecevier.Receiver";

    private Receiver __mReceiver;

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

    public void clearReceiver()
    {
        __mReceiver = null;
    }

    /**
     * Set the receiver
     * @param receiver the object getting called back using onReceiveResult
     */
    public void setReceiver( Receiver receiver )
    {
        __mReceiver = receiver;
    }

    @Override
    protected void onReceiveResult(int resultCode, Bundle resultData)
    {
        if (__mReceiver != null)
        {
            __mReceiver.onReceiveResult(RestStatus.getEnum(resultCode), resultData);
        }
        else
        {
            Log.w(TAG, "Dropping result on floor for code " + resultCode + ": "
                    + resultData.toString());

        }
    }

    public void send(RestStatus resultStatus, Bundle resultData)
    {
        super.send(resultStatus.getCode(), resultData);
    }

    /**
     * Interface to implement for objects doing rest service call
     */
    public interface Receiver
    {
        public void onReceiveResult(RestStatus resultStatus, Bundle resultData);
    }
}

在运行时,我有一个java.lang.ClassCastException异常。我只是想知道为什么我可以得到我的RestResultReceiver。 如果我使用

final ResultReceiver resultReceiver = intent.getParcelableExtra(RestResultReceiver.EXTRA_ID_RESULT_RECEIVER);

它不会抛出任何错误,但我显然无法调用自定义发送功能。

有什么想法吗?

您可以提供的任何帮助都很多:)

0 个答案:

没有答案