搜索联系人并从android中的电话联系人中获取联系人号码

Posted

技术标签:

【中文标题】搜索联系人并从android中的电话联系人中获取联系人号码【英文标题】:search contacts and get the contact number from phone contacts in android 【发布时间】:2014-03-30 17:48:52 【问题描述】:

我正在使用一个应用程序,在此我必须在单击按钮并从电话联系人中选择一个联系人时搜索联系人。最后我想把它设置为edittext。我可以从手机联系人中搜索联系人,但我无法从手机联系人中获取联系人。

活动:

searchBtn.setOnClickListener(new OnClickListener() 

@Override
public void onClick(View v) 
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
startActivityForResult(intent,1);


);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);

if(requestCode==RESULT_OK)
Uri contactData = data.getData();
Cursor cursor = managedQuery(contactData, null, null, null, null);
cursor.moveToFirst();
String number = cursor.getString(cursor.getColumnIndexOrThrow
(ContactsContract.CommonDataKinds.Phone.NUMBER));

//contactName.setText(name);
ephoneNumber.setText(number);
//contactEmail.setText(email);


【问题讨论】:

【参考方案1】:

StartActivityForResult 的第一个设置 Intent 像这样

private void pickContact() 
    Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
    pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
    startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);

然后处理intent从本地联系人应用程序携带的结果

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    // Check which request it is that we're responding to
    if (requestCode == PICK_CONTACT_REQUEST) 
        // Make sure the request was successful
        if (resultCode == RESULT_OK) 
            // Get the URI that points to the selected contact
            Uri contactUri = data.getData();
            // We only need the NUMBER column, because there will be only one row in the result
            String[] projection = Phone.NUMBER;

            // Perform the query on the contact to get the NUMBER column
            // We don't need a selection or sort order (there's only one result for the given URI)
            // CAUTION: The query() method should be called from a separate thread to avoid blocking
            // your app's UI thread. (For simplicity of the sample, this code doesn't do that.)
            // Consider using CursorLoader to perform the query.
            Cursor cursor = getContentResolver()
                    .query(contactUri, projection, null, null, null);
            cursor.moveToFirst();

            // Retrieve the phone number from the NUMBER column
            int column = cursor.getColumnIndex(Phone.NUMBER);
            String number = cursor.getString(column);

            // Do something with the phone number...
        
    

查看其他参考链接

Link 1 Link 2

【讨论】:

【参考方案2】:

巴拉特,

你需要这样的许可 -

android:name="android.permission.READ_CONTACTS"/>

然后,调用联系人选择器

Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);

那么,

@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) 
super.onActivityResult(reqCode, resultCode, data);

switch (reqCode) 
case (PICK_CONTACT) :
   if (resultCode == Activity.RESULT_OK) 
    Uri contactData = data.getData();
    Cursor c =  managedQuery(contactData, null, null, null, null);
     if (c.moveToFirst()) 
      String name = c.getString(c.getColumnIndexOrThrow(People.NAME));
      // TODO Whatever you want to do with the selected contact name.
    
  
  break;
  
 

或者,你可以用其他方式

Cursor cursor = null;
try 
    cursor = context.getContentResolver().query(Phone.CONTENT_URI, null, null, null, null);
    int contactIdIdx = cursor.getColumnIndex(Phone._ID);
    int nameIdx = cursor.getColumnIndex(Phone.DISPLAY_NAME);
    int phoneNumberIdx = cursor.getColumnIndex(Phone.NUMBER);
    int photoIdIdx = cursor.getColumnIndex(Phone.PHOTO_ID);
    cursor.moveToFirst();
    do 
        String idContact = cursor.getString(contactIdIdx);
        String name = cursor.getString(nameIdx);
        String phoneNumber = cursor.getString(phoneNumberIdx);
        //...
     while (cursor.moveToNext());  
 catch (Exception e) 
    e.printStackTrace();
 finally 
    if (cursor != null) 
        cursor.close();
    

上述解决方案很简单,适用于获取联系号码。

【讨论】:

【参考方案3】:

试试这个

CursorLoader cursorLoader = new CursorLoader(mActivity,
            CallLog.Calls.CONTENT_URI, null, null, null, null);
managedCursor = cursorLoader.loadInBackground();
int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
int name = managedCursor.getColumnIndex(CallLog.Calls.CACHED_NAME);
String conTactnumber=managedCursor.getString(number);               
String contactname=managedCursor.getString(name);

set permission   
<uses-permission android:name="android.permission.READ_CONTACTS" >
</uses-permission>

【讨论】:

以上是关于搜索联系人并从android中的电话联系人中获取联系人号码的主要内容,如果未能解决你的问题,请参考以下文章

Android:根据电话号码搜索联系人

Android - 如何从通话记录中获取联系人?

如何从android中的给定电话号码获取电话号码类型标签

从 Android 中的 URI 检索联系人电话号码

Android从联系人列表中获取电话号码

android 车机电话的通讯录联系人搜索实现解析 ------- 填坑日记