简体   繁体   English

Android:意图过滤器如何工作?

[英]Android: How Intent-Filter works?

I read another post: Android: Understanding Intent-Filters but I still can't grasp what really Intent-filters do and how they work. 我读了另一篇文章: Android:了解Intent过滤器,但我仍然无法理解Intent过滤器的真正作用以及它们的工作方式。

For example: 例如:

What is the difference between: 之间有什么区别?

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <category android:name="android.intent.category.DEFAULT" />

            <action android:name="android.intent.action.SEND" />
            <action android:name="android.intent.action.SENDTO" />

            <data android:scheme="sms" />
            <data android:scheme="smsto" />
            <data android:scheme="mms" />
            <data android:scheme="mmsto" />
        </intent-filter>

And

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
            <category android:name="android.intent.category.DEFAULT" />

            <action android:name="android.intent.action.SEND" />
            <action android:name="android.intent.action.SENDTO" />

            <data android:scheme="sms" />
            <data android:scheme="smsto" />
            <data android:scheme="mms" />
            <data android:scheme="mmsto" />
        </intent-filter>

Why in the first case the app icon is showed in the app list and in the second case it is not? 为什么在第一种情况下,应用程序图标显示在应用程序列表中,而在第二种情况下却没有显示?

When I need to open an Intent-filter and close it? 当我需要打开一个Intent过滤器并将其关闭时?

If I do: 如果我做:

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
        </intent-filter>
        <intent-filter>
            <category android:name="android.intent.category.LAUNCHER" />
            <category android:name="android.intent.category.DEFAULT" />
         </intent-filter>
         <intent-filter>
            <action android:name="android.intent.action.SEND" />
            <action android:name="android.intent.action.SENDTO" />
         </intent-filter>
         <intent-filter>
            <data android:scheme="sms" />
            <data android:scheme="smsto" />
            <data android:scheme="mms" />
            <data android:scheme="mmsto" />
        </intent-filter>

Is it correct? 这是正确的吗?

Thanks in advance for the replies and clarifications :) 在此先感谢您的答复和澄清:)

Intent filters are supposed to be added BETWEEN the opening and closing tags of a receiver, service or activity. 应该在接收者,服务或活动的开始和结束标签之间添加意图过滤器。 They signify the "implicit intents" that the app can handle. 它们表示应用程序可以处理的“隐式意图”。 In your app menu, where you have all your apps listed, android looks for the intent Main and Launcher. 在列出所有应用程序的应用程序菜单中,android查找意图的Main和Launcher。 Whichever apps have that as an intent filter, those get displayed, and the activity associated with Main, Launcher gets called as soon as the user opens the app. 无论使用哪个应用程序作为意图过滤器,这些应用程序都会显示出来,并且与Main相关联的活动会在用户打开应用程序后立即调用。

      <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

These two intent filters associated with my activity called MainActivity tell android to 1) Place my app in the menu. 与我的活动相关的这两个意图过滤器MainActivity告诉android 1)将我的应用放置在菜单中。 2) Open MainActivity as soon as the user selects the app. 2)用户选择应用程序后,立即打开MainActivity。 Hence you should have only one activty with Main and Launcher as its intent filters. 因此,您应该只将Main和Launcher作为其意图过滤器进行一项活动。

For example if user selects share button and its an implicit intent, then the apps that have "share option" in the form of an filter can be called via a dialog box/ selector. 例如,如果用户选择共享按钮及其隐式意图,则可以通过对话框/选择器来调用具有“共享选项”过滤器形式的应用。

EDIT : 编辑:

< <

activity android:name="ShareActivity">
    <!-- This activity handles "SEND" actions with text data -->
    <intent-filter>
        <action android:name="android.intent.action.SEND"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:mimeType="text/plain"/>
    </intent-filter>
    <!-- This activity also handles "SEND" and "SEND_MULTIPLE" with media data -->
    <intent-filter>
        <action android:name="android.intent.action.SEND"/>
        <action android:name="android.intent.action.SEND_MULTIPLE"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:mimeType="application/vnd.google.panorama360+jpg"/>
        <data android:mimeType="image/*"/>
        <data android:mimeType="video/*"/>
    </intent-filter>
