Android get phone number from contacts list

When you create an application to send sms or an application to make calls, getting a destination number from the contacts list is a common task. In this Android tip, I am going to show the code to fetch a number from the contacts list. Now let me tell you how to achieve the goal. First, you need to create an Intent object for the PICK_ACTION action. To open the contacts list, the table that contains the contacts information must be specified as a parameter of the constructor of the Intent class. You can refer to the table using ContactsContract.Contacts.CONTENT_URI. Then call the startActivityForResult () method passing the Intent object and request code to open the contacts list.



After a contact is selected from the contacts list, to get the result, you need to override the onActivityResult(int reqCode, int resultCode, Intent data) method of the activity. You can call the getData() method of the data parameter to get the table or uri that contains the selected contact. From the table, you will use query() method of ContentResolver to fetch the contact id. When you obtain the contact id, you can query the ContactsContract.CommonDataKinds.Phone table to retrieve the phone number for the selected contact.
Now to have an example application on getting phone number from contacts list, you create a new Android project. Then in activity_main.xml file, you add a Button.


get phone number from contacts list


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
  >
   <Button
      android:id="@+id/btpick_contact"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Pick number from contacts list"
      android:layout_gravity="center"
      >
   </Button>
</LinearLayout>

Here is the code written in the MainActivity class to open the contacts list and display phone number of the contact in Toast message.
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.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {  
   private final int REQUEST_CODE=99;
   protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     Button btPick=(Button)findViewById(R.id.btpick_contact);
     btPick.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
             Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
             startActivityForResult(intent, REQUEST_CODE);
        }
     });
     }
   @Override
   public void onActivityResult(int reqCode, int resultCode, Intent data) {
     super.onActivityResult(reqCode, resultCode, data);
     switch (reqCode) {
        case (REQUEST_CODE):
         if (resultCode == Activity.RESULT_OK) {
            Uri contactData = data.getData();
             Cursor c = getContentResolver().query(contactData, null, null, null, null);
             if (c.moveToFirst()) {
                String contactId = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
                String hasNumber = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
                String num = "";
                if (Integer.valueOf(hasNumber) == 1) {
                   Cursor numbers = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
                   while (numbers.moveToNext()) {
                     num = numbers.getString(numbers.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                     Toast.makeText(MainActivity.this, "Number="+num, Toast.LENGTH_LONG).show();
                 }
                }
            }
            break;
           }
     }
   }
}



Posted by: Dara | post date: 03-26-2015 | Subject: Android Apps Development

write programming tip