Joke Collection Website - Public benefit messages - The difference between Android Intent and PendingIntent

The difference between Android Intent and PendingIntent

Intent is an intention, which describes the intention to start an Activity, Broadcast or Service. The main information it holds is the component (Activity, Broadcast or Service) it wants to start.

PendingIntent can be seen as a wrapper for Intent. Can be called by other apps besides the current app. It means something like "passive" or "Callback", but not strictly "passive" or "Callback". In short, the current App cannot use it to immediately activate the Intent it wraps. Instead, when the external App executes this PendingIntent, the Intent inside is indirectly and actually called. The main information held by PendingIntent is the Intent it wraps and the Context of the current App. Precisely because PendingIntent saves the Context of the current App, it gives the external App the ability to execute the Intent in the PendingIntent just like the current App. Even if the current App no ??longer exists during execution, it can still exist through the current App. The Context in PendingIntent still executes the Intent.

Intent means intention in English, and pending means something that is about to happen or coming.

The PendingIntent class is used to handle what is about to happen. For example, it is used to jump to the page in Notification, but not immediately.

Intent is started in time, and the intent disappears when the activity where it is located disappears.

PendingIntent can be regarded as a wrapper for intent. The instance of pendingintent is usually obtained through getActivity, getBroadcast, and getService. The current activity cannot start the intent it contains immediately, but when the pendingintent is executed externally, Call the intent. Because the context of the current App is saved in pendingintent, it gives the external App the ability to execute the Intent in the pendingintent just like the current App. Even if the current App no ??longer exists at the time of execution, it can still exist through the existing App. The Context in pendingintent still executes the Intent. In addition, you can also handle the operations after the intent is executed. Often used with alertmanager and notificationmanager.

Intent is generally used to transfer data between Activity, Sercvice, and BroadcastReceiver, while PendingIntent is generally used in Notification and can be understood as an intent that delays execution. PendingIntent is a wrapper for Intent.

java code:

private void showNotify(){

Notification notice=new Notification();

notice.icon=R .drawable.icon;

notice.tickerText="You have a new message";

notice.defaults=Notification.DEFAULT_SOUND;

notice.when =10L;

//After 100 milliseconds delay, vibrate for 250 milliseconds, pause for 100 milliseconds, and then vibrate for 500 milliseconds

//notice.vibrate = new long[] { 100, 250, 100, 500}; Error?

//notice.setLatestEventInfo(this, "Notice", "Meeting", PendingIntent.getActivity(this, 0, null, 0));

notice.setLatestEventInfo(this, "Notification", "Meeting", PendingIntent.getActivity(this, 0, new Intent(this, Activity2.class), 0)); //The page will jump soon, not yet Jump

NotificationManager manager=(NotificationManager)getSystemService(this.NOTIFICATION_SERVICE);

manager.notify(0, notice);

}

1. Example of android sending SMS in GSM network

java code:

String msg = "Hello, beauty";

String number = " 135****6784″;

SmsManager sms = SmsManager.getDefault();

PendingIntent pi = PendingIntent.getBroadcast(SmsActivity.this, 0, new Intent(…), 0);

sms.sendTextMessage(number, null, msg, pi, null);

Toast.makeText(SmsActivity.this, "Send successfully", Toast.LENGHT_LONG). show();

PendingIntent is a description of an Intent. We can give this description to other programs, and other programs will do what you arrange to do at other times based on this description (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, which is equivalent to PendingIntent representing Intent).

The other program in this example is a program that sends text messages. After the text message is successfully sent, the intent must be broadcast.

Explanation of parameters in function SmsManager.sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent):

1) PendingIntent sentIntent: When the SMS is sent, if successful sendIntent will broadcast the intent described inside it, otherwise an error code will be generated and a callback will be made through android.app.PendingIntent.OnFinished. This parameter is best not to be empty, otherwise there will be potential problems of resource waste;

2) PendingIntent deliveryIntent: It is the PendingIntent broadcast after the message has been delivered to the recipient.

Looking at the PendingIntent class, you can see many Send functions, which are the related operations assigned to PendingIntent.