Wednesday, July 7, 2010

How to fetch Contacts List name and number in android?

//=================main.java=========================

private static final int PICK_CONTACT_REQUEST = 1;

private final ContactAccessor mContactAccessor = ContactAccessor
.getInstance();
Boolean select;

// any click event
public void onClick(View v) {
select = true;
pickContact();
}

protected void pickContact() {
startActivityForResult(mContactAccessor.getPickContactIntent(),
PICK_CONTACT_REQUEST);

}


private void loadContactInfo(Uri contactUri) {

AsyncTask task = new AsyncTask() {

@Override
protected ContactInfo doInBackground(Uri... uris) {
return mContactAccessor.loadContact(getContentResolver(),
uris[0]);
}

@Override
protected void onPostExecute(ContactInfo result) {
bindView(result);
}
};

task.execute(contactUri);
}

protected void bindView(ContactInfo contactInfo) {
if (select == true)
{
EditText Name = (EditText) findViewById(R.id.EditText01);
Name.setText(contactInfo.getDisplayName());

EditText Number = (EditText) findViewById(R.id.EditText02);
Number.setText(contactInfo.getPhoneNumber());
}

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_CONTACT_REQUEST && resultCode == RESULT_OK) {
loadContactInfo(data.getData());
}
}


//=====================ContactAccessor.java===============
public abstract class ContactAccessor {

private static ContactAccessor sInstance;

public static ContactAccessor getInstance() {
if (sInstance == null) {
String className;

int sdkVersion = Integer.parseInt(Build.VERSION.SDK); // Cupcake
// style

if (sdkVersion < Build.VERSION_CODES.ECLAIR) { className = "android.contacts.ContactAccessorSdk3_4"; } else { className = "android.contacts.ContactAccessorSdk5"; } try { Class clazz = Class.forName(
className).asSubclass(ContactAccessor.class);
sInstance = clazz.newInstance();
} catch (Exception e) {
throw new IllegalStateException(e);
}
}

return sInstance;
}

public abstract Intent getPickContactIntent();

public abstract ContactInfo loadContact(ContentResolver contentResolver,
Uri contactUri);
}

//=======================ContactAccessorSdk3_4.java==================
public class ContactAccessorSdk3_4 extends ContactAccessor {

/**
* Returns a Pick Contact intent using the pre-Eclair "people" URI.
*/
@Override
public Intent getPickContactIntent() {
return new Intent(Intent.ACTION_PICK, People.CONTENT_URI);
}

/**
* Retrieves the contact information.
*/
@Override
public ContactInfo loadContact(ContentResolver contentResolver, Uri contactUri) {
ContactInfo contactInfo = new ContactInfo();
Cursor cursor = contentResolver.query(contactUri,
new String[]{People.DISPLAY_NAME}, null, null, null);
try {
if (cursor.moveToFirst()) {
contactInfo.setDisplayName(cursor.getString(0));
}
} finally {
cursor.close();
}

Uri phoneUri = Uri.withAppendedPath(contactUri, Phones.CONTENT_DIRECTORY);
cursor = contentResolver.query(phoneUri,
new String[]{Phones.NUMBER}, null, null, Phones.ISPRIMARY + " DESC");

try {
if (cursor.moveToFirst()) {
contactInfo.setPhoneNumber(cursor.getString(0));
}
} finally {
cursor.close();
}

return contactInfo;
}
}

//========================ContactAccessorSdk5.java==============================
public class ContactAccessorSdk5 extends ContactAccessor {

/**
* Returns a Pick Contact intent using the Eclair "contacts" URI.
*/
@Override
public Intent getPickContactIntent() {
return new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
}

/**
* Retrieves the contact information.
*/
@Override
public ContactInfo loadContact(ContentResolver contentResolver, Uri contactUri) {
ContactInfo contactInfo = new ContactInfo();
long contactId = -1;

// Load the display name for the specified person
Cursor cursor = contentResolver.query(contactUri,
new String[]{Contacts._ID, Contacts.DISPLAY_NAME}, null, null, null);
try {
if (cursor.moveToFirst()) {
contactId = cursor.getLong(0);
contactInfo.setDisplayName(cursor.getString(1));
}
} finally {
cursor.close();
}

// Load the phone number (if any).
cursor = contentResolver.query(Phone.CONTENT_URI,
new String[]{Phone.NUMBER},
Phone.CONTACT_ID + "=" + contactId, null, Phone.IS_SUPER_PRIMARY + " DESC");
try {
if (cursor.moveToFirst()) {
contactInfo.setPhoneNumber(cursor.getString(0));
}
} finally {
cursor.close();
}

return contactInfo;
}
}
//======================ContactInfo.java===============================
public class ContactInfo {

private String mDisplayName;
private String mPhoneNumber;

public void setDisplayName(String displayName) {
this.mDisplayName = displayName;
}

public String getDisplayName() {
return mDisplayName;
}

public void setPhoneNumber(String phoneNumber) {
this.mPhoneNumber = phoneNumber;
}

public String getPhoneNumber() {
return mPhoneNumber;
}
}

1 comment:

  1. Using this Code u can get or fetch contact info of your phone and display in edit text view.

    if any query please send comment..........

    ReplyDelete