How To Get Contact Name, Number And Emai Id From A List Of Contacts?
I am new to android ,I need to get the details of my contacts, but the details include only 3 Contact name contact number and Email ID when I press a Button it will show these
Solution 1:
By below code you can do that -
publicvoiddoLaunchContactPicker(View view) {
IntentcontactPickerIntent=newIntent(Intent.ACTION_PICK,Contacts.CONTENT_URI);
startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);
}
@OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK) {
switch (requestCode)
{
case CONTACT_PICKER_RESULT:
Cursorcursor=null;
Stringemail="", name = "";
try {
Uriresult= data.getData();
Log.v(DEBUG_TAG, "Got a contact result: " + result.toString());
// get the contact id from the UriStringid= result.getLastPathSegment();
// query for everything email
cursor = getContentResolver().query(Email.CONTENT_URI, null, Email.CONTACT_ID + "=?", newString[] { id }, null);
intnameId= cursor.getColumnIndex(Contacts.DISPLAY_NAME);
intemailIdx= cursor.getColumnIndex(Email.DATA);
// let's just get the first emailif (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();
}
EditTextemailEntry= (EditText) findViewById(R.id.editTextv);
EditTextpersonEntry= (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 -
Don't forget to add the required permission -
<uses-permissionandroid:name="android.permission.READ_CONTACTS"/>
in your AndroidManifest.xml
file. And, Just modify this code with your needs.
Solution 2:
You can access addressbook like this;
ContentResolvercr= getContentResolver();
Cursorcur= cr.query(ContactsContract.Contacts.CONTENT_URI, null,null, null,ContactsContract.Contacts.DISPLAY_NAME);
intkisiSayisi= cur.getCount();
if(kisiSayisi > 0)
{
intKisiIndex=0;
while(cur.moveToNext())
{
Stringid= cur.getString(cur.getColumnIndex(BaseColumns._ID));
Stringname= cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
CursorpCur= cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ " = ?", newString[] { id }, null);
while (pCur.moveToNext())
{
Stringphone= pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
//String phoneType = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));StringdogruGSM= gsmNoKontrol(phone);
if(dogruGSM.compareTo("0") != 0){
Kisikisi=newKisi(KisiIndex, name, dogruGSM, false);
MyList.add(kisi);
KisiIndex ++;
}
}
pCur.close();
}
}
}
Post a Comment for "How To Get Contact Name, Number And Emai Id From A List Of Contacts?"