I have an app which can retrieve the mailing address from contact information on the device. I retrieve the information for street address, post code, city etc. using the contacts API using code similar to below. I then display the address on separate lines and the user can accept it or change it if desired.
String where = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
String[] params = new String[]{id, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE};
Cursor addrCur = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, where, params, null);
...
while(addrCur.moveToNext()) {
String street = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
String city = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
String state = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
String postalCode = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
String country = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
...
}
addrCur.close();
This has worked fine with Android 2.2/2.3 but I noticed on 3.x and higher that the entire address that is returned can be formatted as a single string with line breaks in the STREET field. This occurs when the contact/address is created on the device; if I sync a contact from Gmail, the address is retrieved correctly (I suspect that the synced info is saved in the correct fields). I can't see any way to get the individual data elements (street, postcode, etc) for these types of addresses. Is this possible? Is there a new API I need to use?
I haven't found a way to do it. The only workaround I've found is to get the full, formatted address by getting the ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS column from the cursor and then attempt to break it apart assuming a couple different standard formattings. There's no way to input an address in the Honeycomb/ICS Contacts app other than using a free-form text box, so I guess the Contacts app doesn't try and parse it automatically before storing it in the database.
Hopefully I'm wrong and there is a way to do it - but I haven't found it yet.
Related
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.
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.
I started building my app for api 13, because I thougth it could be efficient enough for the most users.
But i changed it to api 10.
Then the following code stopped working `
String empName=data.getStringExtra("name");
String hourPay=data.getStringExtra("hourPay");
String key="";
String id="";
Uri lookupUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_FILTER_URI, empName);
Cursor idCursor = getContentResolver().query(lookupUri, null, null, null, null);
idCursor.moveToFirst();
id = idCursor.getString(idCursor.getColumnIndex(ContactsContract.Contacts._ID));
key = idCursor.getString(idCursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));`
The problem is that it works only with contact names written in English or the ones that have just name (not surname)
For example :
It works for the following contacts :
Express Service,
George,
Maria Mpakou,
Σοφια,
With no distinction between languages
But when a contact is written in my language (greek) and has space (e.g Αιμιλιος Δημιος) it returns the underneath cursor with index out of bounds.
When i first tried at api 13 worked like a charm.
By the way my Android.Manifest has utf-8 encoding if that has something to do with it
Please any recomendation will bw helpfull.
Thanks
After a lot of search i finally did it with a different way...like asking a Sql query to all contacts
Here is the code:
String empName=data.getStringExtra("name");
Cursor idCursor = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI,null,Contacts.DISPLAY_NAME+" = '"+empName+"'", null, null);
That worked like a charm!
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
I am trying to read the contact list using the following code:
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(People.CONTENT_URI,null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext())
{
String id = cur.getString(cur.getColumnIndex(People._ID));
Cursor personCur = cr.query(Contacts.Phones.CONTENT_URI,
null,
Contacts.Phones.PERSON_ID +"= ?"+ Contacts.Phones.NUMBER_KEY ,
new String[]{id}, null);
String phoneKey = "";
while (personCur.moveToNext()) {
phoneKey = personCur.getString(personCur.getColumnIndex(Contacts.Phones.NUMBER_KEY));
}
The problem is that phoneKey return in reverse order meaning if the phone number is 054-123-4567 the value is 7654321450
I could not find any documentation on this issue.
Any idea how to fix it? or is it the expected result?
thanks
-Z
Firstly, Contacts.Phones.NUMBER_KEY is deprecated.
Secondly, why do you need this value? This is not the phone number, but the "normalized" phone number.
If you want the phone number in the way you have your code, use Contacts.Phones.NUMBER
Anyways, I would suggest you not to use deprecated elements unless you are targeting older phones.
================
Phone numbers are stored in stripped reversed order for easier matching. It helps to use the LIKE operator in SQL.
For a normal SQL user, it might look same to use where NUMBER_KEY LIKE '%1234' and where NUMBER_KEY LIKE '4321%'.
But for the db engine, having the wildcard at the end is much easier (efficient) Efficiency is a major factor for incoming call caller id lookup.
BTW, you might have not noticed, most phones (not talking about android only) does not match the whole number for caller id lookup.
In all my Nokia phones I have a number saved, say its 016 12345678 (its not the real number) as Mr X
When I get phone call from another number 016 99345678, it shows Mr X is calling. It compares only last few digits!!!