Get contacts with email id

前端 未结 3 938
孤独总比滥情好
孤独总比滥情好 2021-01-15 00:11

I need to get contacts information(cursor) with email. They must be distinct. There must be one entry per contact if he has got an email. How to do it? I am targetting new c

3条回答
  •  伪装坚强ぢ
    2021-01-15 01:07

    just do it in simple way first of all find contact_id, on basis of it we will search all email_ids related for that contact. on any button click event write this code

    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
    startActivityForResult(intent, 2);
    

    now on activity result,

    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        System.out.println("Request Code--"+requestCode);
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null && requestCode == 2) 
         {
                fromCurrent = true;
                Uri uri = data.getData();
                //fromCurrent=true;
                if (uri != null) {
                    Cursor c = null;
                    try {
                        c = getContentResolver().query(uri, new String[]{ 
                                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
                                    ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                                    ContactsContract.CommonDataKinds.Email.DATA ,
                                    ContactsContract.CommonDataKinds.Email.TYPE },
                                null, null, null);
    
                        if (c != null && c.moveToFirst()) {
                            String id = c.getString(0);
                            String name = c.getString(1);
    
                            System.out.println("id  "+id+" name:"+name);
    
                            ContactID = id;
                            retriveEmail();
    
    
                            if(arrEmail.size() == 0)
                            {
                                showToast("No Email Address found for the selected contact!");
                            }
                            else
                            {
                            ListFile = arrEmail.toArray(new String[arrEmail.size()]);
    
                              builder1 = new AlertDialog.Builder(ShareTheHeart_Activity.this);
    
                              builder1.setTitle("Select an email address :");
    
                              builder1.setSingleChoiceItems(ListFile,-1,new DialogInterface.OnClickListener() {     //@Override
                             public void onClick(DialogInterface dialog, int which) 
                             {
                                txtEmail.setText(ListFile[which]);
                                alert.cancel();
                              }
                             });
    
                              alert = builder1.create();
                              alert.show();      
    
                            }
                        }
                    } finally {
                        if (c != null) {
                            c.close();
                        }
                    }
                }
            }
    }
    

    where retriveEmail is created method, write it in your code this way,

      private void retriveEmail()
    {
        try { 
            arrEmail = new ArrayList();
        String id = ContactID;
    
         // query for everything email  
       cursor = getContentResolver().query(Email.CONTENT_URI,  
                null, Email.CONTACT_ID + "=?", new String[] { id },  
                null);  
    
        int emailIdx = cursor.getColumnIndex(Email.DATA);  
    
        // let's just get the first email  
       if (cursor.moveToFirst())
       {
       do{  
            email = cursor.getString(emailIdx);  
            arrEmail.add(email);
            Log.v("ABC", "Got email: " + email);  
        } while(cursor.moveToNext());
        } else {  
            Log.w("ABC", "No results");  
        }  
    
    } catch (Exception e) {  
        Log.e("ABC", "Failed to get email data", e);  
    } finally {  
        if (cursor != null) {  
            cursor.close();  
        }   
        }
    }
    

    that's it.

    please understand the code, don't just copy and paste!

提交回复
热议问题