1
Answer

Access COntacts in Android App

How Can I access contacts in my android APplication 
Answers (1)
0
Manikandan Murugesan

Manikandan Murugesan

NA 20.5k 98.8k 7y
use this code to pick contact:
  1. Button button = (Button)findViewById(R.id.pickcontact);  
  2.   
  3.     button.setOnClickListener(new OnClickListener()   
  4.         {  
  5.             @Override  
  6.             public void onClick(View v)   
  7.             {  
  8.                  Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);  
  9.                  startActivityForResult(intent, PICK_CONTACT);  
  10.              }  
  11.          });  
  12.   
  13.   
  14.   
  15.     @Override public void onActivityResult(int reqCode, int resultCode, Intent data){ super.onActivityResult(reqCode, resultCode, data);  
  16.   
  17.     switch(reqCode)  
  18.     {  
  19.        case (PICK_CONTACT):  
  20.          if (resultCode == Activity.RESULT_OK)  
  21.          {  
  22.              Uri contactData = data.getData();  
  23.              Cursor c = managedQuery(contactData, nullnullnullnull);  
  24.           if (c.moveToFirst())  
  25.           {  
  26.           String id = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));  
  27.   
  28.           String hasPhone =  
  29.           c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));  
  30.   
  31.           if (hasPhone.equalsIgnoreCase("1"))   
  32.           {  
  33.          Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,   
  34.           ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,nullnull);  
  35.             phones.moveToFirst();  
  36.             String cNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));  
  37.            // Toast.makeText(getApplicationContext(), cNumber, Toast.LENGTH_SHORT).show();  
  38.             setCn(cNumber);  
  39.           }  
  40.          }  
  41.        }  
  42.     }  
  43.    } 
 and also refer the following Links :
https://developer.android.com/training/contacts-provider/index.html 
 
https://stackoverflow.com/questions/6253029/how-to-get-contacts-from-native-phonebook-in-android
 
https://stackoverflow.com/questions/5662473/how-to-import-contacts-from-phonebook-to-our-application