{"id":1928,"date":"2014-12-05T13:12:31","date_gmt":"2014-12-05T07:42:31","guid":{"rendered":"http:\/\/codetheory.in\/?p=1928"},"modified":"2016-05-12T12:59:00","modified_gmt":"2016-05-12T07:29:00","slug":"android-pending-intents","status":"publish","type":"post","link":"https:\/\/codetheory.in\/android-pending-intents\/","title":{"rendered":"Understanding Android Pending Intents"},"content":{"rendered":"
PendingIntent<\/a> is basically an object that wraps another Intent<\/a> object. Then it can be passed to a foreign application where you’re granting that app the right to perform the operation, i.e., execute the intent as if it were executed from your own app’s process (same permission and identity). For security reasons you should always pass explicit intents to a PendingIntent rather than being implicit.<\/p>\n <\/p>\n It can be used for various purposes like declaring an Intent which is executed at some point of time in future via the A PendingIntent itself is a token referencing the original data (Intent action, data, categories, etc.) maintained by the system. Hence if the owning app’s (creator) process is killed, the PendingIntent will still remain usable from other app proesses that had received it. The owning app can later re-retrieve<\/a> the PendingIntent token if that’s still valid by specifying the same set of data and then even Each explicit intent is supposed to be handled by a specific app component like Activity, BroadcastReceiver or a Service. Hence PendingIntent has different methods to handle different types of Intents that it wraps:<\/p>\n Let’s get into some code now. We’ll create an Intent and wrap it into a PendingIntent:<\/p>\n So we created an intent, wrapped it inside a PendingIntent and executed the operation associated with the pending intent (using Although this code sample wasn’t any useful as the same could have been done with just an Intent and We kick off with in a similar fashion, i.e., create and Intent and wrap it inside a PendingIntent. Next we get the alarm manager to which we pass the In your BroadcastReceiver’s As soon as the broadcast event is sent, the device will vibrate for 2 seconds.<\/p>\n So hopefully you understood how an Intent can be passed on to other native or third party apps via PendingIntent for execution. Hope that helps!<\/p>\n","protected":false},"excerpt":{"rendered":" PendingIntent is basically an object that wraps another Intent object. Then it can be passed to a foreign application where you’re granting that app the right to perform the operation, i.e., execute the intent as if it were executed from your own app’s process (same permission and identity). For security reasons you should always pass … Continue reading “Understanding Android Pending Intents”<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[121],"tags":[105,89],"_links":{"self":[{"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/posts\/1928"}],"collection":[{"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/comments?post=1928"}],"version-history":[{"count":7,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/posts\/1928\/revisions"}],"predecessor-version":[{"id":2452,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/posts\/1928\/revisions\/2452"}],"wp:attachment":[{"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/media?parent=1928"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/categories?post=1928"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/tags?post=1928"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}AlarmManager<\/code><\/a> class or one which is executed when the user performs some action with your app Notification.<\/p>\n
cancel()<\/code><\/a> it.<\/p>\n
\n
PendingIntent.getActivity()<\/code><\/a> – Retrieve a PendingIntent to start an Activity.\n<\/li>\n
PendingIntent.getBroadcast()<\/code><\/a> – Retrieve a PendingIntent to perform a broadcast.\n<\/li>\n
PendingIntent.getService()<\/code><\/a> – Retrieve a PendingIntent to start a Service.\n<\/li>\n<\/ul>\n
\r\n\/\/ Explicit intent to wrap\r\nIntent intent = new Intent(this, LoginActivity.class);\r\n\r\n\/\/ Create pending intent and wrap our intent\r\nPendingIntent pendingIntent = PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_CANCEL_CURRENT);\r\ntry {\r\n \/\/ Perform the operation associated with our pendingIntent\r\n pendingIntent.send();\r\n} catch (PendingIntent.CanceledException e) {\r\n e.printStackTrace();\r\n}\r\n<\/pre>\n
send()<\/code><\/a> which is invoking
LoginActivity<\/code>. Let’s once go through the arguments passed to
getActivity()<\/code><\/a>:<\/p>\n
\n
this<\/code> (context) – This is the context in which the `PendingIntent` should start the activity.\n<\/li>\n
1<\/code> (requestCode) – This is a private request code for the sender. Use it later with the same method again to get back the same pending intent later. Then you can do various things like cancelling the pending intent with
cancel()<\/code><\/a>, etc.\n<\/li>\n
intent<\/code> (intent) – Explicit intent of the activity to be launched.\n<\/li>\n
PendingIntent.FLAG_CANCEL_CURRENT<\/code><\/a> (flags) – One of the flags that can be passed that. This one states that if the described PendingIntent already exists, then the current one will be cancelled first and then the new one will be generated. I’ve seen some code samples where simply
0<\/code> is passed which isn’t the value of any flags which probably means no flag is passed and the behaviour isn’t documented but I guess it’s similar to
FLAG_CANCEL_CURRENT<\/code>.\n<\/li>\n<\/ul>\n
startActivity()<\/code>, let’s see a better usage of pending intents.<\/p>\n
\r\nint seconds = 3;\r\n\/\/ Create an intent that will be wrapped in PendingIntent\r\nIntent intent = new Intent(this, MyReceiver.class);\r\n\r\n\/\/ Create the pending intent and wrap our intent\r\nPendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, intent, 0);\r\n\r\n\/\/ Get the alarm manager service and schedule it to go off after 3s\r\nAlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);\r\nalarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (seconds * 1000), pendingIntent);\r\n\r\nToast.makeText(this, "Alarm set in " + seconds + " seconds", Toast.LENGTH_LONG).show();\r\n<\/pre>\n
pendingIntent<\/code> and specify it to go off in 3 seconds.
AlarmManager.RTC_WAKEUP<\/code><\/a> passed to
AlarmManager.set()<\/code><\/a> is explained well here<\/a>.<\/p>\n
onReceiver()<\/code> method you can add a simple piece of code like this:<\/p>\n
\r\n@Override\r\npublic void onReceive(Context context, Intent intent) {\r\n \/\/ Vibrate for 2 seconds\r\n Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);\r\n vibrator.vibrate(2000);\r\n}\r\n<\/pre>\n