让 Flutter 应用程序接受传入的意图

Making a Flutter app accept incoming intents

Flutter for Android developers 文档解释了如何使 Flutter 应用程序接受传入的 Android 意图,并给出了接受 Share 意图的代码示例。我已经复制了它并且效果很好。

但我现在正在尝试让我的应用程序接受其他类型的意图,特别是编辑联系人意图,这样当我使用默认的 Phone 应用程序并点击一个联系人,我的应用程序被建议完成该操作。

我已经尝试了 <intent-filter> 的所有可能组合,我可以在谷歌搜索和搜索 GitHub 后找到它,但没有任何方法可以让它发挥作用。举几个:

<intent-filter>
    <action android:name="android.intent.action.VIEW"/>
    <category android:name="android.intent.category.DEFAULT"/>
    <data android:mimeType="vnd.android.cursor.item/person"/>
    <data android:mimeType="vnd.android.cursor.item/contact"/>
    <data android:mimeType="vnd.android.cursor.item/raw_contact"/>
    <data android:mimeType="vnd.android.cursor.item/phone"/>
    <data android:mimeType="vnd.android.cursor.item/phone_v2"/>
</intent-filter>

<intent-filter>
    <action android:name="android.intent.action.GET_CONTENT"/>
    <category android:name="android.intent.category.DEFAULT"/>
    <data android:mimeType="vnd.android.cursor.item/person" android:host="contacts"/>
    <data android:mimeType="vnd.android.cursor.item/contact" android:host="contacts"/>
    <data android:mimeType="vnd.android.cursor.item/raw_contact" android:host="contacts"/>
</intent-filter>

<intent-filter >
    <action android:name="android.intent.action.EDIT" />
    <category android:name="android.intent.category.DEFAULT" />
    <data
        android:host="contacts"
        android:mimeType="vnd.android.cursor.item/person" />
    <data
        android:host="com.android.contacts"
        android:mimeType="vnd.android.cursor.item/contact" />
    <data
        android:host="com.android.contacts"
        android:mimeType="vnd.android.cursor.item/raw_contact" />
</intent-filter>

<intent-filter>
    <action android:name="android.intent.action.VIEW"/>
    <category android:name="android.intent.category.APP_CONTACT"/>
</intent-filter>

etc.

我做错了什么?

这取决于您使用的通讯录应用程序。

每台设备都可以附带制造商特定的联系人应用程序(例如 Samsung Contacts), Google 提供的应用程序(Google 联系人), 服务提供商可以安装他们自己的联系人应用程序, 或者用户可能正在使用从 Google Play 安装的应用程序(例如 Contacts+)。

每个此类应用都可以选择通过其他应用可能捕获的 Intent 来启动编辑器,或者只启动它自己的内置编辑器而不涉及 Intent 系统。

如果有要捕获的意图,您可以通过检查 LogCat、过滤到标签 ActivityManager 来找出它是什么,它会显示设备上抛出的所有意图,所以您可以研究它以找出您需要捕捉的意图细节。

为了后代,在上面 marmor 的回答的基础上显示具体步骤:

$ adb logcat | grep ActivityTaskManager
[...]
04-01 18:09:27.306  1384  4229 I ActivityTaskManager: START u0 {act=android.intent.action.INSERT_OR_EDIT typ=vnd.android.cursor.item/contact cmp=android/com.android.internal.app.ResolverActivity (has extras)} from uid 10065
[...]

所以我所要做的就是将它添加到我的 AndroidManifest.xml:

<intent-filter>
  <action android:name="android.intent.action.INSERT_OR_EDIT"/>
  <category android:name="android.intent.category.DEFAULT"/>
  <data android:mimeType="vnd.android.cursor.item/contact"/>
</intent-filter>