I got a code from internet, the code below is picking the contact and displaying it.
I want to display the Name and Phone no: of the person in next ACTIVITY. Could u please help me with this….
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.TextView;
public class Contacts_PickerActivity extends Activity
{
private TextView tv;
private static final int CONTACT_PICKER = 1;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
tv = new TextView(this);
setContentView(tv);
Intent intent = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, CONTACT_PICKER);
}
public void onActivityResult(int reqCode, int resultCode, Intent data)
{
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode)
{
case CONTACT_PICKER:
if (resultCode == Activity.RESULT_OK)
{
StringBuilder sb = new StringBuilder();
Uri contactData = data.getData();
Cursor contactsCursor = managedQuery(contactData, null, null, null, null);
if (contactsCursor.moveToFirst())
{
String id = contactsCursor.getString(contactsCursor
.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
String name = contactsCursor.getString(contactsCursor
.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
String hasPhoneNumber = contactsCursor.getString(contactsCursor
.getColumnIndexOrThrow(ContactsContract.Contacts.HAS_PHONE_NUMBER));
sb.append("You picked: " + name + "\n");
if (Integer.parseInt(hasPhoneNumber) > 0)
{
Uri myPhoneUri = Uri.withAppendedPath(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, id);
Cursor phoneCursor = managedQuery(
myPhoneUri, null, null, null, null);
for (phoneCursor.moveToFirst(); !phoneCursor.isAfterLast(); phoneCursor.moveToNext())
{
String phoneNumber = phoneCursor.getString(phoneCursor
.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
sb.append("Phone: " + phoneNumber + "\n");
}
}
else
{
sb.append("This contact doesn't have a phone number");
}
tv.setText(sb.toString());
}
}
break;
}
}
}
In the onActivityResult method, the name and phone number are pulled from the Intent data as Strings. You can pass them on to the next Activity as extras on the Intent you create to launch the Activity.