Android 选择电子邮件意图

发布于 2024-11-04 16:45:54 字数 1081 浏览 0 评论 0原文

我想从联系人列表中选择一封电子邮件。 选择一个联系人还不够好,因为一个联系人可能有多个电子邮件地址。

使用 API 演示,我成功地选择了联系人、电话号码,甚至地址。示例:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
// OR
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
// OR
intent.setType(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE);

但是,当尝试选择电子邮件时,

intent.setType(ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE);

我收到“活动未找到”异常。

知道如何从所有联系人的电子邮件中选择一封电子邮件吗?

谢谢。 阿利克.

更新(2011/05/02): 找到了另一种从联系人中挑选内容的方法,但电子邮件选择器仍然没有注册到意图。

工作:

new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
new Intent(Intent.ACTION_PICK,ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
new Intent(Intent.ACTION_PICK,ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI);

不工作:

new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Email.CONTENT_URI);

I'd like to pick an email from the contacts list.
Picking a contact is not good enough, because a contact can have several emails.

Using the API demo, I managed to pick a contact, phone number and even an address. Example:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
// OR
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
// OR
intent.setType(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE);

BUT, when trying to pick an email

intent.setType(ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE);

I get activity not found exception.

Any idea on how to pick an email from the all the contacts' emails?

Thanks.
Alik.

Update (2011/05/02):
Found another way to pick things from the contacts but still the email picker is not registered to the intent.

Working:

new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
new Intent(Intent.ACTION_PICK,ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
new Intent(Intent.ACTION_PICK,ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI);

NOT working:

new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Email.CONTENT_URI);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

知你几分 2024-11-11 16:45:54

我没有专门尝试使用选择器,但我们循环遍历联系人缓存并查找 MIME 类型为 Email.CONTENT_ITEM_TYPE 的所有联系人详细信息。

然后我们构建一个对话框,让用户选择他们想要使用的电子邮件地址,并将该电子邮件地址传递给用户的电子邮件应用程序,例如

  final Intent emailIntent = new Intent(Intent.ACTION_SEND);
  emailIntent.putExtra(Intent.EXTRA_STREAM, "Some text");

  Builder builder = new Builder(this);
  builder.setTitle("Choose email");
  builder.setItems(emailAddressesArray, new DialogInterface.OnClickListener()
  {
    @Override
    public void onClick(DialogInterface dialog, int which)
    {
      String address = emailAddresses.get(emailAddressesArray[which]);
      sLog.user("User selected e-mail: " + address);
      emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{address});
      startExternalActivity(emailIntent);
    }
  });
  builder.show();

I haven't specifically tried to use a picker, but we loop through our cache of the contacts and find all the contact details with a MIME type of Email.CONTENT_ITEM_TYPE.

Then we build a dialog to the let user pick which e-mail address they want to use, and we pass that e-mail address to the user's e-mail app, e.g.

  final Intent emailIntent = new Intent(Intent.ACTION_SEND);
  emailIntent.putExtra(Intent.EXTRA_STREAM, "Some text");

  Builder builder = new Builder(this);
  builder.setTitle("Choose email");
  builder.setItems(emailAddressesArray, new DialogInterface.OnClickListener()
  {
    @Override
    public void onClick(DialogInterface dialog, int which)
    {
      String address = emailAddresses.get(emailAddressesArray[which]);
      sLog.user("User selected e-mail: " + address);
      emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{address});
      startExternalActivity(emailIntent);
    }
  });
  builder.show();
挽手叙旧 2024-11-11 16:45:54

您必须使用以下常量(不是 CONTENT_ITEM_TYPE):

intent.setType(ContactsContract.CommonDataKinds.Email.CONTENT_TYPE);

you have to use the following constant (not CONTENT_ITEM_TYPE):

intent.setType(ContactsContract.CommonDataKinds.Email.CONTENT_TYPE);
时间海 2024-11-11 16:45:54

完美工作:

 Intent intent = new Intent(Intent.ACTION_PICK);
 intent.setType(ContactsContract.CommonDataKinds.Email.CONTENT_TYPE);
 startActivityForResult(intent, 1);

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            if (resultCode == Activity.RESULT_OK) {
                Uri contactData = data.getData();
                Cursor cursor = getActivity().managedQuery(contactData, null, null, null, null);
                cursor.moveToFirst();
                String email = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
                editText.setText(email);
            }


        }
}

Perfectly working:

 Intent intent = new Intent(Intent.ACTION_PICK);
 intent.setType(ContactsContract.CommonDataKinds.Email.CONTENT_TYPE);
 startActivityForResult(intent, 1);

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            if (resultCode == Activity.RESULT_OK) {
                Uri contactData = data.getData();
                Cursor cursor = getActivity().managedQuery(contactData, null, null, null, null);
                cursor.moveToFirst();
                String email = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
                editText.setText(email);
            }


        }
}
简美 2024-11-11 16:45:54

