Android implicit intents VS explicit intents

2019-01-16 18:10发布

问题:

Working with android I realized that implicit intents are good choice in most of cases due to their's flexibility. But what's about explicit intents? What are benefits of using them? What are common cases when it's a good practice to use them?

回答1:

Implicit Intents do not directly specify the Android components which should be called , it only specifies action to be performed.An Uri can be used with the implicit intent to specify data type.

for example

Intent intent = new Intent(ACTION_VIEW,Uri.parse("http://www.google.com");

this will cause web browser to open a webpage .Android system searches for all components which are registered for the specific action and the data type.If many components are found then the user can select which component to use..

Explicit intents are used in the application itself wherein one activity can switch to other activty...Example Intent intent = new Intent(this,Target.class); this causes switching of activity from current context to the target activity. Explicit Intents can also be used to pass data to other activity using putExtra method and retrieved by target activity by getIntent().getExtras() methods.

Hope this helped.



回答2:

You typically use explicit intents for starting activities within your own application. At that point you know exactly which activity you want to start, so there is no reason to go through the extra work of setting up the implicit intents.



回答3:

  1. Explicit Intents are used to call a specific component. When you know which component you want to launch and you do not want to give the user free control over which component to use.For example, you have an application that has 2 activities. Activity A and activity B. You want to launch activity B from activity A. In this case you define an explicit intent targeting activityB and then use it to directly call it.

  2. Implicit Intents are used when you have an idea of what you want to do, but you do not know which component should be launched. Or if you want to give the user an option to choose between a list of components to use. If these Intents are send to the Android system it searches for all components which are registered for the specific action and the data type. If only one component is found, Android starts the component directly. For example, you have an application that uses the camera to take photos. One of the features of your application is that you give the user the possibility to send the photos he has taken. You do not know what kind of application the user has that can send photos, and you also want to give the user an option to choose which external application to use if he has more than one. In this case you would not use an explicit intent. Instead you should use an implicit intent that has its action set to ACTION_SEND and its data extra set to the URI of the photo.

    An explicit intent is always delivered to its target, no matter what it contains; the filter is not consulted. But an implicit intent is delivered to a component only if it can pass through one of the component's filters



回答4:

1) Explicit Intent: component name developer know so, name specified in Intent.

2) Implicit Intent: Not specified a component in Intent.



回答5:

From Docs:

There are two types of intents:

  • 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, you can 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.


回答6:

When You know and when you don't know

Explicit Intent: use explicit intent when you know exactly which activity can handle your request.
A very common example would be, you have a list activity and when you click an item in the list it opens a detail activity. In this case, you know that the details od the list item can be shown or handled by DetailActivity.class (or com.yourpackage.DetailActivity) of your application. so you start the activity by giving a full package name

startActivity(this,com.yourPackage.DetaiActivy.class);

Implicit Intent: When you don't know which activity of which application can handle your request (ACTION). Then you leave the task to OS.
Example: You have a list of song items and when you click any item it should play the song that's it. You don't know who can handle your request so you let the os decide it for you.

In this case
1) You write an intent
2) Add the Action
3) Start activity

intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(songPath);

How OS decides?

There is a term for that, called Intent Resolution.
In Intent resolution OS takes out the Action specified in your intent and goes in PackageManager and looks up into all the registered activities with an intent filter of all the application installed in your device. then it shows the pop up with the list of appropriate applications. So the safe way to write an implicit intent is this

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(songPath);
if (intent.resolveActivity(getPackageManager()) != null) {
    startActivity(intent);
}

How to make your application get inside that popup list ?

Let's say you are in File Explorer and you click on a music file then an implicit intent gets triggered with some action and extra data. If you want your applications to be added in the list of application then you have to register an intent filter with the action in AndroidManifest.xml file. Like this.

<application
    .....  >

    ......
    <activity android:name=".MusicActivity">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <data android:type="audio/*" />
        </intent-filter>
    </activity>
 ..... 


</application>

References
Common Intents and action list
More on Intent filters and Intent resolution



回答7:

  1. Implicit intent - When we want to call the system components through intent to perform a particular task and we don't really know the name of the components to be used, the android system will show the desired list of applications to perform the task.
  2. Explicit intent - When we want to call the another activity with the full qualified name of the activity and of course we know the name of the activity.


回答8:

Simply we can describe both intents like this..

Explicit Intents : They are used for communication between two activities in a single application.

eg : Consider an application which has a login page consisting of two Fields (say username and password).If both are true it will lead us to a page which displays the username field which we entered before.In this case we use explicit intents because we need to change the activities and to carry data from one activity to the other activity(username field) in the same application.

Implicit Intents : They are used for communication between two activities of different applications.

eg : consider a news app which describes about an accident in which the video of accident is recorded and uploaded in Facebook. While clicking on the link given in the news app it will direct us to Facebook .In this case the communication is between an activity in news app and and an activity in Facebook app.For this purpose we use Implicit Intents.

I hope you can understand.