【问题标题】:how can i get phone number of selected contact如何获取所选联系人的电话号码
【发布时间】:2015-05-01 15:10:32
【问题描述】:
c.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Intent i = new Intent(ContactsContract.Contacts.CONTENT_TYPE);
            Intent i = new Intent(Intent.ACTION_PICK);
            i.setType(ContactsContract.Contacts.CONTENT_TYPE);
            startActivityForResult(i, CONTACT_PICKER_RESULT); }
        });
}
public void onActivityResult(int requestCode ,int resultCode ,Intent data) {
    if ((requestCode == CONTACT_PICKER_RESULT) && (resultCode == RESULT_OK)) {

    }

}

此代码包含打开联系人的意图,但我需要如果选择联系人获取联系人电话号码,然后将其设置为字符串

【问题讨论】:

    标签: android android-contacts


    【解决方案1】:

    检查此解决方案:How to call Android contacts list? 它几乎是您需要做的,但您应该使用光标搜索数字。

    PS:下次搜索更好,有很多人问。

    【讨论】:

      【解决方案2】:

      你可以试试这个。

      @Override
      public void onActivityResult(int reqCode, int resultCode, Intent data){
          super.onActivityResult(reqCode, resultCode, data);
      
          if(reqCode == this.pickContact){
              if (resultCode == Activity.RESULT_OK) {
                  Log.d("ContactsH", "ResOK");
                  Uri contactData = data.getData();
                  Cursor contact =  getContentResolver().query(contactData, null, null, null, null);
      
                  if (contact.moveToFirst()) {
                      String name = contact.getString(contact.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                      // TODO Whatever you want to do with the selected contact's name.
      
                      ContentResolver cr = getContentResolver();
                      Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
                              "DISPLAY_NAME = '" + name + "'", null, null);
                      if (cursor.moveToFirst()) {
                          String contactId =
                                  cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                          //
                          //  Get all phone numbers.
                          //
                          Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                                  ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
                          while (phones.moveToNext()) {
                              String number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                              int type = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                              switch (type) {
                                  case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
                                      // do something with the Home number here...
                                      break;
                                  case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
                                      // do something with the Mobile number here...
                                      Log.d("ContactsH", number);
                                      break;
                                  case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
                                      // do something with the Work number here...
                                      break;
                              }
                          }
                          phones.close();
                      }
                      cursor.close();
                  }
              }
          }else{
              Log.d("ContactsH", "Canceled");
          }
      }
      

      这对我有用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-12-26
        • 1970-01-01
        • 1970-01-01
        • 2015-01-24
        • 1970-01-01
        • 2013-02-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多