compare phone number in android contacts database - android

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.

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();

There can be multiple ways to do this:
As you see below here:
String yourInputString = "+91XXXXXXXXXX";
String dbContactString = "XX XX XXXXXX";
both have different format; so we must have to extract substring ("XXXXXXXXXX") from first string and also have to remove whitespaces ("XXXXXXXXXX") from the second string!
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"
now, both have same format so you can easily compare them with equals().
yourInputString.equals(dbContactString) is the way.
It returns true if yourInputString is equals to dbContactString in value. Else, it will return false.
Hope this helps!

Related

How to get the base alphabet of a foreign word and sort them accordingly like in device local contact?

For example, let's say that a local contact have multiple Japanese contacts with the name たなか, つなき, and てるてる. In the local contact these names will be sorted under the section index letter た.
For what I've read, it is possible to sort them via collate, but how do you sort them to index た or even know that they should go to the た index?
Thank you.
The API has a trick for returning that list of alphabet letters, and optionally also including counts per character.
See official docs for EXTRA_ADDRESS_BOOK_INDEX:
https://developer.android.com/reference/android/provider/ContactsContract.Contacts.html#EXTRA_ADDRESS_BOOK_INDEX
Example code:
Uri uri = Contacts.CONTENT_URI.buildUpon()
.appendQueryParameter(Contacts.EXTRA_ADDRESS_BOOK_INDEX, "true")
.build();
Cursor cursor = getContentResolver().query(uri,
new String[] {Contacts.DISPLAY_NAME},
null, null, null);
Bundle bundle = cursor.getExtras();
if (bundle.containsKey(Contacts.EXTRA_ADDRESS_BOOK_INDEX_TITLES) && bundle.containsKey(Contacts.EXTRA_ADDRESS_BOOK_INDEX_COUNTS)) {
String sections[] = bundle.getStringArray(Contacts.EXTRA_ADDRESS_BOOK_INDEX_TITLES);
// sections will now contain an array of characters that represent the first letter of each available contact name
// optionally - if you need counts per letter - int counts[] = bundle.getIntArray(Contacts.EXTRA_ADDRESS_BOOK_INDEX_COUNTS);
}

Android - phone number formatting and removing the country/area code

I have built an app where I loop through and collect the users phone contacts, my aim is to then use these numbers and query my parse database and look for records that contain the users contacts (this will be to check if any of the users contacts are a user of my app, a users phone number will be saved to my parse database when they register). The problem I've got is that when collecting the users contacts numbers they are returned in different formats, some +447966000000, some 07966000000, some 07 966000 000000, etc.
My question is, what would be the best way to format my numbers when saving them to the database and retrieving them from the users contacts so that all numbers are saved and retrieved in the same format so that when I do a conditional check on them they will be easy to compare?
I have downloaded phone Number Utils library but I am not sure what in the library could be used to do something like this.
Code so far:
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
while (phones.moveToNext())
{
String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Toast.makeText(getApplicationContext(),name + " " + phoneNumber, Toast.LENGTH_LONG).show();
}
phones.close();
You can use PhoneNumberUtils.compare to compare and check if they are same or not.It returns true if they are same ignoring country codes etc.
Example:
PhoneNumberUtils.compare(context, 1234567890, +911234567890);
returns true
I have done it for Indian mobile number format
private String getNumber(String moNumber) {
Pattern special = Pattern.compile ("[!##$%&*()_+=|<>?{}\\[\\]~-]");
if (moNumber.isEmpty()||moNumber.length()<10) {
MydebugClass.showToast(getContext(), "Please input valid Number");
return null;
}else if (moNumber.length()>10 && !special.matcher(moNumber).find()){
String[] number=moNumber.split("");
StringBuilder stringBuilder=new StringBuilder();
for(int i=moNumber.length();i>moNumber.length()-10;i--){
stringBuilder.append(number[i]);
}
String reverse=new StringBuffer(stringBuilder).reverse().toString();
return reverse;
}else if(moNumber.length()>10&&special.matcher(moNumber).find()){
String numberOnly= moNumber.replaceAll("[^0-9]", "");
String[] number=numberOnly.split("");
StringBuilder stringBuilder=new StringBuilder();
for(int i=moNumber.length();i>moNumber.length()-10;i--){
stringBuilder.append(number[i]);
}
String reverse=new StringBuffer(stringBuilder).reverse().toString();
Log.d("mobilenumberspecial",reverse);
return reverse;
}
else {
return moNumber;
}
return null;
}