下面是一段示例代码,用于向用户显示联系人列表中的所有电子邮件地址并允许他们选择一个电子邮件地址(然后将其放入 ID 为 R 的 EditText 中)。 id.youredittextid)。

注意:这是一种相当低效的方法,如果您有很多联系人,则会导致相当大的延迟。但所有必要的代码都在这里;根据您的需要进行定制...

        // We're going to make up an array of email addresses
        final ArrayList<HashMap<String, String>> addresses = new ArrayList<HashMap<String, String>>();

        // Step through every contact
        final ContentResolver cr = getContentResolver();
        final Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        while (cursor.moveToNext())
        {
            final String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            final String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

            // Pull out every email address for this particular contact
            final Cursor emails = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, 
                                            ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, null, null);
            while (emails.moveToNext()) 
            {
                // Add email address to our array
                final String strEmail = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));

                final HashMap<String, String> email = new HashMap<String, String>();
                email.put("Name", contactName);
                email.put("Email", strEmail);

                addresses.add(email);
            }
            emails.close();
        }

        // Make an adapter to display the list
        SimpleAdapter adapter = new SimpleAdapter(this, addresses, android.R.layout.two_line_list_item,
                                                    new String[] { "Name", "Email" },
                                                    new int[] { android.R.id.text1, android.R.id.text2 });

        // Show the list and let the user pick an email address
        new AlertDialog.Builder(this)
          .setTitle("Select Recipient")
          .setAdapter(adapter, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

                EditText e = (EditText)findViewById(R.id.youredittextid);
                HashMap<String, String> email = addresses.get(which);
                e.setText(email.get("Email"));

              dialog.dismiss();
            }
          }).create().show(); 

Here's a sample bit of code to display all email addresses in the contact list to the user and allow them to select a single one (which is then put into an EditText with the id of R.id.youredittextid).

Note: This is a rather inefficient way to do this, and will cause quite a delay if you have lots of contacts. But all the necessary code is here; customize as you see fit...

        // We're going to make up an array of email addresses
        final ArrayList<HashMap<String, String>> addresses = new ArrayList<HashMap<String, String>>();

        // Step through every contact
        final ContentResolver cr = getContentResolver();
        final Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        while (cursor.moveToNext())
        {
            final String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            final String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

            // Pull out every email address for this particular contact
            final Cursor emails = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, 
                                            ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, null, null);
            while (emails.moveToNext()) 
            {
                // Add email address to our array
                final String strEmail = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));

                final HashMap<String, String> email = new HashMap<String, String>();
                email.put("Name", contactName);
                email.put("Email", strEmail);

                addresses.add(email);
            }
            emails.close();
        }

        // Make an adapter to display the list
        SimpleAdapter adapter = new SimpleAdapter(this, addresses, android.R.layout.two_line_list_item,
                                                    new String[] { "Name", "Email" },
                                                    new int[] { android.R.id.text1, android.R.id.text2 });

        // Show the list and let the user pick an email address
        new AlertDialog.Builder(this)
          .setTitle("Select Recipient")
          .setAdapter(adapter, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

                EditText e = (EditText)findViewById(R.id.youredittextid);
                HashMap<String, String> email = addresses.get(which);
                e.setText(email.get("Email"));

              dialog.dismiss();
            }
          }).create().show(); 
一人独醉 2024-11-11 16:45:54

一个旧线程但是......这个信息可能对某人有用。
当您使用 Intent.ACTION_PICK 启动 Intent 时,您将尝试调用“联系人选择器”活动,该活动通常由联系人/电话簿应用程序提供。

最新版本的普通 (Google) 通讯录应用 (Android 4.4.4) 的意图过滤器中确实有用于 mimetype 的 Email.CONTENT_ITEM_TYPE ,因此它应该响应此类意图,就像您所做的那样。我不确定,但旧版本(ICS、JB)的联系人选择器似乎在其意图过滤器中没有此功能。

简而言之,此意图应该适用于安装了普通联系人的 KK,而不适用于较旧的 Android 版本。

An old thread but... this information could prove useful to someone.
When you start an Intent with Intent.ACTION_PICK you are trying to invoke the "Contact Picker" activity, which is usually supplied by Contacts / Phonebook application.

Latest version of vanilla (Google) Contacts app (Android 4.4.4) does have Email.CONTENT_ITEM_TYPE for mimetype in its intent filter, so it should respond to such intent, just as you made it. I am not sure but it seems that Contact Picker for older versions (ICS, JB) did not have this in its intent filters.

In short, this intent should work on KK with vanilla Contacts installed, and should not work on older Android versions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文