I'm trying to better understand the topic of intents.
An explicit intent is configured with a component's name. In every example I've seen, it's used to start or stop the component. Is this the only purpose of an explicit intent?
An implicit intent doesn't have a target component. Implicit intents can also start/stop a component, but they can also be received by BroadcastReceivers. Is there another way to receive an implicit intent?
When the OS sends the intent with the action set to Action.MAIN, that's an explicit intent, right?
Thanks.
An intent is a messaging object, a broadcast receiver is an app component. An intent is used to request some action from some app component, it could be a broadcast receiver, an activity or a service.
In android, Implicit Intents won't specify any name of the component to start instead, it declare an action to perform and it allows a component from other apps to handle it. For example, by using implicit intents we can request another app to show the location details of the user or etc.
From the android documentation:
Explicit intents specify the component to start by name (the fully-qualified class name). You'll typically use an explicit intent to start a component in your own app, because you know the class name of the activity or service you want to start. For example, start a new activity in response to a user action or start a service to download a file in the background.
Implicit intents do not name a specific component, but instead declare a general action to perform, which allows a component from another app to handle it. For example, if you want to show the user a location on a map, you can use an implicit intent to request that another capable app show a specified location on a map.
Explicit intents, as you've said, are used to start an activity in your application -- or to transition from one "screen" to another. An explicit intent would be something like Intent intent = new Intent(currentContext, ActivityB.class);
These types of intents are used when you are within your application and explicitly know which component you want to start depending on how the user interacts with your activity.
Implicit Intents don't directly specify the Android components which should be called, but rather just specify a general action to be performed. These are generally used when you would like some external application to do something for you. An example of an implicit intent used to send an email using an external application would be:
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL , new String[]{"[email protected]"});
i.putExtra(Intent.EXTRA_SUBJECT, "subject");
i.putExtra(Intent.EXTRA_TEXT , "body");
This intent would query for applications installed on the device that can handle sending an email, but there could be quite a few applications that can do this -- for example, if we have a gmail application, hotmail application, etc. So, basically you're just specifying a general action and asking the system "who can handle this", and the system will handle the rest. These types of intents are used by application developers so they don't have to "reinvent the wheel" if there is already something on the device that can do what the developer wants.
Here's a couple of references that might help explain it better:
http://developer.android.com/guide/components/intents-filters.html
http://www.vogella.com/articles/AndroidIntent/article.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With