从Android的联系人数据库中获取联系ID没有如预期 [英] Getting Contact ID from Android Contacts database is not working as intended
问题描述
我有以下的code。
INT phoneContactID =新的随机()nextInt();
光标contactLookupCursor = context.getContentResolver()查询(Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,Uri.en code(contactNumber)),新的String [] {PhoneLookup.DISPLAY_NAME,PhoneLookup._ID},NULL,NULL,NULL );
尝试
{
contactLookupCursor.moveToFirst();
而(contactLookupCursor.moveToNext())
{
phoneContactID = contactLookupCursor.getInt(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup._ID));
}
}
最后
{
contactLookupCursor.close();
}
在上面的code的问题是,即使我给一个现有号码在仿真器的接触,其不返回任何结果。我测试了一个小时,回来了,它是工作的罚款,而现在,当我再次测试它,它不返回任何东西。我不知道什么是错的code。我所要做的就是获得一个ID匹配多号单。例如说,有一个名为A联系人姓名和A有两个数字。实际上对于A的接触式ID应该是1不管我指的是哪一些。我的假设是正确的?
更新:我做了一些更多的测试。比方说,如果一个号码存储,而不该国code像222-222-2222联系人数据库。使用下面的code回报接触式ID搜查只有当我通过2222222222或222-222-2222。如果相同的号码存储像12222222222一个有效的接触式ID只收到如果我搜索号码是12222222222.
的String []投影=新的String [] {ContactsContract.CommonDataKinds.Phone._ID,ContactsContract.CommonDataKinds.Phone.CONTACT_ID};
乌里contactUri = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI,Uri.en code(contactNumber));
光标C = context.getContentResolver()查询(contactUri,投影,NULL,NULL,NULL);
如果(c.moveToFirst()){
phoneContactID = c.getInt(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
}
我不知道如果我在这里做得不对。任何帮助将是AP preciated。
公共静态INT getContactIDFromNumber(字符串contactNumber,上下文语境)
{
contactNumber = Uri.en code(contactNumber);
。INT phoneContactID =新的随机()nextInt();
光标contactLookupCursor = context.getContentResolver().query(Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,Uri.contactNumber),new的String [] {PhoneLookup.DISPLAY_NAME,PhoneLookup._ID},NULL,NULL,NULL);
而(contactLookupCursor.moveToNext()){
phoneContactID = contactLookupCursor.getInt(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup._ID));
}
contactLookupCursor.close();
返回phoneContactID;
}
在code是现在的工作。
I have the following code.
int phoneContactID = new Random().nextInt();
Cursor contactLookupCursor = context.getContentResolver().query( Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,Uri.encode(contactNumber)), new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup._ID},null,null,null);
try
{
contactLookupCursor.moveToFirst();
while(contactLookupCursor.moveToNext())
{
phoneContactID = contactLookupCursor.getInt(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup._ID));
}
}
finally
{
contactLookupCursor.close();
}
The problem in the above code is that even if I give a existing number in emulator contacts, its not returning any results. I was testing it an hour back and it was working fine, and now when I tested it again, its not returning anything. I am not sure if anything is wrong with the code. What I am trying to do is get a ID that matches a single with multiple numbers. For instance say there is a contact name called "A" and A has two numbers. Essentially the contact ID for A should be 1 regardless of which number I refer to. Is my assumption correct ?
UPDATE : I did some more tests. Let's say if a number is stored without the country code in the contacts database like 222-222-2222. A search using the below code returns contact id only when I pass 2222222222 or 222-222-2222. And if the same number is stored like 12222222222 a valid contact id is received only if I search number is 12222222222.
String[] projection = new String[] {ContactsContract.CommonDataKinds.Phone._ID,ContactsContract.CommonDataKinds.Phone.CONTACT_ID};
Uri contactUri = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI, Uri.encode(contactNumber));
Cursor c = context.getContentResolver().query(contactUri, projection, null, null, null);
if (c.moveToFirst()) {
phoneContactID = c.getInt(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
}
I am not sure if I am doing something wrong here. Any help would be appreciated.
public static int getContactIDFromNumber(String contactNumber,Context context)
{
contactNumber = Uri.encode(contactNumber);
int phoneContactID = new Random().nextInt();
Cursor contactLookupCursor = context.getContentResolver().query(Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,Uri.contactNumber),new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup._ID}, null, null, null);
while(contactLookupCursor.moveToNext()){
phoneContactID = contactLookupCursor.getInt(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup._ID));
}
contactLookupCursor.close();
return phoneContactID;
}
The code that is working now.
这篇关于从Android的联系人数据库中获取联系ID没有如预期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!