Pending Intent is a special kind of Intent which wraps around an intent (base intent) and allows us
to send this intent to other application; which at later time, can execute the base intent on our behalf.
By our behalf, I mean that intent (provided as base intent) is executed with same permissions as the application; who created the pending
intent.
Why pending intent?
Say, we have a Service which syncs our important data with server. Service is local to app and it can not be started from outside the app.
We want this Service to be executed by system at specific time. After thinking a bit, we have decided to use Alarm for this purpose.
Now, our service is local to application (it can not be started from outside the app), how can we manage to start out service? Is it
even possible? Yes. Here, we ask PendingIntent to help us. Pending intent says, you don’t need to worry at all. Just create an
intent (as if you are starting service from your app) and handle it to me. I will fire the intent on your app’s behalf when needed.
Finally, we create a pending intent which starts our service and hand it over to alarm manger which invokes it on appropriate time.
Usage
Pending intent can be used in following situations
Returning result from services
Starting specific components from Alarm Manager
Starting components from notifications etc
Example
For the sake of this article, we will return data from a service to activity using pending intent.
SampleService - waits for 3 seconds and send data back to MainActivity.
MainActivity - start the service passing it pending intent and logs the data returned from service.
Output
Just in case, if you are wondering what AppUtils.log does. It simply logs the message.