Reputation: 1190
I am a newbie to android and when I was searching the code for sending an sms I found he used a pending intent and started learnign about it, I studied that it is used by 3rd party applications to start intents and also it takes action in the future but I didn't understand exactly how these work? Anyway in future whenever a pending intent gets called it calls an intent so instead why don't we directly go for intent?
I hope the question is clear so if possible please give me examples of working with intents and pending intents please dont't explain me the definitions again....
Upvotes: 0
Views: 85
Reputation: 1018
The main difference is even if the owning application's process of PendingIntent is killed, the PendingIntent itself will remain usable from other processes. If the creating application later re-retrieves the same kind of PendingIntent, it will receive a PendingIntent representing the same, if that its still valid, and also can call cancel() to remove it(actually the previous one).
Upvotes: 0
Reputation: 10785
why can't I go for intent instead of pending intent?
the reason you need to provide PendingIntent
and not Intent
:
1) let's assume you gave another application an Intent
in order that that application would use it somehow (for instance - the systems notification manager is another application). now think about it - how this application will know if this intent intended for starting Activity
or intended to start Service
or sending brodcast? answer: it can't! you might think that it can know based on the class parameter, but don't forget that it's possible also to start implicit Activities/Services!!!
conclusion : the other application can't know what exactly to to with this Intent without knowing what it intends for. that's why when creating PendingIntent you create it using the getBroadcast()
or getActivity()
or getService()
methods.
that's one of the things PendingIntent
is all about: encapsulating Intent
+ what to do with it.
2) let's assume that after a while you want to provide to that application another Intent
. now, there are 2 options:
how will the receiving application will know which one from the two?
answer: pending intent's helds and created by the system, and by providing pending intent that filter equal to another and same request code - will replace automatically previous one and not create new one.
3) let's assume that the reference to the intent was held and created directly by your application, and not by the system (as I wrote in '2'). now imagine this scenario:
3.1) your application created intent object
3.2) your application provided reference to that intent object to another applciation
3.3) your application process stopped from some reason
3.4) WTF should happend? reference to your application object held by another application??? that's a mess for the GC. the system could not free your process memory allocations because of that!
that's the second reason behind the scene pending intents instances held only by the system, and not by your specific application.
if it's not enough - and there is also the issue of security...
I'm not going to write you usage example, since any google search for PendingIntent turorial would results with tones of good examples, and anyway - the code would be different for a specific use case...
Upvotes: 1