title: Attempting to get contact _ID using email address
body: <p>I know that with aggregation contact _ids are not static, but may change over time (see: <a href="https://groups.google.com/forum/?fromgroups=#!topic/android-developers/zS3Ipmf-lsk" rel="nofollow">Google Group Discussion</a>).</p>
  
  <p>So, in my app I am storing the content lookup key and the current id of a selected contact. </p>
  
  <p>What I am trying to do is this: when a message comes in from a particular email address, query the phone contacts and see if that from email address is associated with any of them, and then compare that contact with my stored contact, first doing a fresh query using my stored contact _id and lookup key to get the current contact _id.</p>
  
  <p>Here is how I am attempting to find the contact _id using the email address:</p>
  
  <pre><code>String[] projection = {
      ContactsContract.Contacts.LOOKUP_KEY,
      ContactsContract.Contacts._ID
  }
  
  uri = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Email.CONTENT_LOOKUP_URI, Uri.encode(data)); //data = email address
  cur = resolver.query(uri, projection, null, null, null);
  
  if (cur != null){
      while (cur.moveToNext()){
          msg.set_contact(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)), cur.getLong(cur.getColumnIndex(ContactsContract.Contacts._ID)));
      }
  }
  </code></pre>
  
  <p>The above code works in that it finds that the email address is associated with a contact on the phone.</p>
  
  <p>The problem: I think I am getting the _id of the raw email address entry, not the _id of the overall contact. Because when I refresh my stored contact _id and compare it with what is returned from the above they don't match, even though I have confirmed that the email is associated with my stored contact.</p>
  
  <p>Here is how I am looking up my stored contact:</p>
  
  <pre><code>String[] projection = {
      ContactsContract.Contacts.LOOKUP_KEY,
      ContactsContract.Contacts._ID
  }
  
  Uri uri = ContactsContract.Contacts.getLookupUri(contact_id, contact_lookup);
  
  Cursor cur = resolver.query(cLookupUri, projection, null, null, null);
  if(cur != null){
      while (cur.moveToNext()){
          msg.set_contact(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)), cur.getLong(cur.getColumnIndex(ContactsContract.Contacts._ID)));
      }
  }
  </code></pre>
  
  <p>What am I doing wrong? Basically I just want to get the contact_id of any contact associated with the incoming email address...</p>
  
  <p>Any help is much appreciated!</p>