3

all i want to take number from phonebook of android in my application database.. i have tried it with below code but here am getting name of person instead i want number from phonebook and want to store it in my database..how to achieve this????can any one guide me..

@Override
    public void onActivityResult(int reqCode, int resultCode, Intent data){
        super.onActivityResult(reqCode, resultCode, data);

        switch(reqCode){
           case (PICK_CONTACT):
             if (resultCode == Activity.RESULT_OK){
                 Uri contactData = data.getData();
                 Cursor c = managedQuery(contactData, null, null, null, null);

                 if (c.moveToFirst()){
                     // other data is available for the Contact.  I have decided
                     //    to only get the name of the Contact.
                     String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.CONTENT_TYPE));
                     Toast.makeText(getApplicationContext(), name, Toast.LENGTH_SHORT).show();
                 }
             }
        }

Thanks in Advance--

sawan
  • 2,341
  • 3
  • 25
  • 51

3 Answers3

6

I know that this is an old question, but I think the below resource from Android is extremely helpful on this matter. The "bonus" section is the exact code that raj was looking for. I think this link should be helpful to anyone who sees this question in the future, especially if you don't understand CapDroid's snippet.

http://developer.android.com/training/basics/intents/result.html

eclectronica
  • 116
  • 1
  • 2
6

try this code,

   @Override
   public void onActivityResult(int reqCode, int resultCode, Intent
  data){
    super.onActivityResult(reqCode, resultCode, data);

    switch(reqCode){
       case (PICK_CONTACT):
         if (resultCode == Activity.RESULT_OK)
         {
             Uri contactData = data.getData();
             Cursor c = managedQuery(contactData, null, null, null, null);
          if (c.moveToFirst()) {
          String id =   
            c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));

          String hasPhone =
          c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

          if (hasPhone.equalsIgnoreCase("1")) {
         Cursor phones = getContentResolver().query( 
                      ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, 
                      ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id, 
                      null, null);
            phones.moveToFirst();
            String cNumber = phones.getString(phones.getColumnIndex("data1"));
          }
            }
         }
    }
Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
  • okey.....and if i want to show that particular number on textview on same layout then how to achieve it???? – sawan Aug 25 '11 at 06:40
  • done..its working..thanks a lot.....but cursor is moving to next contact its retrieving same number for all contacts... – sawan Aug 25 '11 at 06:53
0

This will help you:

public void onActivityResult(int reqCode, int resultCode, Intent data) { super.onActivityResult(reqCode, resultCode, data);

    try {
        if (resultCode == Activity.RESULT_OK) {
            Uri contactData = data.getData();
            Cursor cur = managedQuery(contactData, null, null, null, null);
            ContentResolver contect_resolver = getContentResolver();

            if (cur.moveToFirst()) {
                String id = cur.getString(cur.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
                String name = "";
                String no = "";

                Cursor phoneCur = contect_resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);

                if (phoneCur.moveToFirst()) {
                    name = phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    no = phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                }

                Log.e("Phone no & name :***: ", name + " : " + no);
                txt.append(name + " : " + no + "\n");

                id = null;
                name = null;
                no = null;
                phoneCur = null;
            }
            contect_resolver = null;
            cur = null;
            //                      populateContacts();
        }
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
        Log.e("IllegalArgumentException::", e.toString());
    } catch (Exception e) {
        e.printStackTrace();
        Log.e("Error :: ", e.toString());
    }
}
Abhishek
  • 682
  • 6
  • 17