in this question : Android get a cursor only with contacts that have an email listed >android 2.0 DArkO said that has resolved problem of listing only contacts with email. But I still can't figure out what is that filter string he used. I saw someone was asking him in comment, but without any response.
Or maybe someone else know easy way to get contacts with email?
You can query Data table set where clause to mimetype = Email.CONTENT_ITEM_TYPE and get the raw_contact_id. Then using this raw_contact_id you can get any info about this contact.
Related
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.
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)
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.
I'm a little bit stuck with the new ContactsContract-API here. I think I don't fully understand the API and hope someone can give me a hint.
I red in the documentation that Android is aggregating several RawContacts to so called "aggregated contacts". Thats a pretty nice new feature with the 2.x Androidrelease.
What I'm not able to do is, to access this "aggregated contact". For example a user has a normal phonecontact with realname and phone saved in his phonebook and he has the same person as facebookcontact with his nickname and his birthday. Now he joins those two contacts and has his nice aggregated contact with the realname, phone and birthday which he can access.
My question: How can I as a developer access this aggregated contact?
I thought about getting all RawContacts associated with one Contact like this:
Cursor c = getContentResolver().query(RawContacts.CONTENT_URI,
new String[]{RawContacts._ID},
RawContacts.CONTACT_ID + "=?",
new String[]{String.valueOf(contactId)}, null);
But then I would not be able to decide which name is the "real name" of the Contact and other problems. And I disliked my other hackish ideas how to aggregated those RawContacts myself, because Android with the help of the user already did this job.
Any hint how I can query those aggregated Contacts is very much appreciated!
If I understand your question correctly, ContactsContract.RawContactsEntity may help you. Check this
This problem has now been solved in this thread:
Get all contacts and their details (e.g. postal address) in one OUTER JOIN query
Google should really document their contacts API, its really irritating to find out how to insert specific details.
Anyways,
I want to send following contact details to Contact native application of android:
Name
Last Name [Family Name]
Street Address
City
State
Zip Code
Contact Phone no.
I have figured out that Family name is stored in ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME
Anyhelp will really be appreciated. And if you know any other columns insertion beyond these mentioned. I am ready to buy it. Only condition is it should use ContactsContract i.e. above android 2.1 API Level 5
Thankx :)
To set the name, your code would be like :
ConactsIntent.putExtra(ContactsContract.Intents.Insert.NAME, firstName);
Try Adding a space in between first name and the last name.
ConactsIntent.putExtra(ContactsContract.Intents.Insert.NAME, firstName+" "+lastName);
ContactsContract.CommonDataKinds.StructuredPostal.STREET
ContactsContract.CommonDataKinds.StructuredPostal.CITY
ContactsContract.CommonDataKinds.StructuredPostal.REGION
ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE
ContactsContract.CommonDataKinds.Phone.NUMBER
You'll find all of them here.