Search This Blog

Thursday, December 1, 2011

How to call Android contacts list?

Hi this is very import concept to Read Existing Contacts from Your Emulator or Device

There are three steps to this process.


1) Permissions
Add a permission to read contacts data to your application manifest. 

Android:name="android.permission.READ_CONTACTS"/>

2) Calling the Contact Picker

Within your Activity, create an Intent that asks the system to find an Activity that can perform a PICK action from the items in the Contacts URI.

Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);

Call startActivityForResult, passing in this Intent (and a request code integer , PICK_CONTACT in this example). This will cause Android to launch an Activity that's registered to support ACTION_PICKon the People.CONTENT_URI, then return to this Activity when the selection is made (or canceled).

startActivityForResult(intent, PICK_CONTACT);

3) Listening for the Result

Also in your Activity, override the onActivityResult method to listen for the return from the 'select a contact' Activity you launched in step 2. You should check that the returned request code matches the value you're expecting, and that the result code is RESULT_OK.

You can get the URI of the selected contact by calling getData() on the data Intent parameter. To get the name of the selected contact you need to use that URI to create a new query and extract the name from the returned cursor.

@Override 
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(); 
                                        Cursor c = managedQuery(contactData, null, null, null, null);
                                        if (c.moveToFirst()) 
                                       { 
                                 String name = c.getString(c.getColumnIndexOrThrow(People.NAME));
                                 // TODO Whatever you want to do with the selected contact name. 
                                      } 
                                 }
 break; 
}

}    
Home Screen If No Contacts Contacts

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.