【问题标题】:access the contact number of a contact访问联系人的联系电话
【发布时间】:2011-07-11 02:35:44
【问题描述】:

我无法访问联系人的联系电话。 我从网上得到这个代码

Intent intent = new Intent(Intent.ACTION_PICK);           
            intent.setType(ContactsContract.Contacts.CONTENT_TYPE);           
            startActivityForResult(intent, PICK_CONTACT);           
            break;      
            }  
        }



public void onActivityResult(int requestCode, int resultCode, Intent intent)  {    
    if (requestCode == PICK_CONTACT)   
    {                
        Cursor cursor =  managedQuery(intent.getData(), null, null, null, null);       
        cursor.moveToNext();       
        String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));        
        String  name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));         
        Toast.makeText(this, "Contect LIST  =  "+name, Toast.LENGTH_LONG).show();    

    } 

但它只显示联系人的姓名。 我想要联系人的电话号码。

【问题讨论】:

    标签: android contact phone-number


    【解决方案1】:

    查看这篇文章,它有您正在寻找的答案:Read all contact's phone numbers in android

    【讨论】:

    • 上面的代码 sn-p 处理 Intent 而您发布的链接描述了有关使用 api 获取联系方式的详细信息。他们完全不同!
    • 两者都使用从联系人查询返回的光标。您是否暗示由于查询是在 onActvityResult 中进行的,所以 Contact 的属性发生了变化?
    • 谢谢!该链接也帮助我了解了其他一些事情。 :)
    【解决方案2】:

    修改你的代码sn-p

        Intent intent = new Intent(Intent.ACTION_PICK);                          
        intent.setType(ContactsContract.Contacts._ID);                          
        startActivityForResult(intent, PICK_CONTACT);                          
        break;                     
                }             
                } 
         public void onActivityResult(int requestCode, int resultCode, Intent intent)  
        {          if (requestCode == PICK_CONTACT)         
        { Cursor cursor =  managedQuery(Email.CONTENT_URI, null, Email.CONTACT_ID +  " = " + intent.getData(), null, null);                 
    cursor.moveToNext();                 
        String contactId = cursor.getString(cursor.getColumnIndex(Email._ID));
        String  name = cursor.getString(cursor.getColumnIndexOrThrow(Email.DATA1));                   
        Toast.makeText(this, "Contect LIST  =  "+name, Toast.LENGTH_LONG).show();            
        }  
    

    【讨论】:

    • 谢谢!我感谢您的回复,但这对我没有帮助......它仍然显示联系人姓名而不是号码!
    【解决方案3】:
    // phone numbers
    
       int _contact_id=10111; // your contact id
       Cursor curPhone = null;
    
       // 1 = home ,2= personal(mobile), 3= work
    
       try {
    
           Uri URI_PHONE = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
           String[] PROJECTION_PHONE = new String[]{
                                ContactsContract.CommonDataKinds.Phone.NUMBER,
                                ContactsContract.CommonDataKinds.Phone.LABEL,
                                ContactsContract.CommonDataKinds.Phone.TYPE};
          String SELECTION_PHONE = ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?";
          String[] SELECTION_ARRAY_PHONE = new String[]{String.valueOf(_contact_id)};
    
          curPhone = context.getContentResolver()
                     .query(URI_PHONE, PROJECTION_PHONE, SELECTION_PHONE, SELECTION_ARRAY_PHONE, null);
    
          if (curPhone.getCount() > 0) {
    
             curPhone.moveToFirst();
             int indexNumber = curPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
             int labelIndex = curPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LABEL);
             int indexPhoneType = curPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
              while (!curPhone.isAfterLast()) {
                       String contactNumber = curPhone.getString(indexNumber);
                       String labelName = "";
                       int labelType = curPhone.getInt(indexPhoneType);
                       if (labelType ==ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM) {
                              labelName = curPhone.getString(labelIndex);
                         }
               android.util.Log.e("!_@)(@_Number:- ", contactNumber + "[" + labelType + "] " + labelName);
                       curPhone.moveToNext();
              }
            } else {
                        //    noPhones = true;
            }
        } catch (Exception e) {
           e.printStackTrace();
      } finally {
           if (curPhone != null) {
               curPhone.close();
          }
     }
    

    【讨论】:

      【解决方案4】:

      要从您的联系人列表中获取所选联系人的联系人姓名、电话号码和电子邮件,然后只需添加此代码。

      private Button btn;
      private String TAG = "Contacts";
      private static final int RESULT_PICK_CONTACT = 1;
      
      
      btn.setOnClickListener(new View.OnClickListener() {    
      @Override
      public void onClick(View v) {
      selectSingleContact();
      }
      });
      
      private void selectSingleContact() {   
      Intent contactPickerIntent = new     
      Intent(Intent.ACTION_PICK,ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
      startActivityForResult(contactPickerIntent, RESULT_PICK_CONTACT);
      }    
      
      @Override
      public void onActivityResult(int requestCode, int resultCode, Intent data) {
      // check whether the result is ok
      if (resultCode == RESULT_OK) {
      // Check for the request code, we might be usign multiple startActivityForResult
      switch (requestCode) {
          case RESULT_PICK_CONTACT:
              contactPicked(data);
              break;
        }
      } else {
      Log.e("ContactFragment", "Failed to pick contact");
         }
      }
      
      private void contactPicked(Intent data) {
      
      Uri uri = data.getData();
      Log.i(TAG, "contactPicked() uri " + uri.toString());
      Cursor cursor;
      ContentResolver cr = getActivity().getContentResolver();
      
      try {
          Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
      if (null != cur && cur.getCount() > 0) {
          cur.moveToFirst();
          for (String column : cur.getColumnNames()) {
              Log.i(TAG, "contactPicked() Contacts column " + column + " : " + cur.getString(cur.getColumnIndex(column)));
          }
      }
      
      if (cur.getCount() > 0) {
        //Query the content uri
          cursor = getActivity().getContentResolver().query(uri, null, null, null, null);
      
          if (null != cursor && cursor.getCount() > 0) {
              cursor.moveToFirst();
              for (String column : cursor.getColumnNames()) {
                  Log.i(TAG, "contactPicked() uri column " + column + " : " + cursor.getString(cursor.getColumnIndex(column)));
              }
          }
      
          cursor.moveToFirst();
          id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
          Log.i(TAG, "contactPicked() uri id " + id);
          String contact_id = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
          Log.i(TAG, "contactPicked() uri contact id " + contact_id);
          // column index of the contact name
          name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
          // column index of the phone number
          phoneNo = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
          //get Email id of selected contact....
          Log.e("ContactsFragment", "::>> " + id + name + phoneNo);
      
          Cursor cur1 = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                  ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{contact_id}, null);
      
          if (null != cur1 && cur1.getCount() > 0) {
              cur1.moveToFirst();
              for (String column : cur1.getColumnNames()) {
                  Log.i(TAG, "contactPicked() Email column " + column + " : " + cur1.getString(cur1.getColumnIndex(column)));
                  email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
              }
      
             //HERE YOU GET name, phoneno & email of selected contact from contactlist.....
              Log.e("setcontactDetails","::>>" + name+"\nPhoneno:" + phoneNo+"\nEmail: " + email);
          } else {
              Log.e("setcontactDetails","::>>" + name+"\nPhoneno:" + phoneNo+"\nEmail: " + email);
          }
        }
      } catch (IndexOutOfBoundsException e) {
      e.printStackTrace();
          }
      }
      

      还为您的 AndroidManifest.xml 添加权限它适用于我,希望它也适用于您。

      【讨论】:

        猜你喜欢
        • 2014-02-09
        • 1970-01-01
        • 1970-01-01
        • 2017-12-01
        • 2012-04-18
        • 2013-02-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多