Skip to content

Instantly share code, notes, and snippets.

@dmitriy-chernysh
Created January 5, 2015 15:49
Show Gist options
  • Save dmitriy-chernysh/221e104ba9bcb6b7774c to your computer and use it in GitHub Desktop.
Save dmitriy-chernysh/221e104ba9bcb6b7774c to your computer and use it in GitHub Desktop.
Pick contact`s phone number (for Android)
public class PickPhoneNumberActivity extends ActionBarActivity {
final int PICK_CONTACT_REQUEST = 1;
//some code
.....
private void pickContact() {
Intent pickContact = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Check which request it is that we're responding to
if (requestCode == PICK_CONTACT_REQUEST) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// Get the URI that points to the selected contact
Uri contactUri = data.getData();
// We only need the NUMBER column, because there will be only one row in the result
String[] projection = {Phone.NUMBER};
// Perform the query on the contact to get the NUMBER column
// We don't need a selection or sort order (there's only one result for the given URI)
// CAUTION: The query() method should be called from a separate thread to avoid blocking
// your app's UI thread. (For simplicity of the sample, this code doesn't do that.)
// Consider using CursorLoader to perform the query.
Cursor cursor = getContentResolver().query(contactUri, projection, null, null, null);
cursor.moveToFirst();
// Retrieve the phone number from the NUMBER column
int columnNumber = cursor.getColumnIndex(Phone.NUMBER);
String phoneNumber = cursor.getString(columnNumber);
// Do something with the phone number...
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment