Android:Intent-Filter 是如何工作的?

Android: How Intent-Filter works?

我读了另一个 post: Android: Understanding Intent-Filters 但我仍然无法理解真正的 Intent-filters 是做什么的以及它们是如何工作的。

例如:

有什么区别:

        <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>

        <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>

为什么在第一种情况下应用程序图标显示在应用程序列表中,而在第二种情况下却没有?

何时需要打开和关闭 Intent-filter?

如果我这样做:

        <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>

是否正确?

提前感谢您的答复和说明:)

根据this指南:

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.

过滤器示例

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>

有关详细信息,请阅读 the article

意图过滤器应该添加在接收器、服务或 activity 的开始和结束标记之间。它们表示应用程序可以处理的 "implicit intents"。在您的应用程序菜单中,您列出了所有应用程序,android 查找 Intent Main 和 Launcher。无论哪个应用程序将其作为 Intent 过滤器,都会显示这些应用程序,并且 activity 与 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>

这两个与我的 activity 关联的名为 MainActivity 的 Intent 过滤器告诉 android 1) 将我的应用程序放在菜单中。 2)用户选择应用程序后立即打开 MainActivity。因此,您应该只有一个以 Main 和 Launcher 作为其 intent 过滤器的活动。

例如,如果用户选择共享按钮及其隐式意图,则可以通过对话框/选择器调用具有 "share option" 过滤器形式的应用程序。

编辑:

<

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 看看这个网站。因此,意图过滤器描述了 activity 可以做什么、如何启动它(通过另一个应用程序或主启动器或浏览器)以及它可以执行哪些附加功能。

阅读以下链接。这些将使您对意图过滤器有一个很好的了解

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

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