如何从Android设备获取所有电子邮件地址

时间:2015-01-18 13:21:52

标签: android

如何收集所有电子邮件地址而不仅仅是主电子邮件?我有以下代码,只收集1个电子邮件地址并将其作为事件发送,

      **public String getEmails(){
          Pattern emailPattern = Patterns.EMAIL_ADDRESS;
          Account[] accounts = AccountManager.get(this).getAccounts();
          for (Account account : accounts){
              if (emailPattern.matcher(account.name).matches()){
                  return account.name;
              }
          } return"";

      }     

abc.setUserEmails(this.getEmails(),this.getEmails()); **

1 个答案:

答案 0 :(得分:0)

假设您的意思是来自Android联系人列表,这里有一个应该有用的片段:

注意:我误解了这个问题,但如果有人正在寻找从联系人管理员那里获取电子邮件地址的方法,我会留下代码示例。

public void getAddresses()
{
    try
    {

        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        if (cur.getCount() > 0)
        {

            while (cur.moveToNext())
            {
                count++;

                String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                // Assuming you want this

                Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]
                        { id }, null);

                i=0;
                while (emailCur.moveToNext())
                {
                    // This would allow you get several email addresses
                    // if the email addresses were stored in an array
                    String email = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                    String emailType = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));

                    System.out.println(email);  // please dont spam me

                    i++;
                }
                emailCur.close();


            }


        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}