compare phone number in android contacts database

时间:2015-08-14 22:41:54

标签: android sqlite

I have a phone number with country code like +91XXXXXXXXXX. and a phone number in my android contacts database is in the form of XX XX XXXXXX. How to compare them in sqlite database query. Any help would be appreciable....

I need this to update or delete the existing phone number in android database.

Note

Phone number can be of any country. Above is just the example.

2 个答案:

答案 0 :(得分:2)

有多种方法可以做到这一点:

如下所示:

String yourInputString = "+91XXXXXXXXXX";
String dbContactString = "XX XX XXXXXX";

两者都有不同的格式;所以我们必须从第一个字符串中提取子字符串(“XXXXXXXXXX”),并且还必须从第二个字符串中删除空格(“XXXXXXXXXX”)!

yourInputString = yourInputString.substring(2); // 2 is start index  and result will be "XXXXXXXXXX" 
dbContactString = dbContactString.replaceAll("\\s+",""); // replaces all whitespaces and result will be "XXXXXXXXXX"

现在,两者都具有相同的格式,因此您可以轻松地将它们与equals()进行比较。

yourInputString.equals(dbContactString)就是这样。

如果yourInputString的值等于dbContactString,则返回true。否则,它将返回false。

希望这有帮助!

答案 1 :(得分:2)

you can try the code below

Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode("Pass phone number here"));
Cursor c = getContentResolver().query(uri, new String[]{PhoneLookup.NUMBER}, null, null, null);
if(c.getCount()>0){
    c.moveToFirst(); 
    System.out.println(c.getString(c.getColumnIndex(PhoneLookup.NUMBER)));
}
c.close();