I am a beginner to android, i am building a application in which when the user presses a button, the contacts which is stored in the mobile are shown. When he selects a cont
public void readcontact(){
try {
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
PhoneLookup.CONTENT_FILTER_URI); // creates the contact list intent
contactPickerIntent.setType(Contacts.Phones.CONTENT_TYPE); // selects contacts with phone only
startActivityForResult(contactPickerIntent, PICK_CONTACT);
} catch (Exception e) {
e.printStackTrace();
}
}
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(); // has the uri for picked contact
Cursor c = getContentResolver().query(contactData, null, null, null, null); // creates the contact cursor with the returned uri
if (c.moveToFirst()) {
String name = c..getString(c.getColumnIndex(PhoneLookup.DISPLAY_NAME));
String number = c.getString(c.getColumnIndex(PhoneLookup.NUMBER));
Toast.makeText(this, name + " has number " + number, Toast.LENGTH_LONG).show();
}
}
break;
}
}