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)
this,
MyBroadcastReceiver::class.java
)
val pi = PendingIntent.getBroadcast(this, pendingIntentReqCode, i,
PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE)
this,
MyActivity::class.java
)
val pi = PendingIntent.getActivity(this, pendingIntentReqCode, i,
PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE)
this,
MyService::class.java
)
val pi = PendingIntent.getService(this, pendingIntentReqCode, i,
PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE)
this,
MyActivity::class.java
)
val pi = PendingIntent.getActivity(this, pendingIntentReqCode, i,
PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE)
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)
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
Post a Comment