Friday, February 22, 2013

Android - How to check contact exists in Contact list


To check whether the contact exists in Contact list or not

 public class Conatclist extends Activity {
   @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.conatct);

        //Check if contact is already exists or not in Contact list
        if(contactExists(this,"4561237890"))
        {
            Toast.makeText(GanzfeldActivity.this,"exists",Toast.LENGTH_SHORT).show();
           
        }
        else
        {
            Toast.makeText(GanzfeldActivity.this," not exists",Toast.LENGTH_SHORT).show();
        }

}

public  boolean contactExists(Activity _activity,String number){
      Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,Uri.encode(number));

     String[] mPhoneNumberProjection = { PhoneLookup._ID, PhoneLookup.NUMBER, PhoneLookup.DISPLAY_NAME };

    Cursor cur = _activity.getContentResolver().query(lookupUri,mPhoneNumberProjection, null, null, null);
  try {
        if (cur.moveToFirst()) {
          // if contact are in contact list it will return true
          return true;
       }} finally {
           if (cur != null)
              cur.close();
      }
         //if contact are not match that means contact are not added
        return false;
    }

 }



For that you have give permission to mmanifest file.

 


This code will help you when you are working with contact backup & restore or server side backup & restore.

No comments:

Post a Comment