0

サービスを実装しようとしていますが、サービスにclassCastExceptionがあります。LocationはParcelableではないため、ParcelableArrayListでラップします。

public class GPSService extends Service {
    public void setLocation(Location location) {
        ArrayList<Location> locationArrayList = new ArrayList<Location>(1);
        locationArrayList.add(location);
        Bundle b = new Bundle();
        b.putParcelableArrayList("location", locationArrayList);
        // try with an arrayList instead of just getExtra doesn't change anything :(
        ArrayList<GPSResultReceiver> resultReceiverArrayList = intent.getParcelableArrayListExtra("receiver");
        // this is a resultReceiver and not a GPSResultReceiver
        // where did the upcast happen?
        GPSResultReceiver receiver = resultReceiverArrayList.get(0);
        Log.d("ResultClass", receiver.getClass().toString());
        receiver.onReceiveResult(1111, b);
    }

私のアクティビティは次のようになります。

public class MyActivity extends Activity implements GPSResultReceiver.Receiver {
    void startService() {
        resultReceiver = new GPSResultReceiver(new Handler());
        resultReceiver.setReceiver(this);
        final Intent intent = new Intent(Intent.ACTION_SEND, null, this, GPSService.class);
        // intent.putExtra("receiver", resultReceiver);
        ArrayList<GPSResultReceiver> resultReceiverArrayList = new ArrayList<GPSResultReceiver>(1);
        resultReceiverArrayList.add(resultReceiver);
        intent.putParcelableArrayListExtra("receiver", resultReceiverArrayList);
        serviceRunning = true;
        startService(intent);
    }

私のGPSResultReceiver:

public class GPSResultReceiver extends ResultReceiver {
    private Receiver resultReceiver;
    public GPSResultReceiver(Handler handler) {
        super(handler);
    }
    public void setReceiver(Receiver receiver) {
        resultReceiver = receiver;
    }
    public interface Receiver {
        public void onReceiveResult(int resultCode, Bundle resultData);
    }
    @Override
    public void onReceiveResult(int resultCode, Bundle resultData) {
        if (resultReceiver != null) {
            resultReceiver.onReceiveResult(resultCode, resultData);
        }
    }
}

では、なぜこのキャストがここで発生するのでしょうか。また、どうすれば回避できますか?

前もって感謝します

4

1 に答える 1

0

ResultReceiverのCREATORがResultReceiverを作成しますが、子クラスではないため、期待どおりに機能しているようです。ここでのポイントは次のとおりです。実際には、サービスにGPSResultReceiverは必要ありません。ResultReceiverだけで結果を送り返すことができますが、public void send(int resultCode、Bundle resultData)を呼び出す必要がありますが、呼び出す必要はありませんonReceiveResult

したがって、サービスのようになります。

public class GPSService extends Service {

    Intent intent = null;
    ResultReceiver receiver = null;

    @Override
    public IBinder onBind(final Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onStart(final Intent inIntent, final int startId) {

        super.onStart(inIntent, startId);

        this.intent = inIntent;
        setLocation(null);
    }

    void setLocation(final Location location) {
        final ArrayList<Location> locationArrayList = new ArrayList<Location>(1);
        locationArrayList.add(location);
        final Bundle b = new Bundle();
        b.putParcelableArrayList("location", locationArrayList);

        try {
            this.receiver = this.intent.getParcelableExtra("com.example.testservice.TestResultReceiver");
        } catch (final ClassCastException e) {
            Log.e("GPSService", "Failed to cast to  GPSResultReceiver", e);
        }

        try {
            this.receiver = GPSResultReceiver.class.cast(this.intent.getParcelableExtra("com.example.testservice.TestResultReceiver"));
        } catch (final ClassCastException e) {
            Log.e("GPSService", "Failed to cast to  GPSResultReceiver 2", e);
        }

        Log.d("ResultClass", this.receiver.getClass().toString());
        this.receiver.send(1111, b);
    }
}
于 2012-06-30T06:05:06.300 に答える