1

I have registred an intent in AndroidManifest file, to make my app, pops up when the user selected a contact in contacts app, and i succeeded in retreiving the look up key, but i can't found a way to get the contact selected details, i have looked at some others using cursor, but when the i execute the query function my app crashes, this is my code retreiving the look up key

    Intent intent = getIntent();
    String action = intent.getAction();
    Uri contactUri = intent.getData();
    if (Intent.ACTION_VIEW.equals(action)) {
        // here i don't know what to use to search for the contact by the contactUri
    }

So, i have to get Contact Name, and the Contact Number !

Iliyass Hamza
  • 1,355
  • 3
  • 15
  • 28
  • http://stackoverflow.com/questions/14069375/get-specific-contact-information-from-uri-returned-from-intent-action-pick – denvercoder9 Apr 14 '15 at 09:59

1 Answers1

0
Intent intent = getIntent();
String action = intent.getAction();
Uri contactUri = intent.getData();
if (Intent.ACTION_VIEW.equals(action)) {

       Log.d("START","Getting all Contacts");
       ArrayList<PhoneContactInfo> arrContacts = new ArrayList<PhoneContactInfo>();
       PhoneContactInfo phoneContactInfo=null;    
       Cursor cursor = context.getContentResolver().query(contactUri , new String[] {ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone._ID}, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
       cursor.moveToFirst();
       while (cursor.isAfterLast() == false)
         {
           String contactNumber= cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));  
           String contactName =  cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
          int phoneContactID = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));


          phoneContactInfo = new PhoneContactInfo();
          phoneContactInfo.setPhoneContactID(phoneContactID);             
          phoneContactInfo.setContactName(contactName);                   
        phoneContactInfo.setContactNumber(contactNumber); 
        if (phoneContactInfo != null)
        {
             arrContacts.add(phoneContactInfo);
         }
       phoneContactInfo = null; 
       cursor.moveToNext();
}       
cursor.close();
cursor = null;
Log.d("END","Got all Contacts");

}
Zied R.
  • 4,964
  • 2
  • 36
  • 67