Android implicit intents VS explicit intents

前端 未结 9 673
攒了一身酷
攒了一身酷 2020-12-04 16:30

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

相关标签:
9条回答
  • 2020-12-04 16:41

    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.
    0 讨论(0)
  • 2020-12-04 16:48
    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

    0 讨论(0)
  • 2020-12-04 16:49

    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

    0 讨论(0)
  • 2020-12-04 16:52

    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.

    0 讨论(0)
  • 2020-12-04 16:55

    Simply we can describe both intents like this...

    Explicit Intents: They are used for communication inside a particular application.

    eg: Consider an application that has a login page consisting of two Fields (say username and password).If both are true it will lead us to a page that 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 across two different applications.

    eg: consider an app that uses user's image from the camera as a profile picture. For this purpose, it uses Implicit Intent. It will call intent to camera application to get a picture from the it.

    I hope you can understand.

    0 讨论(0)
  • 2020-12-04 16:58

    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.
    0 讨论(0)
提交回复
热议问题