Android - need of uri & mime type in intent/intent-filter
I am trying to understand the intent mechanism in Android, and am puzzled at a question that I'd like to get resolved.
Android provides action + data + category for intent & intent-filter. Data provides URI and/or MIME type. The intent can also have a bunch of extra values with it.
I want to ask why is the data i.e. URI & MIME type really needed in Android ?
Why couldn't everything have been an action and category of the action ?
What value does URI & MIME type add, in the sense, what would really break if they were missing ?
I ask all this also because when I see applications like phone, messaging and maps, all of them try to use the URI to get some data that could have gone through EXTRAS.
Some one please help me clarify this design aspect of Android's intent mechanism.
Why we use a mime type usually? To understand what kind of data is behind it, no? Android uses mimetype to understand which application could be a candidate to handle a specific file.
Suppose you create a PDF file in your application and you want to let it to be open by other applications installed inside user phone, If you don't set a mime type Android is not able to understand which application (well, it can sometimes understand the mimetype) could be perfect to open the file, and what application can handle it without problems... immaginate how could the SMS app can handle this file for example? it could just crash!
Why we need Data? Well, you should say where you want to act no? If you want to let an application to edit your pdf file you could send an ACTION_EDIT so the application which the user open know "Hey i can edit it!". But why setData()? As you said, we could use putExtra but with putExtra you need know the key where the data is and blabla. How can an application which doesn't know anything about your application handle it correctly? setData is an unique place, where you found what the application need to handle the action correctly.
From official docs:
The URI (a Uri object) that references the data to be acted on and/or the MIME type of that data. The type of data supplied is generally dictated by the intent's action. For example, if the action is ACTION_EDIT, the data should contain the URI of the document to edit. When creating an intent, it's often important to specify the type of data (its MIME type) in addition to its URI. For example, an activity that's able to display images probably won't be able to play an audio file, even though the URI formats could be similar. So specifying the MIME type of your data helps the Android system find the best component to receive your intent. However, the MIME type can sometimes be inferred from the URI—particularly when the data is a content: URI, which indicates the data is located on the device and controlled by a ContentProvider, which makes the data MIME type visible to the system.
To set only the data URI, call setData(). To set only the MIME type, call setType(). If necessary, you can set both explicitly with setDataAndType().
Caution: If you want to set both the URI and MIME type, do not call setData() and setType() because they each nullify the value of the other. Always use setDataAndType() to set both URI and MIME type.