如何构建一个显示我的联系人电话号码的应用程序

时间:2015-09-26 17:42:01

标签: android android-studio android-contacts

我最近建立了一个显示我的联系人姓名的应用程序,但问题是我想显示他们的电话号码而不是他们的名字。以下是我的代码,它精确地显示了联系人姓名

   public class Main extends ActionBarActivity implements View.OnClickListener {
TextView tv ;
Button Action ;
String name ;
 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
     tv = (TextView) findViewById(R.id.textView) ;
     Action = (Button) findViewById(R.id.button) ;
     Action.setOnClickListener(this );
 }

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case (0) :
            if (resultCode == Activity.RESULT_OK) {
                Uri contactData = data.getData();
                Cursor c =  getContentResolver().query(contactData,
                        null,null
                        ,null, null);
                if (c.moveToFirst()) {
                    name = "" ;
                    name = c.getString(c.getColumnIndex(ContactsContract
                            .Contacts.DISPLAY_NAME));
                    // TODO Whatever you want to do with the selected contact name.
                    tv.setText("");
                    tv.setText(name);
                }
            }
            break;
    }

}

2 个答案:

答案 0 :(得分:0)

一个联系人可以有多个语音,因此我们必须在光标中获取电话号码然后重复它。所以编辑你的代码就像下面一样可以工作

if (resultCode == Activity.RESULT_OK) {
  Uri contactData = data.getData();
  Cursor c =  getContentResolver().query(contactData,
                    null,null
                    ,null, null);    
   if (c.moveToFirst()) {

      String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
      String id =c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
      String hasPhone = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

      if (hasPhone.equalsIgnoreCase("1")) {

             Cursor phones = getContentResolver().query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + id,
                        null, null);
             phones.moveToFirst();
             String phoneNumber = phones.getString(phones.getColumnIndex("data1"));
             System.out.println("number is:" + cNumber);
       }

        Log.d(this.getClass().getName(), "Selected " + name);
   }
}

答案 1 :(得分:0)

我已经找到了从Android手机中检索最快的解决方案。 ContactDTO是pojo类。

它将按照"联系人显示名称"。

的升序返回结果
    public static List<ContactDTO> getPhone(Context context){
            List<ContactDTO> contactList = new ArrayList<ContactDTO>();
            ContentResolver cr = context.getContentResolver(); 
            String[] PROJECTION = new String[] { 
                    ContactsContract.RawContacts._ID, 
                    ContactsContract.Contacts.DISPLAY_NAME,
                    ContactsContract.CommonDataKinds.Phone.PHOTO_URI,
                    ContactsContract.CommonDataKinds.Phone.NUMBER,
                    ContactsContract.CommonDataKinds.Photo.CONTACT_ID };

            Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
            String filter = ""+ ContactsContract.Contacts.HAS_PHONE_NUMBER + " > 0 and " + Phone.TYPE +"=" + Phone.TYPE_MOBILE;     
            String order = ContactsContract.Contacts.DISPLAY_NAME + " ASC";// LIMIT " + limit + " offset " + lastId + "";

            Cursor phoneCur = cr.query(uri, PROJECTION, filter, null, order);
            while(phoneCur.moveToNext()){
                 ContactDTO dto = new ContactDTO();
                 dto.setName("" + phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
                 dto.setMobileNo("" + phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
                 dto.setPhotoUrl("" + phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI)));
                 dto.setContactId("" + phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Photo.CONTACT_ID)));
                 contactList.add(dto);
             }
             phoneCur.close();
            return contactList;
    }