Difference Between Intent and Pending Intent | Android Concepts

devrkvibes
0

 Intent

An Android Intent is an object carrying an intent, i.e. a message from one component to another component either inside or outside of the application. Intents can communicate messages among any of the three core components of an application -- Activities, Services, and BroadcastReceivers.

The intent itself, an Intent object, is a passive data structure. It holds an abstract description of an operation to be performed.

For example: say you have an Activity that needs to launch an email client and send an email. To do this, your Activity would send an Intent with the action ACTION_SEND, along with the appropriate chooser, to the Android Intent Resolver:


Intent intent = new Intent(Intent.ACTION_SENDTO);

intent.setData(Uri.parse("mailto:")); // only email apps should handle this

The specified chooser gives the proper interface for the user to pick how to send your email data.

EXPLICIT INTENTS

// Explicit Intent by specifying its class name

   Intent i = new Intent(this, TargetActivity.class);

   i.putExtra("Key1", "ABC");

   i.putExtra("Key2", "123");

// Starts TargetActivity

   startActivity(i);

IMPLICIT INTENTS

// Implicit Intent by specifying a URI

   Intent i = new Intent(Intent.ACTION_VIEW,  Uri.parse("http://www.example.com"));

// Starts Implicit Activity

   startActivity(i); 

Intent

An Android Intent is an object carrying an intent, i.e. a message from one component to another component either inside or outside of the application. Intents can communicate messages among any of the three core components of an application -- Activities, Services, and BroadcastReceivers.

The intent itself, an Intent object, is a passive data structure. It holds an abstract description of an operation to be performed.

For example: say you have an Activity that needs to launch an email client and send an email. To do this, your Activity would send an Intent with the action ACTION_SEND, along with the appropriate chooser, to the Android Intent Resolver:

Intent intent = new Intent(Intent.ACTION_SENDTO);

intent.setData(Uri.parse("mailto:")); // only email apps should handle this

The specified chooser gives the proper interface for the user to pick how to send your email data.


EXPLICIT INTENTS

// Explicit Intent by specifying its class name

   Intent i = new Intent(this, TargetActivity.class);

   i.putExtra("Key1", "ABC");

   i.putExtra("Key2", "123");

// Starts TargetActivity

   startActivity(i);

IMPLICIT INTENTS

// Implicit Intent by specifying a URI

   Intent i = new Intent(Intent.ACTION_VIEW,  Uri.parse("http://www.example.com"));

// Starts Implicit Activity

   startActivity(i); 

Pending Intent

A PendingIntent is a token that you give to a foreign application (e.g. NotificationManager, AlarmManager, Home Screen AppWidgetManager, or other 3rd party applications), which allows the foreign application to use your application's permissions to execute a predefined piece of code.

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: almost always, for example, the base Intent you supply should have the component name explicitly set to one of your own components, to ensure it is ultimately sent there and nowhere else.

Post a Comment

0 Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !
To Top