访问联系人的联系电话

发布于 2024-10-21 03:12:58 字数 948 浏览 1 评论 0原文

我无法访问联系人的联系电话。 我从互联网上获得了此代码

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();    

    } 

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

I am not able to access the contact number of a contact.
I got this code from internet

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();    

    } 

But it displays only the name of the contact.
I want the contact's phone number.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

因为看清所以看轻 2024-10-28 03:12:58

查看这篇文章,它有您正在寻找的答案:读取所有联系人的电话号码在安卓中

Check this post out, it has the answer your looking for: Read all contact's phone numbers in android

煞人兵器 2024-10-28 03:12:58

修改您的代码片段

    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();            
    }  

Modifying your code snippet

    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();            
    }  
生生漫 2024-10-28 03:12:58
// 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();
      }
 }
// 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();
      }
 }
夏至、离别 2024-10-28 03:12:58

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

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 添加权限它对我有用,希望它也对您有用。

To get contact name, number as well as email of a selected contact from your contact list, then simply add this code.

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();
    }
}

Also add permissions to your AndroidManifest.xml It works for me, hope it will works for you also.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文