Showing posts with label contacts. Show all posts
Showing posts with label contacts. Show all posts

Tuesday, April 23, 2013

Android how to use ContactsContract to retrieve phone numbers and email addresses

ContactsContract is the contract between a contacts provider and applications 
that want to use contacts. The following code shows how to find a contact based on 
name, and once it's found, how to retrieve the various types of phone numbers and 
email addresses:
 
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.RawContacts;  
 
String NAME = "Pritom Mondal";
Cursor cursor = managedQuery(ContactsContract.Contacts.CONTENT_URI, null, "DISPLAY_NAME = '" + NAME + "'", null, null);
if (cursor.moveToFirst()) {
 String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
 Cursor phones = managedQuery(Phone.CONTENT_URI, null, Phone.CONTACT_ID + " = " + contactId, null, null);
 while (phones.moveToNext()) {
  String number = phones.getString(phones.getColumnIndex(Phone.NUMBER));
  int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
  System.out.println("TYPE: "+type+" NUMBER: "+number);
 }
 phones.close();
 
 Cursor emails = managedQuery(Email.CONTENT_URI, null, Email.CONTACT_ID + " = " + contactId, null, null);
 while (emails.moveToNext()) {
  String number = emails.getString(emails.getColumnIndex(Email.DATA));
  int type = emails.getInt(emails.getColumnIndex(Phone.TYPE));
  System.out.println("TYPE: "+type+" EMAIL: "+number);
 }
 emails.close();
}
cursor.close();

How to read android sim contacts and phone contacts separately

The <uses-permission> should be contained in the <manifest> element. See Structure of the Manifest File. So trying putting it before <application>.
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
 

try {
    	String[] PROJECTION=new String[] {Contacts._ID,
    	        Contacts.DISPLAY_NAME,
    	        Phone.NUMBER
    	    };
    	Cursor c = managedQuery(Phone.CONTENT_URI, PROJECTION, null, null, null);
    	if (c.moveToFirst()) {
    		String ClsPhonename = null;
            String ClsphoneNo = null;
            do 
            {
                ClsPhonename = c.getString(c.getColumnIndex(Contacts.DISPLAY_NAME));
                ClsphoneNo = c.getString(c.getColumnIndex(Phone.NUMBER));
                System.out.println(c.getString(c.getColumnIndex(Contacts._ID)));

                ClsphoneNo.replaceAll("\\D", "");
                ClsPhonename=ClsPhonename.replaceAll("&", "");
                ClsPhonename.replace("|","");
                String ClsPhoneName=ClsPhonename.replace("|","");
                System.out.println("Name: "+ClsPhoneName);
                System.out.println("Phone: "+ClsphoneNo);
            } while(c.moveToNext());
    	}
    	
    	
    	Uri simUri = Uri.parse("content://icc/adn"); 
        Cursor cursorSim = this.getContentResolver().query(simUri,null,null,null,null);
        String ClsSimPhonename = null; 
        String ClsSimphoneNo = null;
        while (cursorSim.moveToNext()) 
        {      
            ClsSimPhonename =cursorSim.getString(cursorSim.getColumnIndex("name"));
            ClsSimphoneNo = cursorSim.getString(cursorSim.getColumnIndex("number"));
            ClsSimphoneNo.replaceAll("\\D","");
            ClsSimphoneNo.replaceAll("&", "");
            ClsSimPhonename=ClsSimPhonename.replace("|","");
                System.out.println("SimContacts"+ClsSimPhonename);
                System.out.println("SimContactsNo"+ClsSimphoneNo);

        }
    } catch (Exception ex) {
    	System.out.println(ex.getMessage());
    	ex.printStackTrace();
    }