SoFunction
Updated on 2025-04-05

In-depth analysis of pendingIntent and Intent in Android

In-depth analysis of pendingIntent in Android

pendingIntent literal meaning: waiting, undecided Intent.

To get a pendingIntent object, use the static method of the method classgetActivity(Context, int, Intent, int),getBroadcast(Context, int, Intent, int),getService(Context, int, Intent, int) It corresponds to the three behaviors of Intent, jump to an activity component, open a broadcast component, and open a service component.

There are 4 parameters, the third and first things are more important, followed by the fourth and second things. It can be seen that to get this object, an Intent must be passed as a parameter, and a context must be used as a parameter.

pendingIntent is a special type of Intent. The main difference is that the execution of the Intent is immediate, while the execution of the pendingIntent is not immediate. The operation performed by a pendingIntent is essentially an operation of an Intent passed in with parameters, but the purpose of using a pendingIntent is that the execution of the Intent operation it contains needs to meet certain conditions.

Main use places and examples: notification Notificatio sending, short message SmsManager sending, and the execution of the AlarmManager sirens, etc.

Android's status bar notification (Notification)

If you need to view the message, you can drag the status bar to the bottom of the screen to view the message.

step:

1 Get Notification Manager Notification Manager, which is also a system service
2 Establish notification Notification notification = new Notification(icon, null, when);
3 Set parameters for new notifications (such as sound, vibration, light flashing)
4 Add new notifications to Notification Manager

The code for sending the message is as follows:

//Get notification managerNotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)
int icon = .stat_notify_chat;
long when = ();//The time when the notification occurs is the current time of the system// Create a new notification and specify its icon and titleNotification notification = new Notification(icon, null, when);//The first parameter is the icon, the second parameter is the short prompt title, and the third is the notification time = Notification.DEFAULT_SOUND;// Make a default sound |= Notification.FLAG_AUTO_CANCEL;//Check notifications automatically after clicking themIntent openintent = new Intent(this, );
PendingIntent contentIntent = (this, 0, openintent, 0);// When clicking on the message, the openintent will be sent to the system.(this, “title”, “I'm content", contentIntent);
(0, notification);//The first parameter is a custom notification unique identifier

The focus is on the last parameter of the setLatestEventInfo( ) method! ! ! ! It is a PendingIntent!!!!!!!!!

Here we use PendingIntent (the original meaning of pend is to be determined, and the meaning is uncertain)

PendingIntent can be regarded as a wrapper of the Intent. The main information that PendingIntent holds is the Intent it wraps and the Context of the current Application. Because the PendingIntent stores the Context of the current Application, it gives other programs the ability to execute an Intent, even if the current Application no longer exists at the time of execution, the Intent can still be executed through the Context in the PendingIntent.

A good example of PendingIntent:

SmsManager's method for sending text messages:

sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliveryIntent);

The first parameter: destinationAddress the other party's mobile phone number

The second parameter: the scAddress SMS center number is generally set to empty

Third parameter: text message content

The fourth parameter: sentIntent determines whether the SMS is sent successfully. If you do not have a SIM card or the network is interrupted, you can judge it through this itent. Note that it is emphasized whether the "send" action is successful. So as for whether the other party receives it, it is another matter

The fifth parameter: deliveryIntent When the text message is sent to the recipient, this deliveryIntent will be received. That is, it emphasizes the result after "send"
That is to say, the sentIntent and deliveryIntent will only be activated when the SMS is sent successfully and the other party receives this SMS. This is also equivalent to delaying execution of Intent

The above two examples can be understood. PendingIntent is an Intent that can be executed under certain conditions. Its advantage over Intent is that it carries a Context object, so it does not have to rely on a certain activity to exist.

Thank you for reading, I hope it can help you. Thank you for your support for this site!