简体   繁体   English

如何使用联系人ID获取特定联系号码

[英]How to get Specific Contact Number by using Contact Id

Here my Contact names are Displayed on List View . 这里我的联系人姓名显示在列表视图上。 By clicking the List I get ContactName and Contact Id . 通过单击列表,我获得ContactNameContact Id From that I want to fetch Phone number by Using either Contact ID or Contact name ,Please Help me. 从那里我想通过使用Contact IDContact name获取Phone number ,请帮助我。

Here is My Code 这是我的代码

void ReadContacts(String sort) {
    final Uri uri = ContactsContract.Contacts.CONTENT_URI;
    final String[] projection = new String[] {
            ContactsContract.Contacts._ID,
            ContactsContract.Contacts.DISPLAY_NAME
    };
    //boolean mShowInvisible = false;
    String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1'";
    String[] selectionArgs = null;
    final String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

    m_curContacts = managedQuery(uri, projection, selection, selectionArgs, sortOrder);
    String[] fields = new String[] {ContactsContract.Data.DISPLAY_NAME};
    m_slvAdapter = new SimpleCursorAdapter(this, 
                android.R.layout.simple_list_item_1, 
                m_curContacts,
                fields, 
                new int[] {android.R.id.text1});
    m_slvAdapter.setFilterQueryProvider(new FilterQueryProvider() {

        public Cursor runQuery(CharSequence constraint) {
            Log.d(LOG_TAG, "runQuery constraint:"+constraint);
            String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1'" +
                    " AND "+ ContactsContract.Contacts.DISPLAY_NAME + " LIKE '%"+constraint+"%'";
            String[] selectionArgs = null;//new String[]{"'1'"};//, };
            Cursor cur = managedQuery(uri, projection, selection, selectionArgs, sortOrder);
            return cur;
        }

   });
   m_lvContacts.setAdapter(m_slvAdapter); 
   //  cur.close();

}



public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
    ContentResolver cr;
    Cursor cursor = (Cursor) m_lvContacts.getItemAtPosition(position);
    String szDisplayName = cursor.getString(cursor.getColumnIndexOrThrow( ContactsContract.Contacts.DISPLAY_NAME));
    String szId = cursor.getString(cursor.getColumnIndexOrThrow( ContactsContract.Contacts._ID));
    int nId = cursor.getInt(cursor.getColumnIndexOrThrow( ContactsContract.Contacts._ID));
    Log.d(LOG_TAG, "Item click:"+position+" szId:"+szId+" nId:"+nId+" Data:"+szDisplayName);
    Toast.makeText(getBaseContext(), "Item click:"+phoneNumber+" szId:"+szId+" nId:"+nId+" Data:"+szDisplayName, Toast.LENGTH_SHORT).show();

}

Try this one 试试这个吧

ArrayList<String> phones = new ArrayList<String>();

Cursor cursor = mContentResolver.query(
        CommonDataKinds.Phone.CONTENT_URI, 
        null, 
        CommonDataKinds.Phone.CONTACT_ID +" = ?", 
        new String[]{id}, null);

while (cursor.moveToNext()) 
{
    phones.add(cursor.getString(cursor.getColumnIndex(CommonDataKinds.Phone.NUMBER)));
} 

cursor.close();
return(phones);

Try this one 试试这个吧

public void getNameUsingContactId(String contactId){

    String cContactIdString = ContactsContract.Contacts._ID;
    Uri cCONTACT_CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;
    String cDisplayNameColumn = ContactsContract.Contacts.DISPLAY_NAME;

    String selection = cContactIdString + " = ? ";
    String[] selectionArgs = new String[]{String.valueOf(contactId)};

    Cursor cursor = mContext.getContentResolver().query(cCONTACT_CONTENT_URI, null, selection, selectionArgs, null);
    if ((cursor != null) && (cursor.getCount() > 0)) {
        cursor.moveToFirst();
        while ((cursor != null) && (cursor.isAfterLast() == false)) {
            if (cursor.getColumnIndex(cContactIdString) >= 0) {
                if (contactId.equals(cursor.getString(cursor.getColumnIndex(cContactIdString)))) {
                    String name = cursor.getString(cursor.getColumnIndex(cDisplayNameColumn));
                     break;
                }
            }
            cursor.moveToNext();
        }
    }
    if (cursor != null)
        cursor.close();  
}   
public static final String contactIdByPhoneNumber(Context ctx, String phoneNumber) {
    String contactId = null;
    if (phoneNumber != null && phoneNumber.length() > 0) {
        ContentResolver contentResolver = ctx.getContentResolver();

        Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));

        String[] projection = new String[] { PhoneLookup._ID };

        Cursor cursor = contentResolver.query(uri, projection, null, null, null);

        if (cursor != null) {
            while (cursor.moveToNext()) {
                contactId = cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup._ID));
            }
            cursor.close();
        }
    }
    return contactId;
}

You'll find the phonenumbers in the data table. 你会在数据表中找到phonenumbers。 Find the corresponding raw contact ids in the rawcontacts table (use contact ID as where criteria), use these raw contact ids to query the data table. 在rawcontacts表中找到相应的原始联系人ID(使用联系人ID作为条件),使用这些原始联系人ID查询数据表。 The phonenumber will have a certain mime type (away from the docs now) so you can recognize them. phonenumber将具有某种mime类型(现在远离文档),因此您可以识别它们。 Hope this helps! 希望这可以帮助!

Tips: copy the contacts2.db database from your emulator/device and use Squirrel (SQLite jdbc driver) to poke around in the contacts database. 提示:从您的模拟器/设备复制contacts2.db数据库,并使用Squirrel(SQLite jdbc驱动程序)在联系人数据库中查找。 The db is located under /data/data/ look for contacts in the list). 数据库位于/ data / data /下查找列表中的联系人)。

Guys I got the Answer: 伙计们我得到了答案:

Please Use this Code if you need: 如果您需要,请使用此代码:

  Cursor cursor = (Cursor) m_lvContacts.getItemAtPosition(position);
        String szDisplayName = cursor.getString(cursor.getColumnIndexOrThrow( ContactsContract.Contacts.DISPLAY_NAME));
        String szId = cursor.getString(cursor.getColumnIndexOrThrow( ContactsContract.Contacts._ID));
        int nId = cursor.getInt(cursor.getColumnIndexOrThrow( ContactsContract.Contacts._ID));
         long l = Long.parseLong(szId);
        int type=ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE;




 cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
         null,
         ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"='"+szId+"'",
         null, null);

            int phoneNumberIndex = cursor
                    .getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER);

            Log.d(TAG, String.valueOf(cursor.getCount()));

            if (cursor != null) {
                Log.v(TAG, "Cursor Not null");
                try {
                    if (cursor.moveToNext()) {
                        Log.v(TAG, "Moved to first");
                        Log.v(TAG, "Cursor Moved to first and checking");
                        phoneNumber = cursor.getString(phoneNumberIndex);
                        System.out.println("HAGSd"+phoneNumber);
                    }
                } finally {
                    Log.v(TAG, "In finally");
                    cursor.close();
                }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM