The Contacts Database is divided into 3 tables which are contacts, raw contacts and data.
⇒ Each table contains column (_ID) which is an auto incremented primary key.
⇒ Data table contains all the contact info like phone number, mail id, address etc.
⇒ User cannot add any data into contacts table. The data in this table is populated internally.
⇒ The RawContacts table contains all the actual contact created .Hence we use the raw contacts while retrieve contacts.
Uri uri=RawContacts.CONTENT_URI;
String projection [] = new String[]{ RawContacts.CONTACT_ID,RawContacts.ACCOUNT_TYPE,RawContacts.DISPLAY_NAME_PRIMARY };
Here RawContacts.CONTACT_ID is the id of the row in ContactsContract.Contacts table that this raw Contact belongs to.
Raw contacts are linked to contacts by the aggregation process which can be controlled by AGGREGATION_MODE field.
String selection=RawContacts.ACCOUNT_TYPE+ “=?”;
String selectionArgs[]=new String[]{“any account name which type contact you want to retrieve like for
retrieve Google contacts you have to specify “com.google”};
String sortOrder=ContactsContract.Display_Name;
Now create cursor using these parameters.
Cursor cursor=getContentResolver.query(uri, projection, selection, selectionArgs, sortOrder);
Now all the requested column and their value in cursor now and we can get those value from the cursor like this.
While (cursor.moveToNext()) {
int contactId=cursor.getInt(cursor.getColumnIndex(RawContacts.CONTACT_ID));
String contactName = cursor.getString(cursor.getColumnIndex(RawContacts.DISPLAY_NAME_PRIMARY);
String accountType = cursor.getString(cursor.getColumnIndex(RawContacts.ACCOUNT_TYPE);
}
So now we get only Google contacts but if we put selectionArgs=null then we get all the existing contact of device.
How to get Sim contact details from android device?
⇒ In this we have to take different Uri to get sim contact detail.
⇒ The name of the column where sim contact name and contact number are exists names are “name” and “number”.
Uri uri=Uri.parse (“content://icc/and”);
String projection [] = null;
String selection = null;
String selectionArgs [] = null;
String sortOrder = null;
Cursor simCursor= getContentResolver.query (uri, projection, selection, selectionArgs, sortOrder);
While (simCursor.moveToNext()) {
String simContactName=simCursor.getString(simCursor.getColumnIndex(“name”));
String simContactNumber=simCursor.getString(simCursor.getColumnIndex(“number”));
simContactNumber.replaceAll (“\\D”,””);
simContactNumber.replaceAll (“&”,””);
}
So now we get sim contact number and contact name.
Get Contacts
0 comments:
Post a Comment