2

I am new to android ,I need to get the details of my contacts, but the details include only 3

  1. Contact name

  2. contact number and

  3. Email ID

when I press a Button it will show these 3 details of my all contacts

I am using android Eclair version 2.1. Any solution ?

BigBoss
  • 85
  • 1
  • 4
  • 13

2 Answers2

4

By below code you can do that -

public void doLaunchContactPicker(View view) {
    Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,Contacts.CONTENT_URI);
    startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    if (resultCode == RESULT_OK) {
        switch (requestCode) 
        {
        case CONTACT_PICKER_RESULT:
            Cursor cursor = null;
            String email = "", name = "";
            try {
                Uri result = data.getData();
                Log.v(DEBUG_TAG, "Got a contact result: " + result.toString());

                // get the contact id from the Uri
                String id = result.getLastPathSegment();

                // query for everything email
                cursor = getContentResolver().query(Email.CONTENT_URI,  null, Email.CONTACT_ID + "=?", new String[] { id }, null);

                int nameId = cursor.getColumnIndex(Contacts.DISPLAY_NAME);

                int emailIdx = cursor.getColumnIndex(Email.DATA);

                // let's just get the first email
                if (cursor.moveToFirst()) {
                    email = cursor.getString(emailIdx);
                    name = cursor.getString(nameId);
                    Log.v(DEBUG_TAG, "Got email: " + email);
                } else {
                    Log.w(DEBUG_TAG, "No results");
                }
            } catch (Exception e) {
                Log.e(DEBUG_TAG, "Failed to get email data", e);
            } finally {
                if (cursor != null) {
                    cursor.close();
                }
                EditText emailEntry = (EditText) findViewById(R.id.editTextv);
                EditText personEntry = (EditText) findViewById(R.id.person);
                emailEntry.setText(email);
                personEntry.setText(name);
                if (email.length() == 0 && name.length() == 0) 
                {
                    Toast.makeText(this, "No Email for Selected Contact",Toast.LENGTH_LONG).show();
                }
            }
            break;
        }

    } else {
        Log.w(DEBUG_TAG, "Warning: activity result not ok");
    }
}

And, also refer these links -

  1. Get Contact Details

  2. How to Read Contacts

  3. Get All Contact Details

Don't forget to add the required permission -

<uses-permission android:name="android.permission.READ_CONTACTS"/>

in your AndroidManifest.xml file. And, Just modify this code with your needs.

Community
  • 1
  • 1
Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
  • Will I use the above code in my button click ? Now I am using it in the Button click but shows some errors like Multiple markers at this line . what can I do ? – BigBoss Jun 11 '12 at 10:30
  • I put the xml file in pastie.org – BigBoss Jun 11 '12 at 10:39
  • still the error remains use this link to show the error picture (https://mail-attachment.googleusercontent.com/attachment/u/0/?ui=2&ik=eff9917924&view=att&th=137db2f6c82eb0fb&attid=0.1&disp=inline&realattid=f_h3bfeg3x0&safe=1&zw&saduie=AG9B_P8tqKDycYLg-j4GlV1swYLX&sadet=1339412220450&sads=1UgWSNRTxN0VpMDbLcRrZ5cTf04) the above error is also the same error – BigBoss Jun 11 '12 at 10:59
  • just open this link in a new tab you can definitly see Can you see the picture ? – BigBoss Jun 11 '12 at 11:05
0

You can access addressbook like this;

ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,null, null,ContactsContract.Contacts.DISPLAY_NAME);
int kisiSayisi =  cur.getCount();

if(kisiSayisi > 0)
{
    int KisiIndex = 0;
    while(cur.moveToNext())
    {
        String id = cur.getString(cur.getColumnIndex(BaseColumns._ID));
        String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
        {
            Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ " = ?", new String[] { id }, null);
            while (pCur.moveToNext())
            {
                String phone = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
                 //String phoneType = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                 String dogruGSM = gsmNoKontrol(phone);
                 if(dogruGSM.compareTo("0") != 0){
                        Kisi kisi = new Kisi(KisiIndex, name, dogruGSM, false);
                        MyList.add(kisi);
                        KisiIndex ++;
                 }
             }
             pCur.close();
         }
     }
 }
Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
Savas Adar
  • 4,083
  • 3
  • 46
  • 54