Zeeshan Sardar
Zeeshan Sardar

Reputation: 235

Get default set Email of contact? Android programatically?

I have a contact. It has 3 emails. I saved second email as a default email of contact. How do I get that default email programmatically? Help Please. Thankyou

Here is the email string of the contact

enter image description here

Now I want to get this marked email:

enter image description here

Upvotes: 0

Views: 317

Answers (3)

Zulfiqar Tariq
Zulfiqar Tariq

Reputation: 394

Cursor c = getContentProvider().query(Email.CONTENT_URI, new String[] { Email.ADDRESS }, Email.CONTACT_ID + " = " + contactId + " AND " + Email.IS_SUPER_PRIMARY + " = 1", null, null);
if (c != null && c.moveToFirst()) {
   Log.d(TAG, "email = " + c.getString(0));
}

IS_SUPER_PRIMARY is the key to get this. hope this will solve your problem

Upvotes: 0

marmor
marmor

Reputation: 28239

The field you're looking for is IS_SUPER_PRIMARY, here's a code to get the default email for a contact:

long contactId = <your contact id>;
Cursor c = getContentProvider().query(Email.CONTENT_URI, new String[] { Email.ADDRESS }, Email.CONTACT_ID + " = " + contactId + " AND " + Email.IS_SUPER_PRIMARY + " = 1", null, null);
if (c != null && c.moveToFirst()) {
   Log.d(TAG, "email = " + c.getString(0));
}

Upvotes: 1

Ibukun Muyide
Ibukun Muyide

Reputation: 1298

Not sure there is a way to get the default email programmatically except you give the user ability to select an email account, generally, the user's first email are mostly the default email but not always so.

If you want to still use the first email, you just return the first mail from the email account list returned

you can do this

       public  String  getUserAccount(){
        AccountManager am = AccountManager.get(this);
        Account[] accounts = am.getAccounts();
        ArrayList<String> googleAccounts = new ArrayList<String>();
        for (Account ac : accounts)
        {
            String acname = ac.name;
            String actype = ac.type;
            //add only google accounts
            if(ac.type.equals("com.google"))  googleAccounts.add(ac.name);
        }
            String primaryAccount = googleAccounts.get(0);
         return primaryAccount;
      }

This will return the first google email

Upvotes: 0

Related Questions