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

标签 android android-intent intentfilter

我看了另一篇文章: 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>

是否正确?

预先感谢您的答复和说明:)

最佳答案

Intent 过滤器应该添加在接收器、服务或 Activity 的开始和结束标记之间。它们表示应用程序可以处理的“隐式 Intent ”。在您的应用程序菜单中,您列出了所有应用程序,android 会查找 Intent Main 和 Launcher。无论哪个应用将其作为 Intent 过滤器,它们都会显示出来,并且与 Main 关联的 Activity 会在用户打开应用时立即调用 Launcher。

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

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

例如,如果用户选择共享按钮及其隐式 Intent ,则可以通过对话框/选择器调用具有过滤器形式的“共享选项”的应用。

编辑:

<

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

关于Android:Intent-Filter 是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33099748/

相关文章:

android - 从Google Gallery获取图片的Android在更新Google Play服务后卡住了应用

android - 无法捕获使用 android.provider.Telephony.SMS_RECEIVED 收到的短信

android - 除了使用 Intent 过滤器之外,我还可以过滤 BroadcastReceiver 吗?

android - 删除回收者 View 网格布局中的默认间距

android - 从 OTG 为以太网卡分配静态 IP

java - Android - 如何在有意调用日历时指定位置?

android - 如何实现带有选项卡 Activity 的按钮

Android WebView 可以向前渲染 HTML,但不能向后渲染

android - XML 布局 - 条目标识符 0x11a 大于条目计数 0xb2

android - 仅在子 Activity 中使用 Monkey(android 调试)