Android set contact to favorite programmatically - android

Can any one told me if I have a list of contact and read them in my application and I want to set contact to favorite from my application directly, so that when I open my phone contact again I will be able to find contact in the favorite list of android phone.
Please help

you must add permission to your application to be able to write to the contact content provider.
android.permission.WRITE_CONTACTS
android.permission.READ_CONTACTS
After that you need to update the value for the STARRED field.
ContentValues v = new ContentValues();
v.put(ContactsContract.Contacts.STARRED,1);
getContentResolver().update(ContactsContract.Contacts.CONTENT_URI, v, ContactsContract.Contacts.Data.DATA1+"=?", new String[]{putThePhoneNumberHere+""});

You need to update value STARRED in contacts database from 0 to 1.
Something like:
values.put(Contacts.STARRED, 1);
getContentResolver().update(Contacts.CONTENT_URI, values, Contacts.DISPLAY_NAME + "= ?", strNamevalue);
This is a sql query:
UPDATE %Contacts.CONTENT_URI% SET STARRED = 1 WHERE %Contacts.DISPLAY_NAME% = %strNamevalue%
Values in %% should be replaced by valid table name and where clause params
Hope it helps

Related

Update contact name in android 4.4

I would like to modify contact's name in an Android device (GT N5110) with Android 4.4. I have tried to do that by this approach:
ContentValues contentValues = new ContentValues();
String selection = ContactsContract.Contacts._ID + " = ? " ;
String[] selectionArgs = new String[] { Integer.toString(id) };
contentValues.put(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, lastName);
contentValues.put(ContactsContract.Contacts.STARRED, 2); // *
contentValues.put(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, lastName);
contentValues.put(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, firstName);
return this.context.getContentResolver().update(ContactsContract.Contacts.CONTENT_URI,
contentValues, selection, selectionArgs);
The line marked with * was the only one which modified the contact database. I also tried by applyBatch and added the MIMETYPE selection (ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE). The MIMETYPE approach throws some Exception with these message:
android.database.sqlite.SQLiteException: no such column: mimetype (code 1): , while compiling: SELECT _id FROM view_contacts_restricted WHERE _id = ? AND mimetype=?
I researched and tried suggestions present in update contacts display_name and Modifying contact information.
Can someone help me?
The following website explains it to you. It is a full tutorial by android itself. I highly recommend you use this website to first check if there is an answer to your query first before coming here.
http://developer.android.com/reference/android/provider/ContactsContract.Intents.html#EXTRA_FORCE_CREATE
What this website explains to you is
Send data to the contact you require to edit and it automatically opens the contact edit screen with the details which you passed to it.
If you want to know what all extras you can send to it. Visit the following link.
https://developer.android.com/reference/android/provider/ContactsContract.Intents.Insert.html
I am not sure if this is correct but it may be because of some security policy or so that you can't skip ui confirmation. But again I am not familiar with this so I may be wrong. I have not tested if this works or anything but I think it might help. Especially the second one:
http://developer.android.com/reference/android/provider/ContactsContract.Intents.html#EXTRA_FORCE_CREATE
And
http://www.techjini.com/blog/2011/10/31/insert-and-modify-contact-in-android/

Android native Contacts application deletes every account contacts

My problem is that I try to disallow to native Android Contacts application to delete my application's contacts (which are specified by my application account type) from device.
For that I've specified my own SyncAdapter with it's own Service and it's meta-data described in syncadapter.xml. The value of supportsUploading is set to false (this way I say that contacts created by my application are read-only)
However, when I try to delete a contact of my application from standard Contacts app I get the message which says:
You can't delete contacts from read-only accounts, but you can hide
them in your contacts list
Everything seems fine until I try to get data of the contact which I previously deleted ("hide") from standard "Contacts" in my own application.
The returned cursor is null because there isn't any row in Data table associated with this contact's RAW_CONTACT_ID. I also check if the contact exists on the device looking for
it's DELETED flag value in RawContacts table and observed that it has been set to 1 which means that contact has been deleted.
As official documentation describes:
Some sync adapters are read-only, meaning that they only sync
server-side changes to the phone, but not the reverse. If one of those
raw contacts is marked for deletion, it will remain on the phone.
However it will be effectively invisible, because it will not be part
of any aggregate contact.
So the problem is that I can't display this contact's data in my application any more because I don't know how to retrieve them from contacts database. If somebody knows how to handle this situation I would appreciate any advice. Thanks
So after more meticulous search I found the way to retrieve data for any RAW_CONTACT in my application independently of was it deleted from some other application or not.
Using of RawContacts.Entity API does this job.
Previously I tried to retrieve contact's data using such logic:
public Cursor getContactData(long rawContactId) {
return getContentResolver().query(ContactsContract.Data.CONTENT_URI, null,
ContactsContract.Data.RAW_CONTACT_ID + "=" + rawContactId, null);
}
And this method always returned null for deleted contact.
But using RawContacts.Entity such way:
public Cursor getContactData(long rawContactId) {
Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI, id);
rawContactUri = Uri.withAppendedPath(rawContactUri, RawContacts.Entity.CONTENT_DIRECTORY);
return getResolver().query(rawContactUri, null, null, null);
}
allows to fetch contact's data inside application with appropriate authority regardless was it deleted by 3d-party application or not.

