{"id":1913,"date":"2014-11-24T13:57:55","date_gmt":"2014-11-24T08:27:55","guid":{"rendered":"http:\/\/codetheory.in\/?p=1913"},"modified":"2014-12-05T15:02:38","modified_gmt":"2014-12-05T09:32:38","slug":"android-intent-filters","status":"publish","type":"post","link":"https:\/\/codetheory.in\/android-intent-filters\/","title":{"rendered":"Understanding Android Intent Filters (Receiving Implicit Intents)"},"content":{"rendered":"

Intents<\/a> sends signals to the Android system telling it that some action needs to be performed by another component (activity, services, broadcast receivers) in the same app or a different app. The system starts resolving which component in which app is responsible to handle this event that just got triggered. Let’s see a simple example of an implicit intent that opens a webpage URL in the browser.<\/p>\n

<\/p>\n

\r\nString url = "http:\/\/google.com";\r\nIntent intent = new Intent(Intent.ACTION_VIEW);\r\nintent.setData(Uri.parse(url));\r\nstartActivity(intent);\r\n<\/pre>\n

Pretty simple piece of code where we create an intent with the ACTION_VIEW<\/code> intent action and set the data to google.com<\/code>. Once startActivity()<\/code> is triggered Android will start looking for all the apps that can handle this and most probably find just one which is the browser (chrome) that can receive it. It’ll launch chrome and open google.com<\/a> in it.<\/p>\n

Note:<\/strong> I highly recommend you to read my article on Android intents<\/a> to completely understand the concept of Intents.<\/p>\n

Now imagine if we wanted to advertise<\/em> that our<\/strong> app can also handle such an implicit intent where we take an HTTP URL and do some useful jazz with it. In such a case Android will give two<\/strong> choices to the user – single browser installed (chrome) and our app – from which he can make a selection. To register a component of our app that can receive an implicit intent for a specific action and data, we can declare an <intent-filter><\/code><\/a> for that particular component (activity, service or a broadcast receiver) in our manifest file.<\/p>\n

Note:<\/strong> An explicit intent has its target specified (while creation) hence there’s no scene of making choices from a list or some such thing. Hence intent filters for them doesn’t make sense inherently.<\/p>\n

Let’s see an example now where we’ll register an Activity component in our app as a browser (based on the aforementioned description) in our AndroidManifest.xml<\/code>:<\/p>\n

\r\n<activity\r\n    android:name="com.pycitup.pyc.WebViewActivity"\r\n    android:label="@string\/title_activity_web_view" >\r\n\r\n    <intent-filter>\r\n        <action android:name="android.intent.action.VIEW" \/>\r\n        <category android:name="android.intent.category.DEFAULT" \/>\r\n        <data android:scheme="http" \/>\r\n    <\/intent-filter>\r\n\r\n<\/activity>\r\n<\/pre>\n

Make sure that both the sending and receiving activities have requested permission for the type of action (ACTION_VIEW<\/code>) to be performed. In this case all that means is we need the INTERNET<\/code> permission in our manifest:<\/p>\n

\r\n<uses-permission android:name="android.permission.INTERNET" \/>\r\n<\/pre>\n

Now when the intent code that you see above is fired, the user will be given multiple choices among which one of them will be our own app that’ll trigger WebViewActivity<\/code>. Inside the Activity you could then do things like show up the webpage in a WebView or some other sort of funky stuff.<\/p>\n

Similar to this, from my previous article<\/a> on Android intents, we could have an intent filter for the “share” example too (ACTION_SEND<\/code>) where once the user wants to share a piece of message, our app shows up along with facebook, whatsapp, gmail, etc.:<\/p>\n

\r\n<activity\r\n    android:name="com.pycitup.pyc.ShareActivity"\r\n    android:label="@string\/title_activity_share" >\r\n\r\n    <intent-filter>\r\n      <action android:name="android.intent.action.SEND" \/>\r\n      <category android:name="android.intent.category.DEFAULT" \/>\r\n      <data android:mimeType="text\/plain" \/>\r\n    <\/intent-filter>\r\n\r\n<\/activity>\r\n<\/pre>\n

Given that now we understand the basics of intent filters<\/a> and have seen a few examples, let’s start understanding a bit more about the subelements.<\/p>\n

Each activity in the manifest can contain multiple intent filters blocks. Each intent filter specifies the type of intents it’ll accept based on the <action><\/code>, <category><\/code> and <data><\/code> subelements. So as soon as an implicit intent passes through (or conforms to) one of the intent filters it is delivered to that activity component or at least considered in the list of initial dialog with choices.<\/p>\n

Let’s understand what each subelement is for:<\/p>\n