Android implicit intents VS explicit intents

≡放荡痞女 提交于 2019-12-17 15:28:39

问题


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. A Uri can be used with the implicit intent to specify the 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 activity...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:


KEY: When you know and when you don't know

Explicit Intent:

Use explicit intent when you know exactly which Activity can handle your request.

Example: 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 of the item can be shown or handled by DetailActivity.class of your application. So to perform this action you create an Intent by explicitly specifying the class name.

Intent showDeatil = new Intent(this,DetaiActivy.class);  
startActivity(showDeatil);

Implicit Intent:

Use implicit intent when you don't know which activity of which application/s can handle your request.

Example: You have a link. When you click the link it should open the webpage in some browser. You DON'T KNOW exactly which Activity in which application can handle your request. You just have a vague idea that its a webpage link so it should open a webpage in some browser when someone opens it. In this case, you just specify the ACTION and then OS takes care of the rest.

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);

BONUS:

How OS decides?

There is a term for it. It's called Intent Resolution.
In Intent resolution.

  • OS takes out the ACTION specified in your intent.

  • Goes in the PackageManager and looks up for all the registered activities with the matching ACTION all the application installed in your device.

  • Shows the list of all matching applications in a pop-up.

A safer way to write implicit intents.

Sometimes it's possible that there will be no Activity which matches with the ACTION. In this case, you will get a NullPointerException. So a more preferred way is this

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
if (intent.resolveActivity(getPackageManager()) != null) {
    startActivity(intent);
}

How to make your application get inside that popup list?

Let's say you have written some Browser Application. If you want your applications to show up in the pop-up list when someone opens the link. Then you have to register your Activity with the action using Intent Filters AndroidManifest.xml file. Like this.

<application
    .....  >

    ......
    <activity android:name=".YourBrowserActivity">
        <action android:name="android.intent.action.VIEW" />       
        <data android:scheme="http" android:host="www.example.com" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
    </intent-filter>
    </activity>
 ..... 


</application>

References
Common Intent ACTIONS and their Intent-Filters list
More on Intent filters and Intent resolution




回答6:


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.



回答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.




回答9:


Implicit Intent

  • It Pulls up the new Application without being specified which one to pull up.
  • It specifies only action to be performed and do not directly specify Android Components.
  • URI can be used with implicit Intent to specify the type of data.

Explicit Intent

  • It will pull up a specific application and is set while writing the code.
  • It is used in application itself wherein one activity can switch to other activity.
  • Used to pass data to other activity using put extra method and retrieved by target activity getIntent().
  • It is always delivered to target even filter is not consulted.


来源:https://stackoverflow.com/questions/2914881/android-implicit-intents-vs-explicit-intents

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!