</activity>

The first activity, MainActivity, is the app's main entry point—the activity that opens when the user initially launches the app with the launcher icon:

    The ACTION_MAIN action indicates this is the main entry point and does not expect any intent data.
    The CATEGORY_LAUNCHER category indicates that this activity's icon should be placed in the system's app launcher. If the <activity> element does not specify an icon with icon, then the system uses the icon from the <application> element.

These two must be paired together in order for the activity to appear in the app launcher.

The second activity, ShareActivity, is intended to facilitate sharing text and media content. Although users might enter this activity by navigating to it from MainActivity, they can also enter ShareActivity directly from another app that issues an implicit intent matching one of the two intent filters.

http://developer.android.com/guide/components/intents-filters.html Take a look at this site. http://developer.android.com/guide/components/intents-filters.html看看这个网站。 So intent filters describe what the activity CAN do, how it CAN be started (via another app or the main launcher or a browser) and what additional functions it can do. 因此,意图过滤器描述了活动可以做什么,如何(通过另一个应用程序,主启动器或浏览器)启动活动以及活动可以执行的其他功能。

Accordingly to this guide: 根据指南:

To advertise which implicit intents your app can receive, declare one or more intent filters for each of your app components with an element in your manifest file. 要宣传您的应用程序可以接收哪些隐式意图,请为每个应用程序组件声明一个或多个意图过滤器,并在清单文件中声明一个元素。 Each intent filter specifies the type of intents it accepts based on the intent's action, data, and category. 每个意图过滤器根据意图的动作,数据和类别指定它接受的意图的类型。 The system will deliver an implicit intent to your app component only if the intent can pass through one of your intent filters. 仅当意图可以通过您的意图过滤器之一时,系统才会向您的应用程序组件提供隐式意图。

An app component should declare separate filters for each unique job it can do. 应用程序组件应针对其可以执行的每个唯一作业声明单独的过滤器。

Each intent filter is defined by an element in the app's manifest file, nested in the corresponding app component (such as an element). 每个意图过滤器均由应用清单文件中的元素定义,该元素嵌套在相应的应用组件中(例如元素)。 Inside the , you can specify the type of intents to accept using one or more of these three elements: action , data, category 在内,您可以使用以下三个元素中的一个或多个来指定要接受的意图类型:动作,数据,类别

It's okay to create a filter that includes more than one instance of action, data, or category. 可以创建一个包含多个动作,数据或类别实例的过滤器。 If you do, you simply need to be certain that the component can handle any and all combinations of those filter elements. 如果这样做,您只需要确定该组件可以处理这些过滤器元素的任何和所有组合即可。

Example filters 过滤器示例

To better understand some of the intent filter behaviors, look at the following snippet from the manifest file of a social-sharing app. 为了更好地理解某些意图过滤器行为,请查看社交共享应用程序清单文件中的以下代码片段。

<activity android:name="MainActivity">
    <!-- This activity is the main entry, should appear in app launcher -->
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

<activity android:name="ShareActivity">
    <!-- This activity handles "SEND" actions with text data -->
    <intent-filter>
        <action android:name="android.intent.action.SEND"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:mimeType="text/plain"/>
    </intent-filter>
    <!-- This activity also handles "SEND" and "SEND_MULTIPLE" with media data -->
    <intent-filter>
        <action android:name="android.intent.action.SEND"/>
        <action android:name="android.intent.action.SEND_MULTIPLE"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:mimeType="application/vnd.google.panorama360+jpg"/>
        <data android:mimeType="image/*"/>
        <data android:mimeType="video/*"/>
    </intent-filter>
</activity>

For more information read the article . 有关更多信息,请阅读文章

Read following links. 阅读以下链接。 These will give you a good idea about intent filters 这些将为您提供有关意图过滤器的好主意

http://www.tutorialspoint.com/android/android_intents_filters.htm http://www.tutorialspoint.com/android/android_intents_filters.htm

http://codetheory.in/android-intent-filters/ http://codetheory.in/android-intent-filters/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM