如何在Android中按号码执行T9联系人搜索

时间:2014-08-05 07:33:45

标签: android search t9

如何通过键盘输入的号码搜索联系人?我认为android已经实现了T9算法,但我该如何使用这个实现呢?

1 个答案:

答案 0 :(得分:0)

我没有测试过这个......可能会有所帮助..

    String contactId = "";
String contactName = "";

private void retrieveContactRecord(String phoneNo) {
    try{
        Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNo));
        String[] projection = new String[] { ContactsContract.PhoneLookup._ID, ContactsContract.PhoneLookup.DISPLAY_NAME };
        String selection = null;
        String[] selectionArgs = null;
        String sortOrder = ContactsContract.PhoneLookup.DISPLAY_NAME+ " COLLATE LOCALIZED ASC";
        ContentResolver cr = mContext.getContentResolver();
        if(cr != null){
            Cursor resultCur = cr.query(uri, projection, selection, selectionArgs, sortOrder);
            if(resultCur != null){
                while (resultCur.moveToNext()) {
                    contactId = resultCur.getString(resultCur.getColumnIndex(ContactsContract.PhoneLookup._ID));
                    contactName = resultCur.getString(resultCur.getColumnIndexOrThrow(ContactsContract.PhoneLookup.DISPLAY_NAME));
                    Log.e("Info","Contact Id : "+contactId);
                    Log.e("Info","Contact Display Name : "+contactName);
                    break;
                }
                resultCur.close();
            }
        }
    }
    catch(Exception sfg){
        Log.e("Error", "Error in loadContactRecord : "+sfg.toString());
    }
}