How to synchronize all the phone contacts with my app - android

In my application I want to show an option like Sync Contacts with App.
It should synchronize all the contacts with my app (Just like Facebook does when we go to the Contact tab of Find Friends page).
So when all the contacts are synchronized, I'll match the contacts with the server and the latter will return all the members which are in my contact list as well as those ones registered on the server but not in my friends list.
So can anyone help me?
How to synchronize the device contacts with my application?

For Syncing the contacts, you will require to get all the contacts from your contact list. Then you need to get the contacts from your server and compare contacts from both the places and choose the common ones.
Here is code for getting contacts from your Contact list.
public void getPhoneNumbers() {
String PHONE_NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER;
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, new String[]{PHONE_NUMBER}, null, null, null);
ArrayList<String> phones = new ArrayList<>();
while (cur.moveToNext()) {
String number = cur.getString(0);
number = number.replaceAll(" ", "");
if (!phones.contains(number)) phones.add(number);
}
cur.close();
}
Since i don't know your server side code, i cannot help you in getting your contacts from the server but a simple query should do the job and matching the contacts will require for loop inside a for loop.

You should create sync adapter and inside that you should sync and observe content provider for contacts.
For detailed explanation you can take reference from below reference.
http://blogs.quovantis.com/syncing-contacts-with-an-android-application-2/

You should check official docs, it's pretty well explained there, and not very difficult. Then, if you still have some problems when implementing it, just come back here and ask for a concrete help with your code.
Hope it helps. Thanks.

All you need is a SyncAdapter, if you want to sync your contacts with your application. If you don't add it, then Contacts won't be shown. When you add the sync adapter you will also need an Authenticator.

So you mean you want to sync the local data with the server data?
Build the connection between local data and server data.
Load from server and check the local data./ Upload the local data to server and the server check the data and mark it.
It depends on you situation. Just like the relation. You can use the phone number to build the connection between two person. If Jack has registered, so your server has Jack's phone number and his id. When Mary register, the client upload mary's local contacts. Then the server check the data. Having found Jack's phone, that means Jack is Mary's friend. So when Jack or Mary load the 'Frinds', it shows the data.

Related

how to display device address book ordered by custom external data

