Join Contact data with my table in Android - android

I have table with contacts identified by LOOKUP_KEY and I need JOIN it with system contact data to get real name and photo. Is possible do this? Or I must read contact data for every contact item CursorAdapter and get required information about contact?
This question is related to:
https://stackoverflow.com/questions/7222297/custom-contacts-view-join-or-combine-contactscontract-with-sqlite-table.
But it's without reply.

Maybe a CursorJoiner would do the trick?

The only way to do that is going to be to read the data through the Content Provider, and put it in your own database. Afterwards you can perform JOIN queries with the data.

Related

Android Contacts: Is it possible to reference a given contact entry using a URN?

Let me try and clarify my intentions.
I'm developing an app that accesses to the Android contacts provider. I have already implemented a mechanism for pulling contacts from the contacts provider and storing the results in an SQLite table. Currently, when I query for the results of a contact's _ID, I can retrieve all the data for that contact, phone numbers, email addresses, etc.
However, in order to specify which of those my app should use on future occasions, I have to store the resulting contact data (e.g. CommonDataKinds.Phone.NUMBER, CommonDataKinds.Phone.TYPE etc) in the SQL table.
This presents a problem if the data in the Android contacts provider has changed. One solution I have considered is to re-query the _ID and store the data that has changed. However, implementing such a solution requires gathering all data for that contact, making it difficult to determine the correct contact data to use.
My question is thus:
Is there a unique record key used in the Android Provider's contact data, in the same way as there is in the Provider's contact entry itself? A phone number or email address equivalent of Contact._ID?
Failing that, does the Android contacts provider store the last modified date and time? I'm hoping that if I can't reference the contact data in the provider, I can at least run a check to see if anything has changed since the contact was selected for use in the app, allowing my app to alert the user that the data has changed.
Yoi may use ContentObserver with the help of a service to monitor for contacts change or update.
for example-
extend contentObserver-
public class Contact_change extends ContentObserver
register contentobserver-
Contact_change changeObserver = new Contact_change();
getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true,chageobserver);
By using this you can monitor for contact changes and update your database.
So while looking to see what was possible, I've come across some interesting findings by doing a localised record dump to my tablet.
I incorrectly referred to the phone _ID when asking my question. It's there, but it references individual records in any given contact. This might be useful, it might be exactly what I hoped, but it might also be a dead end.
So far, I've retrieved a contact using the Android Contacts Provider, which returns the contact's data including the fields that follow
ContactsContract.Contacts._ID
ContactsContract.Contacts.DISPLAY_NAME
After assigning the _ID to a String and using it in a query, I initially searched for the _ID in the ContactsContract.Data._ID field, which did not work as intended.
That's when I realised I was doing it wrong. I needed to apply the search to ContactsContract.Data.CONTACT_ID, which then retrieved all related data records for my selected contact.
Having corrected the error and re-ran my query, I found a series of records for the CONTACT_ID, each of which had its' own unique _ID (with the exception of photos and binary data, which all had a null reference to _ID instead.
Now the question is: Does this data reference change even if a phone number, email address or IM entry is modified, as opposed to deleted and re-created? I'll probably find out through further testing, but I wanted to ask if anyone had already tried this first.
Ultimately, I am hoping my internal SQL table can store only the CONTACT_ID and the _ID for each entry, relying on callbacks to the Android Provider to pull the relevant values. That way, I can be sure the data displayed in my app is up to date between the app and the stored contact data on the device.
Combined with the change notifier code provided here, I should be able to alert the user to any changes that may require action on their part.
Update:
More investigation reveals that the UID associated with a given contact entry is preserved through entry edits.
The UID is lost obviously on deletion, and in my brief testing, I have not found UID's being recycled. This solves a referential integrity issue for me at least, as that UID won't suddenly reference another record out of nowhere.
The upshot for anyone else wanting to reference individual entries in the Contacts Provider is that it's possible, and you can store only the _ID and still retrieve individual entries in the provider.

Get contacts from SQL database by certain criteria on Android

How do I query all contacts matching a certain criteria on Android?
Let´s say, I want to have all contacts, which have a name, a phone number, but no profile picture.
As I understand the SQL thing, I need some kind of selection, which is passed to my query, but how would it exactly look in the example above?
I´m asking, because I´m currently running an app which first queries all contacts and after that I iterate through it and filter it, which seems much less performant then querying directly the right contacts
You are correct in the sense that contacts are stored in a SQLite database, but they are accessed via the Contacts Content Provider.
The Contacts Content Provider is organized in a three table fashion. At the bottom of the data structure lies the Data table. Here you will find all data that pertains to a specific RawContact (will discuss shortly). The Data table stores information like phone numbers, emails, addresses,etc. It does so by using MIME name value pairs defined on the mimetypes database. Up one level is the RawContacts database. Here all the information that pertains to the same account (i.e. Contact John Doe may have a twitter and Facebook account). In short, the RawContacts table keeps track of Data MIME types that pertain to the same person so there is no need to store the same information multiple times for different accounts. At the top of the data structure you have the Contacts table that groups all the information for a single person. It is basically a way of unifying all account information into one contact. Think of this contacts as you regularly would.
You should take a look at the Loader class which will allow you to perform queries on a background Thread. If you use this class once you implement the Loader callback for the Cursor, you will create a CursorLoader. One of its constructors parameter is
A typical MySQL where clause where you can specify the constraints you want to include as part of your query.
Here you would find a guide to Loaders to query a database.

