- The query used for reading the content is as following-
- The first parameter is the uri for the action we are doing. Other parameters are for filtering the selection of the contacts. For now, we will keep all of them to null. The only thing that is mandatory is the uri. The above statement returns a Cursor.
- First of all, import the following packages-
- The URI for reading phone contacts will be obtained by Phone.CONTENT_URI. So use it in the query() and save the result of query in a cursor.
- Now the cursor has all information of all phone contacts. Start a while loop with moving the cursor to the next position. The loop will be ended when there will be no item left in the cursor.
- For retrieving the information from the cursor, use getColumnIndex() method of the class Cursor. Cursor is a kind of table that has different contacts in different rows and their different information in different columns.
- Above statement will retrieve the name of the content at which the cur is pointing at that time and store it in a String variable name.
- Similarly Phone number can be accessed using-
- Thus the code becomes-
- In this way different information can be retrieved. Now you can use this information as you want.
- Give the following permission in AndroidManifest.xml-