Pages

Search This Blog

Friday, 27 July 2012

3 SIMPLE STEPS IN ANDROID TO ACCESS CONTACTS

This post helps you to access contacts in android and get the selected phone number from the contacts


Step 1: Create your User Interface using layout file


For this example two controls are required, A EditText field named phoneno to display selected phone number and a Button named Contacts to launch contact picker.
<RelativeLayout
        android:id="@+id/relativeLayout3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <EditText  
        android:layout_height="wrap_content"  
        android:id="@+id/phoneno"  
        android:inputType="text|phone"  
        android:layout_width="wrap_content"  
        android:layout_toLeftOf="@+id/Contacts"  
        android:layout_alignParentLeft="true"></EditText>  
    <Button  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:id="@+id/Contacts"  
        android:text="Contacts"  
        android:layout_alignParentRight="true" 
        android:onClick="doLaunchContactPicker"></Button>  
    </RelativeLayout>

Step 2: doLaunchContactPicker will launch the contacts. Here's code for its implementation
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Contacts;  
[Import statements are must]


private static final int CONTACT_PICKER_RESULT = 1001;
public void doLaunchContactPicker(View view) 
    {  
        Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,Contacts.CONTENT_URI);  
        startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);  
        
    }
 Intent is used along with the startActivityForResult() method to launch another Android application and retrieve the result.  

Step 3: Handle results by implementing onActivityResult()
  1. protected void onActivityResult(int requestCode, int resultCode, Intent data) 
  2.     {  
  3.      String phone="";
  4.      Cursor contacts=null;
  5.      try
  6.      {
  7.         if (resultCode == RESULT_OK) 
  8.         {  
  9.             switch (requestCode) 
  10.             {  
  11.             case CONTACT_PICKER_RESULT:  
  12.                 // gets the uri of selected contact
  13.              Uri result = data.getData();
  14.              // get the contact id from the Uri (last part is contact id) 
  15.              String id = result.getLastPathSegment(); 
  16.                  //queries the contacts DB for phone no
  17.              contacts=getContentResolver().query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + "=?", new String[] { id },  null);
  18.                 //gets index of phone no
  19.              int phoneIdx = contacts.getColumnIndex(Phone.DATA); 
  20.              if (contacts.moveToFirst()) 
  21.              {
  22.                    //gets the phone no  
  23.                    phone = contacts.getString(phoneIdx);  
  24.                    EditText phoneTxt=(EditText)findViewById(R.id.phoneno);
  25.                    //assigns phone no to EditText field phoneno
  26.                    phoneTxt.setText(phone);                   
  27.                 } 
  28.              else 
  29.              {  
  30.              Toast.makeText(this, "error", 100).show();
  31.                 }  
  32.              break;  
  33.             }  
  34.       
  35.         } 
  36.         else 
  37.         {  
  38.             // gracefully handle failure  
  39.             Toast.makeText(this, "Warning: activity result not ok",50).show();  
  40.         }  
  41.     }
  42.      catch (Exception e) 
  43.      {  
  44.         Toast.makeText(this, e.getMessage(), 50).show();  
  45.         } 
  46.      finally 
  47.      {  
  48.         if (contacts != null) 
  49.         {  
  50.             contacts.close();  
  51.         }
  52.      }
  53.     }

2 comments:

  1. hello..i tried this but some error on onActivityResult line...Multiple markers at this line- void is an invalid type for the variable
    onActivityResult

    ReplyDelete
    Replies
    1. This method doesn't return any value. Check your function call. U would have used a variable on the left side of function call to assign the result of the function. If your not able to resolve the issue please post your code.

      Delete