How to retrieve the number of new contacts added to the contacts database

is there a column in the database of contacts indicates the date added to the base or the last modification date, because I want a method used to retrieve the new add
I found SEVERAL method but I did not understand, I am new to android
Edit : Means how to get the newly updated contacts.
you want to retrieve the number of new contacts since when?
if you adding contacts with your app then just get the number of contacts before you add any and save that value.
then after the user has added contacts get the number again and find the difference between the previous number of contacts. that will be the number of new contacts, but the best way to do it would just be to keep an internal counter of when the user adds contacts.
I think you are looking for something like this, You want to get the updated contacts.
have look here....
Contacts ContentObserver called randomly
Android notify when phone book is updated(Content Observer)
ContentObserver for listening contact changes
Just make this change to your cursor query
Cursor cursor = getActivity().getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, "_id DESC");

Retrieving all Contacts that have a specific Account attached to them

I am making a communication app that has been set up with a specific Account type in the AccountManager and has a SyncAdapter attached to it. Several of my contacts have a RawContact which has a MIMETYPE of my new account and some associated data.
I am looking for the correct way to query a list/cursor of every contact in a phones contact list that has one of these new accounts attached to it. I want to get the whole contact so i can do things like retrieve their phone numbers, display names and uris for pictures, but i only need the contacts that have my new type of account attached.
I have a feeling i need to join two tables together with some projection or selection parameters to get all of this data in one cursor object. Im just not sure how exactly to do this, though ive tried a bunch of different things already, namely being able to get all the contacts, or all the RawContact rows for my specific account type, but not both together.
Thanks
Get Raw_Id's of all contacts matching the account type and name:
getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI,
new String[]{ContactsContract.RawContacts._ID,
},
ContactsContract.RawContacts.ACCOUNT_NAME + " = ? AND " +
ContactsContract.RawContacts.ACCOUNT_TYPE + " = ? ",
new String[]{mAcccountName,mAccountType},null
);
For each _ID, fetch all rows from ContactsContract.Data , ContactsContract.RawContacts._ID matches ContactsContract.Data.RAW_CONTACT_ID here.
These rows have all the data related to that contact's Raw_id.
Refer to the documentation for the structure of ContactsContract.Data table, and what data it holds.

_ID column in Contacts database

I read that the different entries in different tables are linked via the _ID column in that table. For example a contact might have an _ID = 1 I get via
ContactsContract.Contacts._ID
and now I want to read the phone number of that contact using
Cursor phoneCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId , null, null);
//...
String number = phoneCursor.getString(phoneCursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
String id = phoneCursor.getString(phoneCursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone._ID));
This works fine, but what I would expect is that if the _ID of the contact is 1 that the _ID of the phone number is as well one since they belong together, but they are not equal. So the question is how does Android match these entries?
Thanks, A.
A contact corresponds to 1 or more raw contacts. The actual data for the contact is stored as
ContactsContract.Contacts.Data
Each data item contains the id of the raw contact it belongs to.
Each raw contact contains the id of the contact it belongs to.
So, given a Contact id you can find what raw contacts it represents. Get the raw contact ids and then find what data is within this contact.
you could get more details when you see the database tables
as far as my understanding goes this is the information I got from viewing android contacts2.db file using sqlite3 db browser
I created some 6 contacts for testing my android contacts
in contacts table the _id and raw_contact_id is actually same (it means that ContactsContract.Contacts provider )
in data table we get the real details of the person like phone number and email and firstname and last name using mimetype Id as the where condition
when considering data table use raw_contact_id as the where condition to get a particular records of the contact
ex my raw_contact_id is 1
I got 3 rows which consists of email, phone and display name in data1 column
you may ask that what if we want only phone or email or display name ....there you will need the mimetype_id as the where condition
to get phonenumber mimetype_id is 5
to get display_name mimetype_id is 6
to get email mimetype_id is 1
you may not understand all these stuff if you are a beginner ...but once if u see the internal tables u get every thing which I told you
access contact details only by contact_id or raw_contact_id and not by _id which is in every table ..that column is for different purpose ..
you can have a look at what Im saying in this image
http://img94.imageshack.us/i/tablesxa.jpg

Categories

Resources