Friday, November 23, 2012

Get phone number from Contacts database

In the exercise, start a activity with intent of Intent.ACTION_PICK. Retrieve the phone number from the selected contact using SQLite query.

Get phone number from Contacts database


package com.example.androidreadcontact;

import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;

public class MainActivity extends Activity {
 
 Button buttonReadContact;
 TextView textPhone;
 
 final int RQS_PICKCONTACT = 1;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  buttonReadContact = (Button)findViewById(R.id.readcontact);
  textPhone = (TextView)findViewById(R.id.phone);
  
  buttonReadContact.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View arg0) {
    //Start activity to get contact
    final Uri uriContact = ContactsContract.Contacts.CONTENT_URI;
    Intent intentPickContact = new Intent(Intent.ACTION_PICK, uriContact);
    startActivityForResult(intentPickContact, RQS_PICKCONTACT);
   }});

 }

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  // TODO Auto-generated method stub
  if(resultCode == RESULT_OK){
   if(requestCode == RQS_PICKCONTACT){
    Uri returnUri = data.getData();
    Cursor cursor = getContentResolver().query(returnUri, null, null, null, null);
    
    if(cursor.moveToNext()){
     int columnIndex_ID = cursor.getColumnIndex(ContactsContract.Contacts._ID);
     String contactID = cursor.getString(columnIndex_ID);
     
     int columnIndex_HASPHONENUMBER = cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
     String stringHasPhoneNumber = cursor.getString(columnIndex_HASPHONENUMBER);
     
     if(stringHasPhoneNumber.equalsIgnoreCase("1")){
      Cursor cursorNum = getContentResolver().query(
        ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
        null, 
        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactID, 
        null, 
        null);
      
      //Get the first phone number
      if(cursorNum.moveToNext()){
       int columnIndex_number = cursorNum.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
       String stringNumber = cursorNum.getString(columnIndex_number);
       textPhone.setText(stringNumber);
      }
      
     }else{
      textPhone.setText("NO Phone Number");
     }
     
     
    }else{
     Toast.makeText(getApplicationContext(), "NO data!", Toast.LENGTH_LONG).show();
    }
   }
  }
 }
 
 

}


Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" 
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/readcontact"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:text="Read Contact"/>
    <TextView
        android:id="@+id/phone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>


Permission of "android.permission.READ_CONTACTS" is needed in AndroidManifest.xml.

download filesDownload the files.

10 comments:

Anonymous said...

Thank you very much! I searched for this on Google and only you can tell me the most correct and easy answer.

Anonymous said...

great work.searching exactly this one :)

Unknown said...

did not work , forced close my app ... what is wrong ?

Erik said...

hello Mziad Tariq,

I have re-tried the example, http://android-er.blogspot.com/2014/11/get-phone-number-from-contacts-database.html, found no problem.

Make sure permission of "android.permission.READ_CONTACTS" is add in AndroidManifest.xml.

Anonymous said...

Hello Eric! Would it be okay if we use/credit your code in our app? Thanks in advance

Erik said...

hello "Anonymous",

Yes, you can and welcome.

Anonymous said...

hello sir, how about selecting two or more contacts and displaying them?

rocksivia said...

thanxxxxx very much..... worked fine!!!

Anonymous said...

thanks a lot man, you save me few hours.

Unknown said...

Hey! nice code! but what to do if we want the name of the contact selected ?