Pendingintent implementation principle and code

Source: Internet
Author: User


Some basic Android Developers may not know what PendingIntent is for Android Intent-related content? For sendMessage in Notification and SmsManager and set in AlarmManager, there is PendingIntent. What are the differences between PendingIntent and Intent?

1. Intent

Generally, the Intent in Android is located in android. content. intent implementation is relatively simple. It is implemented directly from the Object class. It mainly stores some String, Int, and lightweight arrays internally, and provides some methods mainly to assign values or values.

2. PendingIntent

This is different from Intent in android. app. in the PendingIntent package, the package belongs to the app layer rather than the content layer of data storage encapsulation. From the first section, we can see that PendingIntent is for what will happen, such as when a text message is sent, this object is used to track future receipt of text messages, mainly text message receipt reports and sending success or failure, because the process of GSM communication to RIL and then to the mobile base station is very long, waiting through a Thread is troublesome and resource-consuming for our applications, while the underlying remote service of TelephonyManager on the Android framework layer will be tracked and eventually tracked through PendingIntent, the specific implementation principles and Code are as follows:

Public final class PendingIntent implements Parcelable {// implements the Parcelable interface to conveniently process binary data and data exchange for remote services
Private final IIntentSender mTarget;

Public static final int FLAG_ONE_SHOT = 1 <30;
Public static final int FLAG_NO_CREATE = 1 <29;
Public static final int FLAG_CANCEL_CURRENT = 1 <28;
Public static final int FLAG_UPDATE_CURRENT = 1 <27;

Public static class CanceledException extends AndroidException {
Public CanceledException (){
}

Public CanceledException (String name ){
Super (name );
}

Public CanceledException (Exception cause ){
Super (cause );
}
}

Public interface OnFinished {
Void onSendFinished (PendingIntent pendingIntent, Intent intent,
Int resultCode, String resultData, Bundle resultExtras );
}

Private static class FinishedDispatcher extendsIIntentReceiver. Stub// Here, Android123 is a friendly reminder. If you have mastered the RemoteService of Android or Java RMI, it should be easy to understand.
ImplementsRunnable{
Private final PendingIntent mPendingIntent;
Private final OnFinished mWho;
Private final Handler mHandler;
Private Intent mIntent;
Private int mResultCode;
Private String mResultData;
Private Bundle mResultExtras;
FinishedDispatcher (PendingIntent pi, OnFinished who, Handler handler ){
MPendingIntent = pi;
MWho = who;
MHandler = handler;
}
Public void implements mreceive (Intent intent, int resultCode,
String data, Bundle extras, boolean serialized, boolean sticky ){
MIntent = intent;
MResultCode = resultCode;
MResultData = data;
MResultExtras = extras;
If (mHandler = null ){
Run ();
} Else {
Mhandler. Post (this );
}
}
Public void run (){
Mwho. onsendfinished (mpendingintent, mintent, mresultcode,
Mresultdata, mresultextras );
}
}

Public static pendingintentGetActivity(Context, int requestcode,
Intent intent, int flags ){
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 pendingintentGetBroadcast(Context, int requestcode,
Intent intent, int flags ){
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 PendingIntentGetService(Context context, int requestCode,
Intent intent, int flags ){
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;
}

Public intentsender getintentsender (){
Return new intentsender (mtarget );
}

Public void cancel (){
Try {
Activitymanagernative. getdefault (). cancelintentsender (mtarget );
} Catch (RemoteException e ){
}
}

Public void send () throws canceledexception {
Send (null, 0, null );
}

Public void send (INT code) throws canceledexception {
Send (null, code, null );
}

Public void send (context, int code, intent)
Throws canceledexception {
Send (context, code, intent, null, null );
}

Public void send (INT code, onfinished, Handler handler)
Throws canceledexception {
Send (null, code, null, onFinished, handler );
}

Public void send (Context context, int code, Intent intent,
OnFinished onFinished, Handler handler) throws CanceledException {
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 String getTargetPackage (){
Try {
Return ActivityManagerNative. getDefault ()
. GetPackageForIntentSender (mTarget );
} Catch (RemoteException e ){
// Shocould never happen.
Return NULL;
}
}

@ Override
Public Boolean equals (Object otherobj ){
If (otherobj instanceof pendingintent ){
Return mtarget. asbinder (). Equals (pendingintent) otherobj)
. Mtarget. asbinder ());
}
Return false;
}

@ Override
Public int hashcode (){
Return mTarget. asBinder (). hashCode ();
}

@ Override
Public String toString (){
StringBuilder sb = new StringBuilder (128 );
Sb. append ("PendingIntent {");
Sb. append (Integer. toHexString (System. identityHashCode (this )));
Sb. append (":");
Sb. append (mTarget! = Null? MTarget. asBinder (): null );
Sb. append ('}');
Return sb. toString ();
}

Public int describecontents (){
Return 0;
}

Public void writetoparcel (parcel out, int flags ){
Out. writestrongbinder (mtarget. asbinder ());
}

Public static final parcelable. creator <pendingintent> creator
= New parcelable. creator <pendingintent> (){
Public pendingintent createfromparcel (parcel in ){
Ibinder target = in. readstrongbinder ();
Return target! = Null? New PendingIntent (target): null;
}

Public PendingIntent [] newArray (int size ){
Return new PendingIntent [size];
}
};

Public static void writePendingIntentOrNullToParcel (PendingIntent sender,
Parcel out ){
Out. writeStrongBinder (sender! = Null? Sender. mTarget. asBinder ()
: Null );
}

Public static PendingIntent readPendingIntentOrNullFromParcel (Parcel in ){
IBinder B = in. readStrongBinder ();
Return B! = Null? New PendingIntent (B): null;
}

/* Package */PendingIntent (IIntentSender target ){
MTarget = target;
}

/* Package */PendingIntent (IBinder target ){
MTarget = IIntentSender. Stub. asInterface (target );
}

/** @ Hide */
Public IIntentSender getTarget (){
Return mTarget;
}
}

The PendingIntent implementation is relatively simple. It mainly deals with Android-specific remote services (such as SMS, notifications, and alarms). Generally, applications do not need to use PendingIntent.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.