Android get contact names by phone number efficiently for displaying in ListView

I'm creating an Sms app which contains a ListView in the Main Activity that displays all the conversations from the sms inbox. Each ListView row displays one conversation along with the phone number, message body and time of the message. Now instead of the phone number I want to display the contact name if it exists.
So far, for getting the contact name by phone number I found this code
private String getDisplayNameByNumber(String number) {
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
Cursor contactLookup = context.getContentResolver().query(uri, new String[] {ContactsContract.PhoneLookup._ID,
ContactsContract.PhoneLookup.DISPLAY_NAME }, null, null, null);
int indexName = contactLookup.getColumnIndex(ContactsContract.Data.DISPLAY_NAME);
try {
if (contactLookup != null && contactLookup.moveToNext()) {
number = contactLookup.getString(indexName);
}
} finally {
if (contactLookup != null) {
contactLookup.close();
}
}
return number;
}
But this seems inefficient as it has to make a query for each contact name individually and lags the app. So instead I tried to get all the contact names from the phone and store it in an HashMap with the phone number as the key and the contact name as the value, so that I can get the contact name any time I want from the HashMap. But there seems to be another problem, the phone numbers are stored in many different formats, for eg:
+91 4324244434
04324244434
0224324244434
So how do I search for a phone number from the HashMap since it can be stored in many different formats?
Before you add the contact to the HashMap, use regular expression to grab the phone number. This way, no matter what format the phone number is in, the regular expression will be able to grab the appropriate number needed.
Once you grab the number, add it to the HashMap accordingly.
Hope that answers your question.

how to get the saved words in android dictionary

I'm developing an app that can add words to UserDictionary and query from it.
when running the app on tablet device it work fine.
but when running it on non tablet device
and save an auto corrected word to my word list and query for it, it didn't retrieve the words.
so what I should to do to query and select all saved auto corrected word.
// this to add word to dictionary
Uri dic = UserDictionary.Words.CONTENT_URI;
UserDictionary.Words.addWord(this, "word", 100, UserDictionary.Words.LOCALE_TYPE_ALL);
// this to query
Uri dic = UserDictionary.Words.CONTENT_URI;
ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(dic, null, null, null, null);
while (cursor.moveToNext()){
String word = cursor.getString(cursor.getColumnIndex(UserDictionary.Words.WORD));
int id = cursor.getInt(cursor.getColumnIndex(UserDictionary.Words._ID));
String app = cursor.getString(cursor.getColumnIndex(UserDictionary.Words.APP_ID));
int frequency = cursor.getInt(cursor.getColumnIndex(UserDictionary.Words.FREQUENCY));
String locale = cursor.getString(cursor.getColumnIndex(UserDictionary.Words.LOCALE));
Log.i("", "word: "+word+"\nId: "+id+"\nAppID: "+app+"\nfrequency: "+frequency+"\nLocale: "+locale);
}
To add a word, the Javadoc suggests using UserDicrionary.Words.addWord.
To use the spellcheker, above Android 4.0, there's a framework, along with a few examples of its use. Hope these help.

assign database value for variable in android

This is about Android and SQLite database.
Simply, I want to know, how can I assign value to variable that get from the Database table?
I have table called wish and colmns are date[date value string], phoneno[phone no string] and wishtype[simply a string].
All I want is get phoneno, and wishtype according to usergiven date[input to the query will be the date] and assign those two into seperate variable.
I tried out so many methods, but it gives me a runtime errors.
String phone_no;
String wish_type;
Cursor c = getContentResolver().query(your_table_uri,
new String[] {"_id", "phone_no", "wish_type", "date" /*these are your column names*/},
"date=?", your_date_filter, null);
if(c.moveToFirst()) { // checks if c retrieves anything. you may want to check first if c!=null
phone_no = c.getString(c.getColumnIndex("phone_no"));
wish_type = c.getString(c.getColumnIndex("wish_type"));
}

Categories

Resources