All About PendingIntents

PendingIntent is an important part of the Android framework, but most of the resources available for developers focus on their implementation details - a "system-supported token reference" - rather than using them.





Android 12 , , PendingIntent



, , , , , PendingIntent



.





PendingIntent?

PendingIntent



Intent



, ,   . , .





, . .





PendingIntent



, Intent



, PendingIntent



, . , , .





, PendingIntent



, .





PendingIntent



- , :





val intent = Intent(applicationContext, MainActivity::class.java).apply {
    action = NOTIFICATION_ACTION
    data = deepLink
}
val pendingIntent = PendingIntent.getActivity(
    applicationContext,
    NOTIFICATION_REQUEST_CODE,
    intent,
    PendingIntent.FLAG_IMMUTABLE
)
val notification = NotificationCompat.Builder(
        applicationContext,
        NOTIFICATION_CHANNEL
    ).apply {
        // ...
        setContentIntent(pendingIntent)
        // ...
    }.build()
notificationManager.notify(
    NOTIFICATION_TAG,
    NOTIFICATION_ID,
    notification
)
      
      



, Intent



, , PendingIntent



, .





, , , PendingIntent



, , , FLAG_IMMUTABLE.





NotificationManagerCompat.notify() . , , PendingIntent.send() PendingIntent



, .





PendingIntent

, PendingIntent



, , ! , PendingIntent



, , FLAG_UPDATE_CURRENT:





val updatedIntent = Intent(applicationContext, MainActivity::class.java).apply {
   action = NOTIFICATION_ACTION
   data = differentDeepLink
}
// Because we're passing `FLAG_UPDATE_CURRENT`, this updates
// the existing PendingIntent with the changes we made above.
val updatedPendingIntent = PendingIntent.getActivity(
   applicationContext,
   NOTIFICATION_REQUEST_CODE,
   updatedIntent,
   PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
)
// The PendingIntent has been updated.
      
      



, PendingIntent



.





Inter-app APIs

. startActivityForResult() onActivityResult(), .





-, API . PendingIntent



extra Intent



, . PendingIntent



, .





PendingIntent



, , , , .





PendingIntent



, , - - Intent



. , , , .





PendingIntents

, , ? , - : " PIZZA!".





PendingIntent



.





PendingIntent



— , , Intent



, , PendingIntent.getIntent()



, Intent



, . ?





send()



PendingIntent



, , , , Intent



:





fun PendingIntent.send(
    context: Context!, 
    code: Int, 
    intent: Intent?
)
      
      



intent



Intent



, PendingIntent



, Intent



, PendingIntent



.





.





val orderDeliveredIntent = Intent(applicationContext, OrderDeliveredActivity::class.java).apply {
   action = ACTION_ORDER_DELIVERED
}
val mutablePendingIntent = PendingIntent.getActivity(
   applicationContext,
   NOTIFICATION_REQUEST_CODE,
   orderDeliveredIntent,
   PendingIntent.FLAG_MUTABLE
)
      
      



PendingIntent



-. customerMessage



, :





val intentWithExtrasToFill = Intent().apply {
   putExtra(EXTRA_CUSTOMER_MESSAGE, customerMessage)
}
mutablePendingIntent.send(
   applicationContext,
   PENDING_INTENT_CODE,
   intentWithExtrasToFill
)
      
      



EXTRA_CUSTOMER_MESSAGE



Intent



.





(pending intent)

  • PendingIntent



    , Intent



    . , , , , Intent.setComponent().





  • , Intent.setPackage()



    . , . Intent



    , .





  • PendingIntent



    , FLAG_IMMUTABLE



    , , Intent



    .





, PendingIntent



, . PendingIntent



- - Intent



.





, PendingIntent



, , .





FLAG_IMMUTABLE: , Intent



PendingIntent



, Intent



PendingIntent.send()



. FLAG_UPDATE_CURRENT



PendingIntent



.





Android 12 PendingIntent



, , .





Android Android 6 (API 23) PendingIntents



.





FLAG_MUTABLE: , Intent



PendingIntent



PendingIntent.send()



.





ComponentName Intent



PendingIntent



. !





Android 12. Android 12 PendingIntents



, FLAG_IMMUTABLE



, .





FLAG_UPDATE_CURRENT: , PendingIntent



, PendingIntent



. PendingIntent



, .





FLAG_ONE_SHOT: PendingIntent



( PendingIntent.send()



). PendingIntent



, Intent



. - .





FLAG_ONE_SHOT , " (replay attacks)".





FLAG_CANCEL_CURRENT: PendingIntent



, , . , PendingIntent



, , . FLAG_CANCEL_CURRENT



, , .





PendingIntents

PendingIntent API. MediaStore.createWriteRequest(), Android 11.





static fun MediaStore.createWriteRequest(
    resolver: ContentResolver, 
    uris: MutableCollection<Uri>
): PendingIntent
      
      



, PendingIntent



Intent



, Intent



, , , .





, PendingIntents



PendingIntent



. , FLAG_UPDATE_CURRENT



FLAG_IMMUTABLE



.





, - ComponentName Intent



- PendingIntent



.





, , PendingIntent



, , .





PendingIntent



Android 12, . .





? !






"Android Developer. Basic". , , .








All Articles