Write a program showing all the contacts in Android Programming

0
1784

Android programming 5Explanation: The first thing to notice is that here we are going to use the phone’s data and for that we have to take the permission by using some of the comments which will be explained further.

Now all these steps will be explained one by one…

Just like all project here also we will name the project first and then give a package name to it.
Lets now create the project.

First of all name the project and the screen will look like this.

Documentation contacts Android

After that we select the minimum sdk and the target sdk. Minimum sdk should be selected as the oldest version available in the version of eclipse available to you and the Target sdk must be selected as the latest version available to you.

Documentation contacts Android2

Then name the package

Android contacts documentation3

Code:
[sourcecode lang=”java”]
public class ContactsActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// As usual here the contentview is set to main i.e. the view will look like the one we have set for the main layout file.
TextView contactView = (TextView) findViewById(R.id.contactview);
// Here the textview is initialized and and associated with the layout file using the findViewById
Cursor cursor = getContacts();
// Here we have used a Cursor which is used to get a reply to the query which we are going to make later on in the code and the name of the code is set to cursor here and here we have to tell that where the cursor should move and we have told it to move through the contacts using the getcontacts();

while (cursor.moveToNext()) {

String displayName = cursor.getString(cursor
.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
// DISPLAY_NAME is used to get the name of the contact from the phone book of phone
contactView.append(displayName);
// the above code is used to add the display name to the contactview
}
}
// In the above used snippet we have used while to tell that the code in the braces will run till the time the cursor moves on to the next column in the data base

private Cursor getContacts() {
// query
Uri uri = ContactsContract.Contacts.CONTENT_URI;
// Uri is used as an id and here we have to tell what type of data we have to search for
//contacts information is permitted through this and .contacts after this is used for
//raw contacts information(names which have just one phone number are called raw contacts) and the Content_uri is
//used for checking the contents in the contact.
String[] search = new String[] { BaseColumns._ID, ContactsContract.Contacts.DISPLAY_NAME };

//This string of array is used to tell which columns of data base has to be searched
// if null is passed this means all the columns have to be searched and whole phone book
// is searched as a result
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + "="
+ ("1") + "’";
// selection tells which rows to be searched
String[] selectionArgs = null;
// if something is used in the selectionArgs than anything in the selection will be
// replaced by that string
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME;
//sort order tells us that in which order the columns should be searched
return managedQuery(uri,search, selection, selectionArgs,sortOrder);
// now it will return all what we asked for using the search,selectionArgs,selection and the sortorder.

}
}
[/sourcecode]

But as told earlier that we also have to take the permission. We will do that by opening the manifest file from the tab on the left of eclipse and after that in the manifest file go to the permission tab. After going to permission tab. Click add and then click uses permission

Android courses 4

As you click on that you will get the pop up menu as shown in the figure

Android programming 5

Doing this we will get the permission to access the data from the phone storage.
After doing this we will notice a change in the AndroidManifest.xml file as shown below.

Contacts Documentation Android 6

Summary: To read the data from the phone we will have to take the permission from the content provider.