How to add one extra field to contact database?

I want to add one field(column) to contact fields,that i can set value for that field in code and mobile user can not see that field in contact properties.In this way i can use contact information without create another database to copy contact database.
Is this possible?
please help me
I don't think you can modify the contacts database directly (or at least not without rooting the device first).
If you want to add additional information to a stored contact, I think your best option would be to actually create a new database. Just create a new table with columns [CONTACT_ID, YOUR_NEW_FIELD] and store that extra piece of information on the contact there. Also, that way you won't be polluting the contact's database with your app's proprietary information, which would have no meaning if the user removed your app.

Fetching contacts in android

I have inserted few contacts in through the android emulator.
I wanted to fetch the names, number,emails,etc. for each contact.
I understood that for fetching contact number we need to refer to
ContactsContract.CommonDataKinds.Phone.CONTENT_URI
instead of
ContactsContract.Contacts.CONTENT_URI
My question is how do i link both the query results so that i can aggregate a single contact and its attributes together ? It seems that only ContactsContract.Data.DISPLAY_NAME is common in both the results and the only attribute which can fetched in both the URI's without specifying in the query's projection.
Can anyone guide me ?
Thanks,
Adithya.
try these links:
How to call Android contacts list?
How to get contacts from native phonebook in android
How to obtain all details of a contact in Android
How to get the first name and last name from Android contacts?
How to import contacts from phonebook to our application
Android contacts extraction
How to get all android contacts but without those which are on SIM
Use select from ContactsContract.Data.CONTENT_URI and group results by LOOKUP_KEY. You'll receive a single cursor with phones, emails, etc., grouped by contact.
In onactivityresult we r getting cursor to the contact database to access various fields of contact database we have ContactsContract.PhoneLookup through which we can get index various db columns and then acccess them.

Looking for a clear answer on how to reliably maintain a list of contacts in my application

I've been reading for a couple hours now, trying to figure out how to maintain a reliable list of contacts in an Android application, but still cannot find one clear successful case.
My situation is this: I let users create Groups in my application, and in each Group, the user can select, from their contact list on the phone, which users they'd like to add to that group. I then need to be able to have a reliable way to compare a call from an incoming contact with my contacts database in my application, to see if they are in specific groups.
The easy parts are to add specific contacts to my database, and also to look up a contact based on their phone number. Thanks to this forum they are easy anyway! :) I'm storing the contacts in my database by Contact Name, Lookup Key, and Contact Id. The hard part for me, and this is what I cannot find a clear answer on, is how do I know that a month down the road, Contact X is going to have the same Lookup Key or Contact Id as when they were added? Couldn't they all change by then? Obviously the name can easily change, but can't the lookup_key and Contact ID also change? I've read about the lookup_key changing if contacts are manually aggregated. In other words, I am looking for the identifiable information for a contact that CANNOT change once they are entered?
I have read about using a ContentObserver to register for changes to the Contacts database, but I don't see that this helps me at all, since if I have Contact X with Lookup Key Y and Contact ID of Z, even if I get updated that the Contacts have changed, I still need to match Contact X in my application with Contact X in the Contacts database to update my info, which I still cannot do if the identifying information has been changed.
For example, I have a contact with Name, Lookup_Key, ContactID of Ted, 230ff392, 3209482. A month later, could it happen that what used to be Ted is now T-bone, 458ee247, 5502981? If this were the case, I cannot use these 3 identifiers as a means to look up the contact.
Thanks so much for the help on this!
Paul
I don't know where you read that the lookup key might change, but the documentation states that they are permanent in contrary to contact ids.
Contacts Provider / Contacts:
The ContactsContract.Contacts table also has the column LOOKUP_KEY
that is a "permanent" link to the contact row. Because the Contacts
Provider maintains contacts automatically, it may change a contact
row's _ID value in response to an aggregation or sync. Even If this
happens, the content URI CONTENT_LOOKUP_URI combined with contact's
LOOKUP_KEY will still point to the contact row, so you can use
LOOKUP_KEY to maintain links to "favorite" contacts, and so forth.
This column has its own format that is unrelated to the format of the
_ID column.

Categories

Resources