How can you get the contact's data that opened your app from the QuickContact dialog? I'm developing a SMS app and I want to fill the recipient's field with this contact's number when the app is launched.
Asked
Active
Viewed 358 times
2 Answers
0
you can have all data from your phone book in your own list view so that you can select contact from list and it will directly appear into recipients field.
here is code to retrieve all contact details into you own list view as follows :
public class ContactListDemo extends ListActivity implements Runnable{
private List<Contact> contacts = null;
private Contact con;
private ContactArrayAdapter cAdapter;
private ProgressDialog prog = null;
private Context thisContext = this;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
prog = ProgressDialog.show(this, "ContactListDemo", "Getting Contacts", true, false);
Thread thread = new Thread(this);
thread.start();
}
public void run() {
if (contacts == null)
{
contacts = fillContactsList();
}
handler.sendEmptyMessage(0);
}
private List<Contact> fillContactsList() {
List<Contact> tmpList = new ArrayList<Contact>();
Cursor c = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while(c.moveToNext()){
String ContactID = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String hasPhone =c.getString(
c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if(Integer.parseInt(hasPhone) == 1){
Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID+ " = " + ContactID, null, null);
while (emails.moveToNext())
{
// This would allow you get several email addresses
String emailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
con = new Contact();
con.setName(name);
con.setEmail(emailAddress);
tmpList.add(con);
emails.close();
}
}
}
c.close();
Collections.sort(tmpList);
return tmpList;
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
prog.dismiss();
cAdapter = new ContactArrayAdapter(thisContext, R.layout.listitemlayout, contacts);
cAdapter.setNotifyOnChange(true);
//ContactArrayAdapter(thisContext, android.R.layout.simple_list_item_multiple_choice, contacts);
getListView().setFastScrollEnabled(true);
setListAdapter(cAdapter);
}
};
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
TextView label = ((TwoLineListItem) v).getText2();
TextView label1 = ((TwoLineListItem) v).getText1();
String phoneNumber = label.getText().toString();
String name = label1.getText().toString();
// int phoneNumber1 = label.getId();
Toast.makeText(this, "Selected " + name +"id" + phoneNumber, Toast.LENGTH_SHORT).show();
}

OnkarDhane
- 1,450
- 2
- 14
- 24
-
sorry if i'm not being clear. I'm trying to get the individual's number that launched the app from the QuickContact dialog. just like how the default SMS application fills the recipient field with the contact name and number that launched the compose message from the QuickContact dialog. Cheers for the code though. – jakedemus Feb 02 '12 at 06:20
-
hi smit, why 'ContactsContract.Contacts.CONTENT_URI' is not working in my application?? – Feb 02 '12 at 06:21
-
1And surely ensure that you have added android.permission.READ_CONTACTS to your AndroidManifest.xml @Swan – OnkarDhane Feb 02 '12 at 06:23