联系存在于接触 [英] contact exists in contacts
本文介绍了联系存在于接触的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的电话号码。有没有什么办法来检查是否存在于联系人数据库中的设备的电话号码或不?根据我需要有进一步的举动在我的应用程序。请建议或任何人可以有样本code段,请提供。
下面是code我写的:
公共布尔contactExists(活动_activity,串号){
的String [] mPhoneNumberProjection = {PhoneLookup._ID,PhoneLookup.NUMBER,PhoneLookup.DISPLAY_NAME};
。光标CUR = _activity.getContentResolver()查询(数量,mPhoneNumberProjection,NULL,NULL,NULL);
尝试 {
如果(cur.moveToFirst()){
返回true;
}
} 最后 {
如果(CUR!= NULL)
cur.close();
}
返回false;
} // contactExists
在此先感谢...
解决方案
公共布尔contactExists(活动_activity,串号){
如果(数字!= NULL){
乌里lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,Uri.en code(数));
的String [] mPhoneNumberProjection = {PhoneLookup._ID,PhoneLookup.NUMBER,PhoneLookup.DISPLAY_NAME};
。光标CUR = _activity.getContentResolver()查询(lookupUri,mPhoneNumberProjection,NULL,NULL,NULL);
尝试 {
如果(cur.moveToFirst()){
返回true;
}
} 最后 {
如果(CUR!= NULL)
cur.close();
}
返回false;
} 其他 {
返回false;
}
} // contactExists
处理的空指针异常。
I have phone number. Is there any way to check whether the phone number exists in contacts database in the device or not? Depending on that I need have move further in my app. Please suggest or if any one can have sample code snippet please provide.
The below is the code I wrote:
public boolean contactExists(Activity _activity, String number) {
String[] mPhoneNumberProjection = { PhoneLookup._ID, PhoneLookup.NUMBER, PhoneLookup.DISPLAY_NAME };
Cursor cur = _activity.getContentResolver().query(number, mPhoneNumberProjection, null, null, null);
try {
if (cur.moveToFirst()) {
return true;
}
} finally {
if (cur != null)
cur.close();
}
return false;
}// contactExists
Thanks in Advance...
解决方案
public boolean contactExists(Activity _activity, String number) {
if (number != null) {
Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
String[] mPhoneNumberProjection = { PhoneLookup._ID, PhoneLookup.NUMBER, PhoneLookup.DISPLAY_NAME };
Cursor cur = _activity.getContentResolver().query(lookupUri, mPhoneNumberProjection, null, null, null);
try {
if (cur.moveToFirst()) {
return true;
}
} finally {
if (cur != null)
cur.close();
}
return false;
} else {
return false;
}
}// contactExists
Handled nullpointer exception.
这篇关于联系存在于接触的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文