0

如何从 QuickContact 对话框中获取打开您的应用程序的联系人数据?我正在开发一个短信应用程序,我想在应用程序启动时用这个联系人的号码填写收件人的字段。

4

2 回答 2

0

您可以在自己的列表视图中拥有电话簿中的所有数据,以便您可以从列表中选择联系人,它将直接显示在收件人字段中。

这是将所有联系方式检索到您自己的列表视图中的代码,如下所示:

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();
}
于 2012-02-02T06:07:14.397 回答
0

对其进行排序。
要获取联系人的号码,请使用:

String launchContactNo = getIntent().getDataString();

这将返回:

 smsto:5556

(这是联系人的号码)

于 2012-02-02T17:51:45.923 回答