FileDocCategorySizeDatePackage
ResultReceiver.javaAPI DocAndroid 5.1 API4633Thu Mar 12 22:22:10 GMT 2015android.os

ResultReceiver

public class ResultReceiver extends Object implements Parcelable
Generic interface for receiving a callback result from someone. Use this by creating a subclass and implement {@link #onReceiveResult}, which you can then pass to others and send through IPC, and receive results they supply with {@link #send}.

Note: the implementation underneath is just a simple wrapper around a {@link Binder} that is used to perform the communication. This means semantically you should treat it as such: this class does not impact process lifecycle management (you must be using some higher-level component to tell the system that your process needs to continue running), the connection will break if your process goes away for any reason, etc.

Fields Summary
final boolean
mLocal
final Handler
mHandler
com.android.internal.os.IResultReceiver
mReceiver
public static final Parcelable.Creator
CREATOR
Constructors Summary
public ResultReceiver(Handler handler)
Create a new ResultReceive to receive results. Your {@link #onReceiveResult} method will be called from the thread running handler if given, or from an arbitrary thread if null.

        mLocal = true;
        mHandler = handler;
    
ResultReceiver(Parcel in)

        mLocal = false;
        mHandler = null;
        mReceiver = IResultReceiver.Stub.asInterface(in.readStrongBinder());
    
Methods Summary
public intdescribeContents()

        return 0;
    
protected voidonReceiveResult(int resultCode, Bundle resultData)
Override to receive results delivered to this object.

param
resultCode Arbitrary result code delivered by the sender, as defined by the sender.
param
resultData Any additional data provided by the sender.

    
public voidsend(int resultCode, Bundle resultData)
Deliver a result to this receiver. Will call {@link #onReceiveResult}, always asynchronously if the receiver has supplied a Handler in which to dispatch the result.

param
resultCode Arbitrary result code to deliver, as defined by you.
param
resultData Any additional data provided by you.

        if (mLocal) {
            if (mHandler != null) {
                mHandler.post(new MyRunnable(resultCode, resultData));
            } else {
                onReceiveResult(resultCode, resultData);
            }
            return;
        }
        
        if (mReceiver != null) {
            try {
                mReceiver.send(resultCode, resultData);
            } catch (RemoteException e) {
            }
        }
    
public voidwriteToParcel(Parcel out, int flags)

        synchronized (this) {
            if (mReceiver == null) {
                mReceiver = new MyResultReceiver();
            }
            out.writeStrongBinder(mReceiver.asBinder());
        }