In my android application I would like to display the mobile user's contacts (names, profile picture ) displaying first the contacts that have already installed and registrered that application (the matching is made by contact's email).
Trying to loop over each contact and match if their email is already registered (in an external SQLITE table) don't seems to be an efficient way.
I would like to directly add (somewhere in the address book ?) the extra data "isRegistered = true/false) and order my addreess book query by this value to scroll the address book.
Is it possible? How to implement this in detail ?
OPTION 1
I think the most efficient way would be what you thought about initially, with a slight improvement:
store the list of registered emails (for the user's contacts) in a local SQLite DB.
read the entire list of emails on application launch, and store them in a HashSet
When sorting the contacts, create a custom Comparator to first check if the contact is an app user or not, and only then fallback to name compare.
OPTION 2
If you still want to check the option of storing the custom value in the Contacts DB, and integrate it into your query, you need to create a SyncAdapter.
This is basically a service that is able to sync contacts to/from a server into your own RawContact, which is then aggregated into one-or-more existing RawContacts, like Google does for Google Contacts.
You can set it to be notified when a new contact is added, and have your SyncAdapter add the needed info to the contact so it'll show links to your app.
If you go to your phone settings > accounts, you can see Whatsapp and Google's SyncAdapters there, where you can turn them off/on.
To create a sync adapter, you can follow the official docs, or this great tutorial.

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 Updated Contacts in Android for Contact Sync

I am working on Contact Sync in Android and I have successfully done with First time Contact Sync.
Here is what I am doing.
1. Fetching all Contacts and Saving each contact in DB with Contact._ID
2. Fetching Names and Phone Numbers and saving in DB.
After that I am sending my contacts data to server so that server can be updated.
Now the problem is how can i check whether my particular contact is updated or not ?
I have implemented Broadcast Receiver so that my app can get intimated about the updation/Addition/Deletion of Contact. But I want to tract particular contact.
I found one solution which is Dirty Flag. It tells us about the contact whether its updated or not, Here is the reference link : http://developer.android.com/reference/android/provider/ContactsContract.RawContacts.html
But I am not able to use this Dirty Flag, Could someone please help me by implementing Dirty Flag.
Thanks in Advance !
The contacts should have the value ContactsContract.RawContacts.VERSION.
If you save this version on the server (or in a database in your app), you can determine if a contact has changed since it was last sent to the server.
You will get number of contacts updated after a particular time from contact data base using below query.
ContactsContract.Contacts.CONTACT_LAST_UPDATED_TIMESTAMP
,try to compare the last uploaded time to your server with this time .Hope then you will get the list of updated contacts after the successful upload to server.
check this
http://developer.android.com/reference/android/provider/ContactsContract.ContactsColumns.html#CONTACT_LAST_UPDATED_TIMESTAMP
When I did I used the ContactsContract.Contacts.CONTACT_LAST_UPDATED_TIMESTAMP, but I had problem to figure out that it was is milliseconds, I was using a column to get the new files and it is in seconds (MediaStore.MediaColumns.DATE_ADDED) so pay attention to use the correct number.
Uri uri = ContactsContract.Contacts.CONTENT_URI;
Cursor cursorNew;
cursorNew = contentResolver.query(uri,
null,
ContactsContract.Contacts.CONTACT_LAST_UPDATED_TIMESTAMP + ">=?",
new String[]{lastUpdate},
null // Ordering
);
'lastUpdate' is a string with my timestamp
Another thing to pay attention is this Contact's column just appeared on API 18, the latest Jelly Bean version, so to solve this problem once and for all I just changed my gradle file.
As you said that you are storing Contacts._ID in your local database and fetching phone number and other info associated with contact from the Contacts db.
I would suggest you to store the data from the RawContacts table into your local mapping and check the entries associated with the raw_contact_id in the data table of contacts you will get all contacts easily.
To check the modified or deleted contacts
With the raw_contact_id present in your local table check mapping with the data table that if contact field associated with the raw_contact_id's version is changed or isDeleted field is changed (i.e; Contact is deleted from contact application)

What's an efficient way to compare phone contacts to MySQL database? (Similar to WhatsApp)

I am trying to determine which contacts from a user's phone are using my app, similar to what WhatsApp does. I have the list of contacts in a Cursor and am trying to figure out an efficient way to compare the list of contacts to my database to see who is using the app. I could do it like this:
//for each contact
while (this.cursor.moveToNext())
{
String phoneNumber = cursor.getString(2);
String sanitizedPhoneNumber = phoneNumber.replaceAll("\\D", "");
//query db to see if user exists
boolean userExists = restClient.checkIfUserExists(sanitizedPhoneNumber);
//save all results in hashmap
//"phoneNumber":"exists"
//"5556781234":"true"
}
Doing it like this, there could potentially be hundreds of API calls to my web service in a row depending how many contacts there are, which is why I'm looking for an alternative. Is there a better way? Should I be using SyncAdapter?
There may be a better way, but you could first build a list of phone numbers on the device, and then make a single API call to an endpoint that takes in this list. Have the endpoint then respond back with a list of known numbers. This way, you're making only one HTTP request.

Custom Contacts List in Android

I am looking to build a local Contacts list in Android. The user selects, say 3 contacts, from the address book and the code needs to save this list locally. The list should include Contact name, mobile number and email address. The user should also be able to edit this list. I am new to Android and struggling to get a solution for this.
The app I am building needs a user to create a local contacts list. I need to be able to extract the full contacts list from the Address Book and then the user gets to choose a limited number of contacts which get saved for later retrieval/modifications.
try Retrieving a List of Contacts in Android Developer.
This link may helps you to retrive the contact list.
Replace book objects with contact objects in the following tutorial and it might be useful...
http://stirfriedcode.blogspot.jp/2013/11/getting-specific-data-from-sqlite.html
I tried a new workaround and adapter-free solution . It's worth to have a look at :
https://github.com/draxdave/ConstraintAccordion

Categories

Resources