FileDocCategorySizeDatePackage
PendingIntent.javaAPI DocAndroid 1.5 API21165Wed May 06 22:41:54 BST 2009android.app

PendingIntent

public final class PendingIntent extends Object implements android.os.Parcelable
A description of an Intent and target action to perform with it. Instances of this class are created with {@link #getActivity}, {@link #getBroadcast}, {@link #getService}; the returned object can be handed to other applications so that they can perform the action you described on your behalf at a later time.

By giving a PendingIntent to another application, you are granting it the right to perform the operation you have specified as if the other application was yourself (with the same permissions and identity). As such, you should be careful about how you build the PendingIntent: often, for example, the base Intent you supply will have the component name explicitly set to one of your own components, to ensure it is ultimately sent there and nowhere else.

A PendingIntent itself is simply a reference to a token maintained by the system describing the original data used to retrieve it. This means that, even if its owning application's process is killed, the PendingIntent itself will remain usable from other processes that have been given it. If the creating application later re-retrieves the same kind of PendingIntent (same operation, same Intent action, data, categories, and components, and same flags), it will receive a PendingIntent representing the same token if that is still valid, and can thus call {@link #cancel} to remove it.

Fields Summary
private final IIntentSender
mTarget
public static final int
FLAG_ONE_SHOT
Flag for use with {@link #getActivity}, {@link #getBroadcast}, and {@link #getService}: this PendingIntent can only be used once. If set, after {@link #send()} is called on it, it will be automatically canceled for you and any future attempt to send through it will fail.
public static final int
FLAG_NO_CREATE
Flag for use with {@link #getActivity}, {@link #getBroadcast}, and {@link #getService}: if the described PendingIntent does not already exist, then simply return null instead of creating it.
public static final int
FLAG_CANCEL_CURRENT
Flag for use with {@link #getActivity}, {@link #getBroadcast}, and {@link #getService}: if the described PendingIntent already exists, the current one is canceled before generating a new one. You can use this to retrieve a new PendingIntent when you are only changing the extra data in the Intent; by canceling the previous pending intent, this ensures that only entities given the new data will be able to launch it. If this assurance is not an issue, consider {@link #FLAG_UPDATE_CURRENT}.
public static final int
FLAG_UPDATE_CURRENT
Flag for use with {@link #getActivity}, {@link #getBroadcast}, and {@link #getService}: if the described PendingIntent already exists, then keep it but its replace its extra data with what is in this new Intent. This can be used if you are creating intents where only the extras change, and don't care that any entities that received your previous PendingIntent will be able to launch it with your new extras even if they are not explicitly given to it.
public static final Parcelable.Creator
CREATOR
Constructors Summary
PendingIntent(IIntentSender target)

        mTarget = target;
    
PendingIntent(android.os.IBinder target)

        mTarget = IIntentSender.Stub.asInterface(target);
    
Methods Summary
public voidcancel()
Cancel a currently active PendingIntent. Only the original application owning an PendingIntent can cancel it.

        try {
            ActivityManagerNative.getDefault().cancelIntentSender(mTarget);
        } catch (RemoteException e) {
        }
    
public intdescribeContents()

        return 0;
    
public booleanequals(java.lang.Object otherObj)
Comparison operator on two PendingIntent objects, such that true is returned then they both represent the same operation from the same package. This allows you to use {@link #getActivity}, {@link #getBroadcast}, or {@link #getService} multiple times (even across a process being killed), resulting in different PendingIntent objects but whose equals() method identifies them as being the same operation.

        if (otherObj instanceof PendingIntent) {
            return mTarget.asBinder().equals(((PendingIntent)otherObj)
                    .mTarget.asBinder());
        }
        return false;
    
public static android.app.PendingIntentgetActivity(android.content.Context context, int requestCode, android.content.Intent intent, int flags)
Retrieve a PendingIntent that will start a new activity, like calling {@link Context#startActivity(Intent) Context.startActivity(Intent)}. Note that the activity will be started outside of the context of an existing activity, so you must use the {@link Intent#FLAG_ACTIVITY_NEW_TASK Intent.FLAG_ACTIVITY_NEW_TASK} launch flag in the Intent.

param
context The Context in which this PendingIntent should start the activity.
param
requestCode Private request code for the sender (currently not used).
param
intent Intent of the activity to be launched.
param
flags May be {@link #FLAG_ONE_SHOT}, {@link #FLAG_NO_CREATE}, {@link #FLAG_CANCEL_CURRENT}, {@link #FLAG_UPDATE_CURRENT}, or any of the flags as supported by {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts of the intent that can be supplied when the actual send happens.
return
Returns an existing or new PendingIntent matching the given parameters. May return null only if {@link #FLAG_NO_CREATE} has been supplied.

        String packageName = context.getPackageName();
        String resolvedType = intent != null ? intent.resolveTypeIfNeeded(
                context.getContentResolver()) : null;
        try {
            IIntentSender target =
                ActivityManagerNative.getDefault().getIntentSender(
                    IActivityManager.INTENT_SENDER_ACTIVITY, packageName,
                    null, null, requestCode, intent, resolvedType, flags);
            return target != null ? new PendingIntent(target) : null;
        } catch (RemoteException e) {
        }
        return null;
    
public static android.app.PendingIntentgetBroadcast(android.content.Context context, int requestCode, android.content.Intent intent, int flags)
Retrieve a PendingIntent that will perform a broadcast, like calling {@link Context#sendBroadcast(Intent) Context.sendBroadcast()}.

param
context The Context in which this PendingIntent should perform the broadcast.
param
requestCode Private request code for the sender (currently not used).
param
intent The Intent to be broadcast.
param
flags May be {@link #FLAG_ONE_SHOT}, {@link #FLAG_NO_CREATE}, {@link #FLAG_CANCEL_CURRENT}, {@link #FLAG_UPDATE_CURRENT}, or any of the flags as supported by {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts of the intent that can be supplied when the actual send happens.
return
Returns an existing or new PendingIntent matching the given parameters. May return null only if {@link #FLAG_NO_CREATE} has been supplied.

        String packageName = context.getPackageName();
        String resolvedType = intent != null ? intent.resolveTypeIfNeeded(
                context.getContentResolver()) : null;
        try {
            IIntentSender target =
                ActivityManagerNative.getDefault().getIntentSender(
                    IActivityManager.INTENT_SENDER_BROADCAST, packageName,
                    null, null, requestCode, intent, resolvedType, flags);
            return target != null ? new PendingIntent(target) : null;
        } catch (RemoteException e) {
        }
        return null;
    
public static android.app.PendingIntentgetService(android.content.Context context, int requestCode, android.content.Intent intent, int flags)
Retrieve a PendingIntent that will start a service, like calling {@link Context#startService Context.startService()}. The start arguments given to the service will come from the extras of the Intent.

param
context The Context in which this PendingIntent should start the service.
param
requestCode Private request code for the sender (currently not used).
param
intent An Intent describing the service to be started.
param
flags May be {@link #FLAG_ONE_SHOT}, {@link #FLAG_NO_CREATE}, {@link #FLAG_CANCEL_CURRENT}, {@link #FLAG_UPDATE_CURRENT}, or any of the flags as supported by {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts of the intent that can be supplied when the actual send happens.
return
Returns an existing or new PendingIntent matching the given parameters. May return null only if {@link #FLAG_NO_CREATE} has been supplied.

        String packageName = context.getPackageName();
        String resolvedType = intent != null ? intent.resolveTypeIfNeeded(
                context.getContentResolver()) : null;
        try {
            IIntentSender target =
                ActivityManagerNative.getDefault().getIntentSender(
                    IActivityManager.INTENT_SENDER_SERVICE, packageName,
                    null, null, requestCode, intent, resolvedType, flags);
            return target != null ? new PendingIntent(target) : null;
        } catch (RemoteException e) {
        }
        return null;
    
IIntentSendergetTarget()

        return mTarget;
    
public java.lang.StringgetTargetPackage()
Return the package name of the application that created this PendingIntent, that is the identity under which you will actually be sending the Intent. The returned string is supplied by the system, so that an application can not spoof its package.

return
The package name of the PendingIntent, or null if there is none associated with it.

        try {
            return ActivityManagerNative.getDefault()
                .getPackageForIntentSender(mTarget);
        } catch (RemoteException e) {
            // Should never happen.
            return null;
        }
    
public inthashCode()

        return mTarget.asBinder().hashCode();
    
public static android.app.PendingIntentreadPendingIntentOrNullFromParcel(android.os.Parcel in)
Convenience function for reading either a Messenger or null pointer from a Parcel. You must have previously written the Messenger with {@link #writePendingIntentOrNullToParcel}.

param
in The Parcel containing the written Messenger.
return
Returns the Messenger read from the Parcel, or null if null had been written.

        IBinder b = in.readStrongBinder();
        return b != null ? new PendingIntent(b) : null;
    
public voidsend()
Perform the operation associated with this PendingIntent.

see
#send(Context, int, Intent, android.app.PendingIntent.OnFinished, Handler)
throws
CanceledException Throws CanceledException if the PendingIntent is no longer allowing more intents to be sent through it.

        send(null, 0, null, null, null);
    
public voidsend(int code)
Perform the operation associated with this PendingIntent.

param
code Result code to supply back to the PendingIntent's target.
see
#send(Context, int, Intent, android.app.PendingIntent.OnFinished, Handler)
throws
CanceledException Throws CanceledException if the PendingIntent is no longer allowing more intents to be sent through it.

        send(null, code, null, null, null);
    
public voidsend(android.content.Context context, int code, android.content.Intent intent)
Perform the operation associated with this PendingIntent, allowing the caller to specify information about the Intent to use.

param
context The Context of the caller.
param
code Result code to supply back to the PendingIntent's target.
param
intent Additional Intent data. See {@link Intent#fillIn Intent.fillIn()} for information on how this is applied to the original Intent.
see
#send(Context, int, Intent, android.app.PendingIntent.OnFinished, Handler)
throws
CanceledException Throws CanceledException if the PendingIntent is no longer allowing more intents to be sent through it.

        send(context, code, intent, null, null);
    
public voidsend(int code, android.app.PendingIntent$OnFinished onFinished, android.os.Handler handler)
Perform the operation associated with this PendingIntent, allowing the caller to be notified when the send has completed.

param
code Result code to supply back to the PendingIntent's target.
param
onFinished The object to call back on when the send has completed, or null for no callback.
param
handler Handler identifying the thread on which the callback should happen. If null, the callback will happen from the thread pool of the process.
see
#send(Context, int, Intent, android.app.PendingIntent.OnFinished, Handler)
throws
CanceledException Throws CanceledException if the PendingIntent is no longer allowing more intents to be sent through it.

        send(null, code, null, onFinished, handler);
    
public voidsend(android.content.Context context, int code, android.content.Intent intent, android.app.PendingIntent$OnFinished onFinished, android.os.Handler handler)
Perform the operation associated with this PendingIntent, allowing the caller to specify information about the Intent to use and be notified when the send has completed.

For the intent parameter, a PendingIntent often has restrictions on which fields can be supplied here, based on how the PendingIntent was retrieved in {@link #getActivity}, {@link #getBroadcast}, or {@link #getService}.

param
context The Context of the caller. This may be null if intent is also null.
param
code Result code to supply back to the PendingIntent's target.
param
intent Additional Intent data. See {@link Intent#fillIn Intent.fillIn()} for information on how this is applied to the original Intent. Use null to not modify the original Intent.
param
onFinished The object to call back on when the send has completed, or null for no callback.
param
handler Handler identifying the thread on which the callback should happen. If null, the callback will happen from the thread pool of the process.
see
#send()
see
#send(int)
see
#send(Context, int, Intent)
see
#send(int, android.app.PendingIntent.OnFinished, Handler)
throws
CanceledException Throws CanceledException if the PendingIntent is no longer allowing more intents to be sent through it.

        try {
            String resolvedType = intent != null ?
                    intent.resolveTypeIfNeeded(context.getContentResolver())
                    : null;
            int res = mTarget.send(code, intent, resolvedType,
                    onFinished != null
                    ? new FinishedDispatcher(this, onFinished, handler)
                    : null);
            if (res < 0) {
                throw new CanceledException();
            }
        } catch (RemoteException e) {
            throw new CanceledException(e);
        }
    
public java.lang.StringtoString()

        return "PendingIntent{"
                + Integer.toHexString(System.identityHashCode(this))
                + " target " + (mTarget != null ? mTarget.asBinder() : null) + "}";
    
public static voidwritePendingIntentOrNullToParcel(android.app.PendingIntent sender, android.os.Parcel out)
Convenience function for writing either a PendingIntent or null pointer to a Parcel. You must use this with {@link #readPendingIntentOrNullFromParcel} for later reading it.

param
sender The PendingIntent to write, or null.
param
out Where to write the PendingIntent.


                                                 
        
              
        out.writeStrongBinder(sender != null ? sender.mTarget.asBinder()
                : null);
    
public voidwriteToParcel(android.os.Parcel out, int flags)

        out.writeStrongBinder(mTarget.asBinder());