select contact in Android using keyword - android

I am working on an Android Contact application and I would like to implement contact lookup using one particular keyword.
This keyword can be either the contact name, phone, email, and so on. Is this possible?
And if so, could anyone please point me in the right direction.
Thanks.
[RE-EDIT]
I was wondering is it okay to use this to lookup the keyword?
Cursor cur = this.context.getContentResolver().query(
ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
null,
ContactsContract.PhoneLookup.LOOKUP_KEY + " like '" + keyword + "%'",
null,
null);

Please use this you can pick up from the phone like phone number and contact name.
ContactsContract.Data.DISPLAY_NAME
ContactsContract.Data.DATA1

Related

How to retrieve all the phone numbers with just one query

Is it possible to retrieve all the phone numbers with just one query(it's ok even if it's a bit slow)?
I found many topics that suggest to make query for the contact list and then a query for each contact but that's really slow.
The only solutions that I found with one query, retrieve just the main number. I need all the numbers of each contact plus the type.
Thanks.
try this
Cursor cursor = getContentResolver()
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[] {Phone._ID, Phone.DISPLAY_NAME, Phone.NUMBER}, null, null, Phone.DISPLAY_NAME + " ASC");

android how to tag phone number of a contact programmatically

i did a lot of research but couldn't find anything to help;
my problem is: i need to create an application where the user can select some of the contacts on his phone to be added to this application where he/she can later communicate with them via sms in a special template. but the user need to select only one phone number to be active to this user on this application. this choice must be caved for later logons.
i was able to retrieve contacts and their phone numbers using lookupkey (which will be saved in my application as a reference for preselected users), but i couldn't figure out how to tag the needed phone number, i was thinking of adding a flag to the phone number but i dont know how, i dont know if this is the right way to do it, i thought of setting the selected phone number as primary then query t when needed... or simply save the phone number id (but i am not sure if saving the id is safe in case user changed the phone numer)...
thx for any help...
After a long period of trial and error I found the solution to my problem. I will be using the contacts lookup key to store the contact and the phone id to store the phone number...as follows:
String selection = ContactsContract.CommonDataKinds.Phone.LOOKUP_KEY + " = '" + lookupkey+ "' and "+Phone._ID+"='"+phoneid+"'";
String[] projection =new String[] {Phone._ID, Phone.DISPLAY_NAME, Phone.NUMBER};
Cursor managedCursor = getContentResolver()
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
projection, selection, null, Phone.DISPLAY_NAME + " ASC");
I'm a bit new to all this stuff. As I understand it, an id match is fast, but may not be stable. If a key+id match fails, you should also try a slower match using only the key - Jeffrey Scofield's SQL selection string could be changed to try id-and-key OR key-only (trusting the query optimiser to prioritize the id match).
I haven't had much luck finding information concerning the wisdom of storing the key long term.

Access Telephone Number suggestion on EditText Field on android

I wrote an sms sending application using the instructions Here. Now I want to add a suggestion for the phone number when the number or name is typed according to the number saved in the phone book i.e like when we enter number to call it suggests a number(dynamically on typing).
Is it possible to do or not? If possible then How Can I do this?I have used API level 2.2.
Edit:
I have do this with the help of Question Here But When I click on the name from phone-book,it place name on text-box but I have need the number to send sms.
Question: How could I get number so that I can sent message to the selected name.
Yes, it is possible. You can change fetch the existing numbers in your phone with ContactsContract, change your phone number EditText to an AutoCompleteTextView, and use a SimpleCursorAdapter to bind the numbers from your contacts to the AutoCompleteTextView.
Addition
If you want to convert a contact name into a phone number you can try this in your "Submit" Button's onClick() method:
Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER },
ContactsContract.Contacts.DISPLAY_NAME + " = '" + autoComplete.getText() + "'",
null, null);
if(cursor.moveToFirst())
phoneNo = cursor.getString(0);
Understand that this only gets the first number from the first contact that matches the name from your AutoCompleteTextView. You'll have to discern a way to distinguish between contacts with the same name and contact with multiple numbers.

read contacts in android

Is it possible to read more than one contact from the contact list from my app.??
what i found is how to receive a list of contacts and loop through the list.
How to read contacts on Android 2.0
but is it possible to select a few of them from the list of contacts.do i need to create a seperate layout for selecting the contacts of my choice and load that layout with data from the contacts list?? please help.
Obviously you are able to select contacts of your choice from list of contacts.But you just need to find a proper "where" clause in query(i.e. what type of contacts you want from list).
And yes,you need to create separate layout to show them up.
For Example: Getting contacts with phone number
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, ContactsContract.Contacts.HAS_PHONE_NUMBER+"='true'", null, null); // gives you the list of contacts who has phone numbers
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null);
while (phones.moveToNext()) {
String phoneNumber = phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phones.close();
}
Hope,i get your requirement correctly.If not,please let me know!
Basically you need to iterate through all contacts with their unique
id called contact_id.
Avoid using cursors inside cursors in the loop.
try this demo app. see how quickly it reads contacts in a proper android way
Source code attached

Android - SimpleAdapter filter query, how to ignore accents?

I am currently using the ContentProvider for contacts to retrieve the contacts from the device and allowing the user to filter the results by typing into an EditText.
To do so I have set up a filter query on a SimpleAdapter as follows:
contactsAdapter.setFilterQueryProvider(new FilterQueryProvider() {
String[] PROJECTION = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.HAS_PHONE_NUMBER,
};
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED COLLATE NOCASE ASC";
public Cursor runQuery(CharSequence constraint) {
String SELECTION = "LOWER(" + ContactsContract.Contacts.DISPLAY_NAME + ")"
+ " LIKE '" + constraint + "%' " + "and " + ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1'";
Cursor cur = managedQuery(ContactsContract.Contacts.CONTENT_URI,
PROJECTION, SELECTION, null, sortOrder);
return cur;
}
});
setListAdapter(contactsAdapter);
This works in most cases however when I have a contact with an accent (Example: Tést Cóntact) then I want the contact to show up even if the user types in Test Contact, currently it doesn't.
Also the case is not ignored in this case either whereas for standard characters it is, for example if I have a contact called Omar and search for omar it matches but if I have a contact called Ómar and search for ómar it doesn't match.
Does anyone know what I should be doing to implement the behavior I want to achieve?
I would see 2 options here :
Create a table that contains accent-less version of the contacts names and a reference to the actual contact Id
Replace accented caracters by ? in your search (which may result in not really user expected behaviour, but is so much simpler)
See my question Using COLLATE in Android SQLite - Locales is ignored in LIKE statement
Short answer - I think it's impossible to use the LIKE statement in Android SQLite and ignore accents. I solved it by making a new column in the database, where you store the same name without accents and in lower case. For example Column 1 stores "Tést Cóntact" - which is used for display and Column 2 stores "test contact" - which is used for using the LIKE statement.
Android 2.3 has a Normalizer class which will remove all accents from a string. If you are supporting lower Android API, then you may need to write your own normalizer somehow...
You can use the replace function to remove the accented characters. Look at this simple solution:
How to SQL compare columns when one has accented chars?

Categories

Resources