Pending Intent in Android

Hello friends hi. This post is regarding all about Pending Intent in Android. Here are the main points about Pending Intent...

1)- What is Pending Intent?

2)- What are Pending Intent constants?

3)- How to create Pending Intent for Broadcast Receiver?

4)- How to create Pending Intent for Activity?

5)- How to create Pending Intent for Service?

6)- What are the main purpose of request code parameter in Pending Intent?

7)- How to cancel a Pending Intent?

8)- What is the use case of Pending Intent in Notification?

9)- How to pass extra data with a Pending Intent?

10)- What happen if we create two Pending Intent with same request code and activity?


These above question are the most important for not only the development but also for the interview purpose...

So let's get start....

1)- What is Pending Intent?

In simple words, a Pending Intent can be defined as "A description of an Intent and the target action perform with it."

On the other hand, A pending Intent is a token that we give to a foreign application (eg:- Notification Manager or Alarm Manager) which allow the foreign application to use your application's permission to perform a predefined task.

Instance of a Pending Intent is created as ---

** getActivity (content, int, intent, int)

** getActivities (content, int, intent[], int)

** getBroadcast (content, int, intent, int)

** getService (content, int, intent, int)

The returned object can be handed to other application so that they can perform action we described on our behalf at the later time.



2)- What are Pending Intent constants?

There are mainly 6 Pending Intent constants which are as following....


** FLAG_CANCEL_CURRENT :- This flag indicated that if the described PendingIntent is already 
exist, then the current one should be canceled before generating the new one.


** FLAG_IMMUTABLE :- This flag indicated that the created PendingIntent should be immutable ie can not be modified.


** FLAG_MUTABLE :- This flag indicated that the created PendingIntent should be mutable ie can be modified.


** FLAG_NO_CREATE :- This flag indicates that if the described PendingIntent does not already exist then simply return null instead of creating the new one.


** FLAG_ONE_SHOT :- This flag indicates that the PendingIntent can be used only once.


** FLAG_UPDATE_CURRENT :- This flag indicated that if the described PendingIntent is already exists then keep titbit replace it's data with what is in the new one.




3)- How to create Pending Intent for Broadcast Receiver?

A PendingIntent for a Broadcast is created using the PendingIntent.getBroadcast() method as----

val i = Intent(
    this,
MyBroadcastReceiver::class.java
)
val pi = PendingIntent.getBroadcast(this, pendingIntentReqCode, i,
PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE)


4)- How to create Pending Intent for Activity?


A PendingIntent for a Broadcast is created using the PendingIntent.getActivity() method as----

val = Intent(
    this,
MyActivity::class.java
)
val pi = PendingIntent.getActivity(this, pendingIntentReqCode, i,
PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE)


5)- How to create Pending Intent for Service?


A PendingIntent for a Broadcast is created using the PendingIntent.getService() method as----

val = Intent(
    this,
MyService::class.java
)
val pi = PendingIntent.getService(this, pendingIntentReqCode, i,
PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE)



6)- What are the main purpose of request code parameter in Pending Intent?

The requestCode Parameter is used to differentiate between multiple PendingIntents that might be using with the same Intents but are supposed to different instances. This code is used by the system to distinguish between different Intents.



7)- How to cancel a Pending Intent

To cancel a PendingIntent, we need to create an Identical PendingIntent and then call the cancel() method on it. Example:


val = Intent(
    this,
MyActivity::class.java
)
val pi = PendingIntent.getActivity(this, pendingIntentReqCode, i,
    PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE)
pi.cancel()



8)- What is the use case of Pending Intent in Notification?

A common use case of PendingIntent in Notification is to defined an action that the notification should perform when it clicked.

 val i = Intent(
this,
MyBroadcastReceiver::class.java
)
val pi = PendingIntent.getActivity(this, pendingIntentReqCode, i,
PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE)

val notif= NotificationCompat.Builder(this, NOTIFICATION_CANCEL_ID)
.setContentTitle("Notification Content Title")
.setContentText("Notification Content Text")
.setSmallIcon(R.drawable.notification_small_icon)
.setContentIntent(pi).build()

val notificationManager=this.getSystemService(this@MainActivity, NOTIFICATION_SERVICE)

notificationManager.notify(NOTIFICATION_ID, nm)




9)- How to pass extra data with a Pending Intent?

We can pass extra data by adding it to the Intent before creating the PendingIntent. Eg:-

val i = Intent(
this,
MyBroadcastReceiver::class.java
)
i.putExtra("myKey", "myValue")

val pi = PendingIntent.getActivity(this, pendingIntentReqCode, i,
PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE)


10)- What happen if we create two Pending Intent with same request code and activity?
If we create two PendingIntent with the same requestCode and Intent, then the second one will either 
replace or update the first one depending on the flag used.
(Example: FLAG_UPDATE_CURRENT will update the existing one)




Comments

Popular posts from this blog

All about Fragment in Android

Architectural patterns in Software Engineering