从电子邮件意图中获取 mailto 值

Get mailto value from email intent

我创建了一个显示在电子邮件发送应用程序列表中的应用程序。

在一个 特殊应用程序 中,当用户单击电子邮件地址时,所有 email sending 应用程序都会出现在列表中,我的应用程序是其中之一,用户可以选择它。

我想在用户点击我的应用程序时保存 电子邮件。

一切正常,但我不知道如何从我的应用程序中获取电子邮件?

我知道其他应用程序使用此代码:

Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
            "mailto","abc@gmail.com", null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Body");
startActivity(Intent.createChooser(emailIntent, "Send email..."));

但我不知道如何访问这些放在 Extra 中的数据。我只需要从顶部代码中获取 mailto 值。

我现在如何获得 mailto 值?


编辑:

我有一个共享许多电子邮件地址的应用程序,但我无法复制它们。当我点击这些电子邮件时,这些电子邮件只是链接 android 显示所有电子邮件发件人应用程序。我想使用我开发的应用程序逐一获取这封电子邮件,并将它们保存在 txt 文件中。


编辑 2: 我找到了一种在所有其他应用程序电子邮件发件人中显示我的应用程序的方法 list.i 发现不同的应用程序使用不同的方法来放置用于发送电子邮件的额外内容。 为了解决这个问题并在所有其他应用程序电子邮件发送列表中显示您的应用程序,我在我的 AndroidManifest.xml 中使用了很多 <intent-filter> 像这样:

许多应用程序使用 ACTION_VIEW 方法发送电子邮件。如果您想在此类应用程序中展示您的应用程序,您必须在清单文件中使用此代码:

<intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <data android:scheme="mailto" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
            </intent-filter>

另一种类型的应用程序使用 ACTION_SEND 提供调用电子邮件发件人应用程序列表的意图。如果你想在这些类型的应用程序中显示你的应用程序,你必须将此代码添加到你的 AndroidManifest.xml 文件和你的 Activity 标签中: 您可以看到 3 种提供意图的方法,我发现这些方法可以在所有应用程序电子邮件发送列表中显示我的应用程序,也许存在其他方法,但我现在不知道:

<intent-filter>
            <action android:name="android.intent.action.SEND"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <data android:scheme="mailto"/>
        </intent-filter>

        <intent-filter>
            <action android:name="android.intent.action.SEND"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <data android:mimeType="text/*"/>
        </intent-filter>

        <intent-filter>
            <action android:name="android.intent.action.SEND"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <data android:mimeType="message/*"/>
        </intent-filter>

编辑 3:我在 activity 中使用了这段代码:

 bundle = intent.getExtras();
                if (bundle != null) {
                    for (String key : bundle.keySet()) {
                        Object value = bundle.get(key);
                        Log.d("Extra : ", String.format("%s %s (%s)", key,
                                value.toString(), value.getClass().getName()));
                    }
                }

现在我得到日志:

D/Extra :: android.intent.extra.EMAIL [Ljava.lang.String;@2244b9ca ([Ljava.lang.String;)

它显示我在 extra 中有一个值,它的名称是 android.intent.extra.EMAIL 但我无法获得该值。我测试了很多方法!

你可以这样做。我刚刚测试了它并且它有效。

在示例发送应用程序中,在单击按钮或 TextView 后执行此操作: 注意不要在使用 autolink 的 TextView 中的 link 上使用它,因为那样会破坏它。只需尝试使用一个简单的按钮即可开始。

// Create the text message with a string
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SENDTO);
sendIntent.setData(Uri.parse("mailto:test@gmail.com"));
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "test subject");
sendIntent.putExtra(Intent.EXTRA_TEXT, "sent from first app");

// Verify that the intent will resolve to an activity
if (sendIntent.resolveActivity(getPackageManager()) != null) {
    startActivity(sendIntent);
}

然后在接收应用清单中:

<!-- 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:scheme="mailto"/>
</intent-filter>

接收应用Activity:

import static android.content.Intent.EXTRA_SUBJECT;
import static android.content.Intent.EXTRA_TEXT;

String dataEmail = intent.getDataString();
if (dataEmail != null) {
    Log.d(TAG, "onCreate: DATA EMAIL: " + dataEmail.substring(7));
}

String subject = intent.getStringExtra(EXTRA_SUBJECT);
Log.d(TAG, "onCreate: SUBJECT: " + subject);

String extraText = intent.getStringExtra(EXTRA_TEXT);
Log.d(TAG, "onCreate: EXTRA TEXT: " + extraText);

如果您希望它也适用于具有自动link 的 TextView,那么您需要将它添加到清单中,但是如果您这样做,那么只有电子邮件部分将工作。主题将返回 null。

<intent-filter>
    <action android:name="android.intent.action.VIEW"/>
    <data android:scheme="mailto"/>
    <category android:name="android.intent.category.DEFAULT"/>
    <category android:name="android.intent.category.BROWSABLE"/>
</intent-filter>

回复问题中的编辑 3 如果我在我的示例应用程序之间传递一个包并使用它:

for (String string :bundle.keySet()) {
    Log.d(TAG, "onCreate: VALUE: " + bundle.getString(string));
}

然后它打印出所有的值就好了。但是我知道我输入了哪些值(字符串),但我不知道您使用的任何应用程序的开发人员使用了什么。

为了帮助解决这个问题,您可以打印以下内容:

Log.d(TAG, "onCreate: BUNDLE STRING: " + bundle.toString());

至少当我尝试它时,我得到了关于我放入其中的内容的详细信息,并且从那里我可以弄清楚(如果我不知道的话)。