Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pick contact number from phone-book of android in to my application?

Tags:

android

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--

like image 553
swan Avatar asked Nov 25 '25 09:11

swan


2 Answers

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"));
          }
            }
         }
    }
like image 143
Niranj Patel Avatar answered Nov 26 '25 22:11

Niranj Patel


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

like image 30
eclectronica Avatar answered Nov 26 '25 22:11

eclectronica



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!