Home | History | Annotate | Download | only in content
      1 /*
      2  * Copyright (C) 2006 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package android.content;
     18 
     19 import android.app.ActivityManagerNative;
     20 import android.app.ActivityThread;
     21 import android.app.IActivityManager;
     22 import android.app.QueuedWork;
     23 import android.os.Bundle;
     24 import android.os.IBinder;
     25 import android.os.RemoteException;
     26 import android.util.Log;
     27 import android.util.Slog;
     28 
     29 /**
     30  * Base class for code that will receive intents sent by sendBroadcast().
     31  *
     32  * <p>If you don't need to send broadcasts across applications, consider using
     33  * this class with {@link android.support.v4.content.LocalBroadcastManager} instead
     34  * of the more general facilities described below.  This will give you a much
     35  * more efficient implementation (no cross-process communication needed) and allow
     36  * you to avoid thinking about any security issues related to other applications
     37  * being able to receive or send your broadcasts.
     38  *
     39  * <p>You can either dynamically register an instance of this class with
     40  * {@link Context#registerReceiver Context.registerReceiver()}
     41  * or statically publish an implementation through the
     42  * {@link android.R.styleable#AndroidManifestReceiver &lt;receiver&gt;}
     43  * tag in your <code>AndroidManifest.xml</code>.
     44  *
     45  * <p><em><strong>Note:</strong></em>
     46  * &nbsp;&nbsp;&nbsp;If registering a receiver in your
     47  * {@link android.app.Activity#onResume() Activity.onResume()}
     48  * implementation, you should unregister it in
     49  * {@link android.app.Activity#onPause() Activity.onPause()}.
     50  * (You won't receive intents when paused,
     51  * and this will cut down on unnecessary system overhead). Do not unregister in
     52  * {@link android.app.Activity#onSaveInstanceState(android.os.Bundle) Activity.onSaveInstanceState()},
     53  * because this won't be called if the user moves back in the history
     54  * stack.
     55  *
     56  * <p>There are two major classes of broadcasts that can be received:</p>
     57  * <ul>
     58  * <li> <b>Normal broadcasts</b> (sent with {@link Context#sendBroadcast(Intent)
     59  * Context.sendBroadcast}) are completely asynchronous.  All receivers of the
     60  * broadcast are run in an undefined order, often at the same time.  This is
     61  * more efficient, but means that receivers cannot use the result or abort
     62  * APIs included here.
     63  * <li> <b>Ordered broadcasts</b> (sent with {@link Context#sendOrderedBroadcast(Intent, String)
     64  * Context.sendOrderedBroadcast}) are delivered to one receiver at a time.
     65  * As each receiver executes in turn, it can propagate a result to the next
     66  * receiver, or it can completely abort the broadcast so that it won't be passed
     67  * to other receivers.  The order receivers run in can be controlled with the
     68  * {@link android.R.styleable#AndroidManifestIntentFilter_priority
     69  * android:priority} attribute of the matching intent-filter; receivers with
     70  * the same priority will be run in an arbitrary order.
     71  * </ul>
     72  *
     73  * <p>Even in the case of normal broadcasts, the system may in some
     74  * situations revert to delivering the broadcast one receiver at a time.  In
     75  * particular, for receivers that may require the creation of a process, only
     76  * one will be run at a time to avoid overloading the system with new processes.
     77  * In this situation, however, the non-ordered semantics hold: these receivers still
     78  * cannot return results or abort their broadcast.</p>
     79  *
     80  * <p>Note that, although the Intent class is used for sending and receiving
     81  * these broadcasts, the Intent broadcast mechanism here is completely separate
     82  * from Intents that are used to start Activities with
     83  * {@link Context#startActivity Context.startActivity()}.
     84  * There is no way for a BroadcastReceiver
     85  * to see or capture Intents used with startActivity(); likewise, when
     86  * you broadcast an Intent, you will never find or start an Activity.
     87  * These two operations are semantically very different: starting an
     88  * Activity with an Intent is a foreground operation that modifies what the
     89  * user is currently interacting with; broadcasting an Intent is a background
     90  * operation that the user is not normally aware of.
     91  *
     92  * <p>The BroadcastReceiver class (when launched as a component through
     93  * a manifest's {@link android.R.styleable#AndroidManifestReceiver &lt;receiver&gt;}
     94  * tag) is an important part of an
     95  * <a href="{@docRoot}guide/topics/fundamentals.html#lcycles">application's overall lifecycle</a>.</p>
     96  *
     97  * <p>Topics covered here:
     98  * <ol>
     99  * <li><a href="#Security">Security</a>
    100  * <li><a href="#ReceiverLifecycle">Receiver Lifecycle</a>
    101  * <li><a href="#ProcessLifecycle">Process Lifecycle</a>
    102  * </ol>
    103  *
    104  * <div class="special reference">
    105  * <h3>Developer Guides</h3>
    106  * <p>For information about how to use this class to receive and resolve intents, read the
    107  * <a href="{@docRoot}guide/topics/intents/intents-filters.html">Intents and Intent Filters</a>
    108  * developer guide.</p>
    109  * </div>
    110  *
    111  * <a name="Security"></a>
    112  * <h3>Security</h3>
    113  *
    114  * <p>Receivers used with the {@link Context} APIs are by their nature a
    115  * cross-application facility, so you must consider how other applications
    116  * may be able to abuse your use of them.  Some things to consider are:
    117  *
    118  * <ul>
    119  * <li><p>The Intent namespace is global.  Make sure that Intent action names and
    120  * other strings are written in a namespace you own, or else you may inadvertantly
    121  * conflict with other applications.
    122  * <li><p>When you use {@link Context#registerReceiver(BroadcastReceiver, IntentFilter)},
    123  * <em>any</em> application may send broadcasts to that registered receiver.  You can
    124  * control who can send broadcasts to it through permissions described below.
    125  * <li><p>When you publish a receiver in your application's manifest and specify
    126  * intent-filters for it, any other application can send broadcasts to it regardless
    127  * of the filters you specify.  To prevent others from sending to it, make it
    128  * unavailable to them with <code>android:exported="false"</code>.
    129  * <li><p>When you use {@link Context#sendBroadcast(Intent)} or related methods,
    130  * normally any other application can receive these broadcasts.  You can control who
    131  * can receive such broadcasts through permissions described below.  Alternatively,
    132  * starting with {@link android.os.Build.VERSION_CODES#ICE_CREAM_SANDWICH}, you
    133  * can also safely restrict the broadcast to a single application with
    134  * {@link Intent#setPackage(String) Intent.setPackage}
    135  * </ul>
    136  *
    137  * <p>None of these issues exist when using
    138  * {@link android.support.v4.content.LocalBroadcastManager}, since intents
    139  * broadcast it never go outside of the current process.
    140  *
    141  * <p>Access permissions can be enforced by either the sender or receiver
    142  * of a broadcast.
    143  *
    144  * <p>To enforce a permission when sending, you supply a non-null
    145  * <var>permission</var> argument to
    146  * {@link Context#sendBroadcast(Intent, String)} or
    147  * {@link Context#sendOrderedBroadcast(Intent, String, BroadcastReceiver, android.os.Handler, int, String, Bundle)}.
    148  * Only receivers who have been granted this permission
    149  * (by requesting it with the
    150  * {@link android.R.styleable#AndroidManifestUsesPermission &lt;uses-permission&gt;}
    151  * tag in their <code>AndroidManifest.xml</code>) will be able to receive
    152  * the broadcast.
    153  *
    154  * <p>To enforce a permission when receiving, you supply a non-null
    155  * <var>permission</var> when registering your receiver -- either when calling
    156  * {@link Context#registerReceiver(BroadcastReceiver, IntentFilter, String, android.os.Handler)}
    157  * or in the static
    158  * {@link android.R.styleable#AndroidManifestReceiver &lt;receiver&gt;}
    159  * tag in your <code>AndroidManifest.xml</code>.  Only broadcasters who have
    160  * been granted this permission (by requesting it with the
    161  * {@link android.R.styleable#AndroidManifestUsesPermission &lt;uses-permission&gt;}
    162  * tag in their <code>AndroidManifest.xml</code>) will be able to send an
    163  * Intent to the receiver.
    164  *
    165  * <p>See the <a href="{@docRoot}guide/topics/security/security.html">Security and Permissions</a>
    166  * document for more information on permissions and security in general.
    167  *
    168  * <a name="ReceiverLifecycle"></a>
    169  * <h3>Receiver Lifecycle</h3>
    170  *
    171  * <p>A BroadcastReceiver object is only valid for the duration of the call
    172  * to {@link #onReceive}.  Once your code returns from this function,
    173  * the system considers the object to be finished and no longer active.
    174  *
    175  * <p>This has important repercussions to what you can do in an
    176  * {@link #onReceive} implementation: anything that requires asynchronous
    177  * operation is not available, because you will need to return from the
    178  * function to handle the asynchronous operation, but at that point the
    179  * BroadcastReceiver is no longer active and thus the system is free to kill
    180  * its process before the asynchronous operation completes.
    181  *
    182  * <p>In particular, you may <i>not</i> show a dialog or bind to a service from
    183  * within a BroadcastReceiver.  For the former, you should instead use the
    184  * {@link android.app.NotificationManager} API.  For the latter, you can
    185  * use {@link android.content.Context#startService Context.startService()} to
    186  * send a command to the service.
    187  *
    188  * <a name="ProcessLifecycle"></a>
    189  * <h3>Process Lifecycle</h3>
    190  *
    191  * <p>A process that is currently executing a BroadcastReceiver (that is,
    192  * currently running the code in its {@link #onReceive} method) is
    193  * considered to be a foreground process and will be kept running by the
    194  * system except under cases of extreme memory pressure.
    195  *
    196  * <p>Once you return from onReceive(), the BroadcastReceiver is no longer
    197  * active, and its hosting process is only as important as any other application
    198  * components that are running in it.  This is especially important because if
    199  * that process was only hosting the BroadcastReceiver (a common case for
    200  * applications that the user has never or not recently interacted with), then
    201  * upon returning from onReceive() the system will consider its process
    202  * to be empty and aggressively kill it so that resources are available for other
    203  * more important processes.
    204  *
    205  * <p>This means that for longer-running operations you will often use
    206  * a {@link android.app.Service} in conjunction with a BroadcastReceiver to keep
    207  * the containing process active for the entire time of your operation.
    208  */
    209 public abstract class BroadcastReceiver {
    210     private PendingResult mPendingResult;
    211     private boolean mDebugUnregister;
    212 
    213     /**
    214      * State for a result that is pending for a broadcast receiver.  Returned
    215      * by {@link BroadcastReceiver#goAsync() goAsync()}
    216      * while in {@link BroadcastReceiver#onReceive BroadcastReceiver.onReceive()}.
    217      * This allows you to return from onReceive() without having the broadcast
    218      * terminate; you must call {@link #finish()} once you are done with the
    219      * broadcast.  This allows you to process the broadcast off of the main
    220      * thread of your app.
    221      *
    222      * <p>Note on threading: the state inside of this class is not itself
    223      * thread-safe, however you can use it from any thread if you properly
    224      * sure that you do not have races.  Typically this means you will hand
    225      * the entire object to another thread, which will be solely responsible
    226      * for setting any results and finally calling {@link #finish()}.
    227      */
    228     public static class PendingResult {
    229         /** @hide */
    230         public static final int TYPE_COMPONENT = 0;
    231         /** @hide */
    232         public static final int TYPE_REGISTERED = 1;
    233         /** @hide */
    234         public static final int TYPE_UNREGISTERED = 2;
    235 
    236         final int mType;
    237         final boolean mOrderedHint;
    238         final boolean mInitialStickyHint;
    239         final IBinder mToken;
    240 
    241         int mResultCode;
    242         String mResultData;
    243         Bundle mResultExtras;
    244         boolean mAbortBroadcast;
    245         boolean mFinished;
    246 
    247         /** @hide */
    248         public PendingResult(int resultCode, String resultData, Bundle resultExtras,
    249                 int type, boolean ordered, boolean sticky, IBinder token) {
    250             mResultCode = resultCode;
    251             mResultData = resultData;
    252             mResultExtras = resultExtras;
    253             mType = type;
    254             mOrderedHint = ordered;
    255             mInitialStickyHint = sticky;
    256             mToken = token;
    257         }
    258 
    259         /**
    260          * Version of {@link BroadcastReceiver#setResultCode(int)
    261          * BroadcastReceiver.setResultCode(int)} for
    262          * asynchronous broadcast handling.
    263          */
    264         public final void setResultCode(int code) {
    265             checkSynchronousHint();
    266             mResultCode = code;
    267         }
    268 
    269         /**
    270          * Version of {@link BroadcastReceiver#getResultCode()
    271          * BroadcastReceiver.getResultCode()} for
    272          * asynchronous broadcast handling.
    273          */
    274         public final int getResultCode() {
    275             return mResultCode;
    276         }
    277 
    278         /**
    279          * Version of {@link BroadcastReceiver#setResultData(String)
    280          * BroadcastReceiver.setResultData(String)} for
    281          * asynchronous broadcast handling.
    282          */
    283         public final void setResultData(String data) {
    284             checkSynchronousHint();
    285             mResultData = data;
    286         }
    287 
    288         /**
    289          * Version of {@link BroadcastReceiver#getResultData()
    290          * BroadcastReceiver.getResultData()} for
    291          * asynchronous broadcast handling.
    292          */
    293         public final String getResultData() {
    294             return mResultData;
    295         }
    296 
    297         /**
    298          * Version of {@link BroadcastReceiver#setResultExtras(Bundle)
    299          * BroadcastReceiver.setResultExtras(Bundle)} for
    300          * asynchronous broadcast handling.
    301          */
    302         public final void setResultExtras(Bundle extras) {
    303             checkSynchronousHint();
    304             mResultExtras = extras;
    305         }
    306 
    307         /**
    308          * Version of {@link BroadcastReceiver#getResultExtras(boolean)
    309          * BroadcastReceiver.getResultExtras(boolean)} for
    310          * asynchronous broadcast handling.
    311          */
    312         public final Bundle getResultExtras(boolean makeMap) {
    313             Bundle e = mResultExtras;
    314             if (!makeMap) return e;
    315             if (e == null) mResultExtras = e = new Bundle();
    316             return e;
    317         }
    318 
    319         /**
    320          * Version of {@link BroadcastReceiver#setResult(int, String, Bundle)
    321          * BroadcastReceiver.setResult(int, String, Bundle)} for
    322          * asynchronous broadcast handling.
    323          */
    324         public final void setResult(int code, String data, Bundle extras) {
    325             checkSynchronousHint();
    326             mResultCode = code;
    327             mResultData = data;
    328             mResultExtras = extras;
    329         }
    330 
    331         /**
    332          * Version of {@link BroadcastReceiver#getAbortBroadcast()
    333          * BroadcastReceiver.getAbortBroadcast()} for
    334          * asynchronous broadcast handling.
    335          */
    336         public final boolean getAbortBroadcast() {
    337             return mAbortBroadcast;
    338         }
    339 
    340         /**
    341          * Version of {@link BroadcastReceiver#abortBroadcast()
    342          * BroadcastReceiver.abortBroadcast()} for
    343          * asynchronous broadcast handling.
    344          */
    345         public final void abortBroadcast() {
    346             checkSynchronousHint();
    347             mAbortBroadcast = true;
    348         }
    349 
    350         /**
    351          * Version of {@link BroadcastReceiver#clearAbortBroadcast()
    352          * BroadcastReceiver.clearAbortBroadcast()} for
    353          * asynchronous broadcast handling.
    354          */
    355         public final void clearAbortBroadcast() {
    356             mAbortBroadcast = false;
    357         }
    358 
    359         /**
    360          * Finish the broadcast.  The current result will be sent and the
    361          * next broadcast will proceed.
    362          */
    363         public final void finish() {
    364             if (mType == TYPE_COMPONENT) {
    365                 final IActivityManager mgr = ActivityManagerNative.getDefault();
    366                 if (QueuedWork.hasPendingWork()) {
    367                     // If this is a broadcast component, we need to make sure any
    368                     // queued work is complete before telling AM we are done, so
    369                     // we don't have our process killed before that.  We now know
    370                     // there is pending work; put another piece of work at the end
    371                     // of the list to finish the broadcast, so we don't block this
    372                     // thread (which may be the main thread) to have it finished.
    373                     //
    374                     // Note that we don't need to use QueuedWork.add() with the
    375                     // runnable, since we know the AM is waiting for us until the
    376                     // executor gets to it.
    377                     QueuedWork.singleThreadExecutor().execute( new Runnable() {
    378                         @Override public void run() {
    379                             if (ActivityThread.DEBUG_BROADCAST) Slog.i(ActivityThread.TAG,
    380                                     "Finishing broadcast after work to component " + mToken);
    381                             sendFinished(mgr);
    382                         }
    383                     });
    384                 } else {
    385                     if (ActivityThread.DEBUG_BROADCAST) Slog.i(ActivityThread.TAG,
    386                             "Finishing broadcast to component " + mToken);
    387                     sendFinished(mgr);
    388                 }
    389             } else if (mOrderedHint && mType != TYPE_UNREGISTERED) {
    390                 if (ActivityThread.DEBUG_BROADCAST) Slog.i(ActivityThread.TAG,
    391                         "Finishing broadcast to " + mToken);
    392                 final IActivityManager mgr = ActivityManagerNative.getDefault();
    393                 sendFinished(mgr);
    394             }
    395         }
    396 
    397         /** @hide */
    398         public void setExtrasClassLoader(ClassLoader cl) {
    399             if (mResultExtras != null) {
    400                 mResultExtras.setClassLoader(cl);
    401             }
    402         }
    403 
    404         /** @hide */
    405         public void sendFinished(IActivityManager am) {
    406             synchronized (this) {
    407                 if (mFinished) {
    408                     throw new IllegalStateException("Broadcast already finished");
    409                 }
    410                 mFinished = true;
    411 
    412                 try {
    413                     if (mResultExtras != null) {
    414                         mResultExtras.setAllowFds(false);
    415                     }
    416                     if (mOrderedHint) {
    417                         am.finishReceiver(mToken, mResultCode, mResultData, mResultExtras,
    418                                 mAbortBroadcast);
    419                     } else {
    420                         // This broadcast was sent to a component; it is not ordered,
    421                         // but we still need to tell the activity manager we are done.
    422                         am.finishReceiver(mToken, 0, null, null, false);
    423                     }
    424                 } catch (RemoteException ex) {
    425                 }
    426             }
    427         }
    428 
    429         void checkSynchronousHint() {
    430             // Note that we don't assert when receiving the initial sticky value,
    431             // since that may have come from an ordered broadcast.  We'll catch
    432             // them later when the real broadcast happens again.
    433             if (mOrderedHint || mInitialStickyHint) {
    434                 return;
    435             }
    436             RuntimeException e = new RuntimeException(
    437                     "BroadcastReceiver trying to return result during a non-ordered broadcast");
    438             e.fillInStackTrace();
    439             Log.e("BroadcastReceiver", e.getMessage(), e);
    440         }
    441     }
    442 
    443     public BroadcastReceiver() {
    444     }
    445 
    446     /**
    447      * This method is called when the BroadcastReceiver is receiving an Intent
    448      * broadcast.  During this time you can use the other methods on
    449      * BroadcastReceiver to view/modify the current result values.  The function
    450      * is normally called within the main thread of its process, so you should
    451      * never perform long-running operations in it (there is a timeout of
    452      * 10 seconds that the system allows before considering the receiver to
    453      * be blocked and a candidate to be killed). You cannot launch a popup dialog
    454      * in your implementation of onReceive().
    455      *
    456      * <p><b>If this BroadcastReceiver was launched through a &lt;receiver&gt; tag,
    457      * then the object is no longer alive after returning from this
    458      * function.</b>  This means you should not perform any operations that
    459      * return a result to you asynchronously -- in particular, for interacting
    460      * with services, you should use
    461      * {@link Context#startService(Intent)} instead of
    462      * {@link Context#bindService(Intent, ServiceConnection, int)}.  If you wish
    463      * to interact with a service that is already running, you can use
    464      * {@link #peekService}.
    465      *
    466      * <p>The Intent filters used in {@link android.content.Context#registerReceiver}
    467      * and in application manifests are <em>not</em> guaranteed to be exclusive. They
    468      * are hints to the operating system about how to find suitable recipients. It is
    469      * possible for senders to force delivery to specific recipients, bypassing filter
    470      * resolution.  For this reason, {@link #onReceive(Context, Intent) onReceive()}
    471      * implementations should respond only to known actions, ignoring any unexpected
    472      * Intents that they may receive.
    473      *
    474      * @param context The Context in which the receiver is running.
    475      * @param intent The Intent being received.
    476      */
    477     public abstract void onReceive(Context context, Intent intent);
    478 
    479     /**
    480      * This can be called by an application in {@link #onReceive} to allow
    481      * it to keep the broadcast active after returning from that function.
    482      * This does <em>not</em> change the expectation of being relatively
    483      * responsive to the broadcast (finishing it within 10s), but does allow
    484      * the implementation to move work related to it over to another thread
    485      * to avoid glitching the main UI thread due to disk IO.
    486      *
    487      * @return Returns a {@link PendingResult} representing the result of
    488      * the active broadcast.  The BroadcastRecord itself is no longer active;
    489      * all data and other interaction must go through {@link PendingResult}
    490      * APIs.  The {@link PendingResult#finish PendingResult.finish()} method
    491      * must be called once processing of the broadcast is done.
    492      */
    493     public final PendingResult goAsync() {
    494         PendingResult res = mPendingResult;
    495         mPendingResult = null;
    496         return res;
    497     }
    498 
    499     /**
    500      * Provide a binder to an already-running service.  This method is synchronous
    501      * and will not start the target service if it is not present, so it is safe
    502      * to call from {@link #onReceive}.
    503      *
    504      * @param myContext The Context that had been passed to {@link #onReceive(Context, Intent)}
    505      * @param service The Intent indicating the service you wish to use.  See {@link
    506      * Context#startService(Intent)} for more information.
    507      */
    508     public IBinder peekService(Context myContext, Intent service) {
    509         IActivityManager am = ActivityManagerNative.getDefault();
    510         IBinder binder = null;
    511         try {
    512             service.setAllowFds(false);
    513             binder = am.peekService(service, service.resolveTypeIfNeeded(
    514                     myContext.getContentResolver()));
    515         } catch (RemoteException e) {
    516         }
    517         return binder;
    518     }
    519 
    520     /**
    521      * Change the current result code of this broadcast; only works with
    522      * broadcasts sent through
    523      * {@link Context#sendOrderedBroadcast(Intent, String)
    524      * Context.sendOrderedBroadcast}.  Often uses the
    525      * Activity {@link android.app.Activity#RESULT_CANCELED} and
    526      * {@link android.app.Activity#RESULT_OK} constants, though the
    527      * actual meaning of this value is ultimately up to the broadcaster.
    528      *
    529      * <p class="note">This method does not work with non-ordered broadcasts such
    530      * as those sent with {@link Context#sendBroadcast(Intent)
    531      * Context.sendBroadcast}</p>
    532      *
    533      * @param code The new result code.
    534      *
    535      * @see #setResult(int, String, Bundle)
    536      */
    537     public final void setResultCode(int code) {
    538         checkSynchronousHint();
    539         mPendingResult.mResultCode = code;
    540     }
    541 
    542     /**
    543      * Retrieve the current result code, as set by the previous receiver.
    544      *
    545      * @return int The current result code.
    546      */
    547     public final int getResultCode() {
    548         return mPendingResult != null ? mPendingResult.mResultCode : 0;
    549     }
    550 
    551     /**
    552      * Change the current result data of this broadcast; only works with
    553      * broadcasts sent through
    554      * {@link Context#sendOrderedBroadcast(Intent, String)
    555      * Context.sendOrderedBroadcast}.  This is an arbitrary
    556      * string whose interpretation is up to the broadcaster.
    557      *
    558      * <p><strong>This method does not work with non-ordered broadcasts such
    559      * as those sent with {@link Context#sendBroadcast(Intent)
    560      * Context.sendBroadcast}</strong></p>
    561      *
    562      * @param data The new result data; may be null.
    563      *
    564      * @see #setResult(int, String, Bundle)
    565      */
    566     public final void setResultData(String data) {
    567         checkSynchronousHint();
    568         mPendingResult.mResultData = data;
    569     }
    570 
    571     /**
    572      * Retrieve the current result data, as set by the previous receiver.
    573      * Often this is null.
    574      *
    575      * @return String The current result data; may be null.
    576      */
    577     public final String getResultData() {
    578         return mPendingResult != null ? mPendingResult.mResultData : null;
    579     }
    580 
    581     /**
    582      * Change the current result extras of this broadcast; only works with
    583      * broadcasts sent through
    584      * {@link Context#sendOrderedBroadcast(Intent, String)
    585      * Context.sendOrderedBroadcast}.  This is a Bundle
    586      * holding arbitrary data, whose interpretation is up to the
    587      * broadcaster.  Can be set to null.  Calling this method completely
    588      * replaces the current map (if any).
    589      *
    590      * <p><strong>This method does not work with non-ordered broadcasts such
    591      * as those sent with {@link Context#sendBroadcast(Intent)
    592      * Context.sendBroadcast}</strong></p>
    593      *
    594      * @param extras The new extra data map; may be null.
    595      *
    596      * @see #setResult(int, String, Bundle)
    597      */
    598     public final void setResultExtras(Bundle extras) {
    599         checkSynchronousHint();
    600         mPendingResult.mResultExtras = extras;
    601     }
    602 
    603     /**
    604      * Retrieve the current result extra data, as set by the previous receiver.
    605      * Any changes you make to the returned Map will be propagated to the next
    606      * receiver.
    607      *
    608      * @param makeMap If true then a new empty Map will be made for you if the
    609      *                current Map is null; if false you should be prepared to
    610      *                receive a null Map.
    611      *
    612      * @return Map The current extras map.
    613      */
    614     public final Bundle getResultExtras(boolean makeMap) {
    615         if (mPendingResult == null) {
    616             return null;
    617         }
    618         Bundle e = mPendingResult.mResultExtras;
    619         if (!makeMap) return e;
    620         if (e == null) mPendingResult.mResultExtras = e = new Bundle();
    621         return e;
    622     }
    623 
    624     /**
    625      * Change all of the result data returned from this broadcasts; only works
    626      * with broadcasts sent through
    627      * {@link Context#sendOrderedBroadcast(Intent, String)
    628      * Context.sendOrderedBroadcast}.  All current result data is replaced
    629      * by the value given to this method.
    630      *
    631      * <p><strong>This method does not work with non-ordered broadcasts such
    632      * as those sent with {@link Context#sendBroadcast(Intent)
    633      * Context.sendBroadcast}</strong></p>
    634      *
    635      * @param code The new result code.  Often uses the
    636      * Activity {@link android.app.Activity#RESULT_CANCELED} and
    637      * {@link android.app.Activity#RESULT_OK} constants, though the
    638      * actual meaning of this value is ultimately up to the broadcaster.
    639      * @param data The new result data.  This is an arbitrary
    640      * string whose interpretation is up to the broadcaster; may be null.
    641      * @param extras The new extra data map.  This is a Bundle
    642      * holding arbitrary data, whose interpretation is up to the
    643      * broadcaster.  Can be set to null.  This completely
    644      * replaces the current map (if any).
    645      */
    646     public final void setResult(int code, String data, Bundle extras) {
    647         checkSynchronousHint();
    648         mPendingResult.mResultCode = code;
    649         mPendingResult.mResultData = data;
    650         mPendingResult.mResultExtras = extras;
    651     }
    652 
    653     /**
    654      * Returns the flag indicating whether or not this receiver should
    655      * abort the current broadcast.
    656      *
    657      * @return True if the broadcast should be aborted.
    658      */
    659     public final boolean getAbortBroadcast() {
    660         return mPendingResult != null ? mPendingResult.mAbortBroadcast : false;
    661     }
    662 
    663     /**
    664      * Sets the flag indicating that this receiver should abort the
    665      * current broadcast; only works with broadcasts sent through
    666      * {@link Context#sendOrderedBroadcast(Intent, String)
    667      * Context.sendOrderedBroadcast}.  This will prevent
    668      * any other broadcast receivers from receiving the broadcast. It will still
    669      * call {@link #onReceive} of the BroadcastReceiver that the caller of
    670      * {@link Context#sendOrderedBroadcast(Intent, String)
    671      * Context.sendOrderedBroadcast} passed in.
    672      *
    673      * <p><strong>This method does not work with non-ordered broadcasts such
    674      * as those sent with {@link Context#sendBroadcast(Intent)
    675      * Context.sendBroadcast}</strong></p>
    676      */
    677     public final void abortBroadcast() {
    678         checkSynchronousHint();
    679         mPendingResult.mAbortBroadcast = true;
    680     }
    681 
    682     /**
    683      * Clears the flag indicating that this receiver should abort the current
    684      * broadcast.
    685      */
    686     public final void clearAbortBroadcast() {
    687         if (mPendingResult != null) {
    688             mPendingResult.mAbortBroadcast = false;
    689         }
    690     }
    691 
    692     /**
    693      * Returns true if the receiver is currently processing an ordered
    694      * broadcast.
    695      */
    696     public final boolean isOrderedBroadcast() {
    697         return mPendingResult != null ? mPendingResult.mOrderedHint : false;
    698     }
    699 
    700     /**
    701      * Returns true if the receiver is currently processing the initial
    702      * value of a sticky broadcast -- that is, the value that was last
    703      * broadcast and is currently held in the sticky cache, so this is
    704      * not directly the result of a broadcast right now.
    705      */
    706     public final boolean isInitialStickyBroadcast() {
    707         return mPendingResult != null ? mPendingResult.mInitialStickyHint : false;
    708     }
    709 
    710     /**
    711      * For internal use, sets the hint about whether this BroadcastReceiver is
    712      * running in ordered mode.
    713      */
    714     public final void setOrderedHint(boolean isOrdered) {
    715         // Accidentally left in the SDK.
    716     }
    717 
    718     /**
    719      * For internal use to set the result data that is active. @hide
    720      */
    721     public final void setPendingResult(PendingResult result) {
    722         mPendingResult = result;
    723     }
    724 
    725     /**
    726      * For internal use to set the result data that is active. @hide
    727      */
    728     public final PendingResult getPendingResult() {
    729         return mPendingResult;
    730     }
    731 
    732     /**
    733      * Control inclusion of debugging help for mismatched
    734      * calls to {@ Context#registerReceiver(BroadcastReceiver, IntentFilter)
    735      * Context.registerReceiver()}.
    736      * If called with true, before given to registerReceiver(), then the
    737      * callstack of the following {@link Context#unregisterReceiver(BroadcastReceiver)
    738      * Context.unregisterReceiver()} call is retained, to be printed if a later
    739      * incorrect unregister call is made.  Note that doing this requires retaining
    740      * information about the BroadcastReceiver for the lifetime of the app,
    741      * resulting in a leak -- this should only be used for debugging.
    742      */
    743     public final void setDebugUnregister(boolean debug) {
    744         mDebugUnregister = debug;
    745     }
    746 
    747     /**
    748      * Return the last value given to {@link #setDebugUnregister}.
    749      */
    750     public final boolean getDebugUnregister() {
    751         return mDebugUnregister;
    752     }
    753 
    754     void checkSynchronousHint() {
    755         if (mPendingResult == null) {
    756             throw new IllegalStateException("Call while result is not pending");
    757         }
    758 
    759         // Note that we don't assert when receiving the initial sticky value,
    760         // since that may have come from an ordered broadcast.  We'll catch
    761         // them later when the real broadcast happens again.
    762         if (mPendingResult.mOrderedHint || mPendingResult.mInitialStickyHint) {
    763             return;
    764         }
    765         RuntimeException e = new RuntimeException(
    766                 "BroadcastReceiver trying to return result during a non-ordered broadcast");
    767         e.fillInStackTrace();
    768         Log.e("BroadcastReceiver", e.getMessage(), e);
    769     }
    770 }
    771 
    772