Update contact name in android 4.4 - android

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/

Related

Detect if an android contact has been deleted

I am trying to maintain a contact database and get a callback for Add/Update/Delete as soon as something changes in the URI.
I have written a ContentObserver to observe on ContactsContract.Contacts.CONTENT_URI on contacts. I get a callback as soon as a contact changes and then I update my database by checking ContactsContract.Contacts.CONTACT_LAST_UPDATED_TIMESTAMP.
While this works fine for add/update, it does does not work for deleting a contact.
I do not want to parse all the contacts that I have in memory and check against android database. That would take time and CPU.
I know there exists many question of these types but I am not able to figure out things specific to deleting the contact.
Does there exist a way to perform this ?
As I have posted in above comment as well, following code works for API level 18 and above.
You can query on a uri ContactsContract.DeletedContacts.CONTENT_URI to get the list of all the contacts that have been deleted.
My query looks like following :
String selection = ContactsContract.DeletedContacts.CONTACT_DELETED_TIMESTAMP + " > ?";
String[] selectionArgs = new String[]{String.valueOf(mLastContactDeleteTime)};
Cursor cursor = mContext.getContentResolver().query(ContactsContract.DeletedContacts.CONTENT_URI, null, selection, selectionArgs, null);

value of FeedEntry.COLUMN_NAME_UPDATED while creating a sql database

I am creating a sql database in my app and I am following the documentation on the official developer guide of android, at the webpage
http://developer.android.com/training/basics/data-storage/databases.html#ReadDbRow.
I don't understand what is the meaning of the FeedEntry.COLUMN_NAME_UPDATED value.
What should is value be? What does it mean actually?
It's the name of the update column in the feed table ;)
// How you want the results sorted in the resulting Cursor
String sortOrder = FeedEntry.COLUMN_NAME_UPDATED + " DESC";

Android set contact to favorite programmatically

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

android: SQLite query returns no results

I am building an Android app that uses a SQLite database.
For this one task I have to run a query that looks like this:
SELECT item.id, item.price, t1.quantity
FROM item, (SELECT id, price
FROM list
WHERE list.state = 'sold') t1
WHERE item.id = t1.id
So far, I have tried:
Cursor c = resolver.query(uriRawQuery, null, selection, null, null)
where uriRawQuery is used to tell the ContentProvider that it should perform a db.rawQuery(selection, null) and selection is a string similar to the query above.
The problem is no data is returned into the Cursor. When I call c.moveToFirst() I get false.
The weird thing is that if I open the database file in SQLite Manager and run the exact same query I get results.
I know I can modify the query to make a join between the original list and item tables but I find it to be less efficient that way.
Any ideas would be very appreciated as I have spent too man hours on this already.
EDIT
I know what a join is, what I said is that it is a lot more efficient if I do it like this instead of using the entire list table.
I forgot a very important aspect
The WHERE clause looks like
" WHERE list.state = 'sold' and list.name like '" + arg + "%'"
where arg is a string.
I managed to solve the problem, I still don't know why this was happening but at least I got the Cursor to actually select the rows.
After many trials I thought about ditching the syntax above and write this instead:
" WHERE list.state = 'sold' and list.name like ? "
and move the argument in
selectionArgs = new String[]{arg + "%"}
I am going to wait a while before accepting the answer, in case someone provides an explanation as to why even though both queries look exactly the same they get different results.

How to update android sms conversation thread?

I wanted to update the "message_count" column and "snippet" column of a sms conversation thread, using this content:"content://mms-sms/conversations". Because the snippet and message count is not updated when an SMS is deleted from the conversation thread.
But I got this error message: "MmsSmsProvider does not support deletes, inserts, or updates for this URI"
Below is my code:
ContentValues values = new ContentValues();
values.put("message_count", ent.getValue().getCount());
values.put("snippet", ent.getValue().getSnip());
Uri conUri = Uri.parse("content://mms-sms/conversations");
getContentResolver().update(conUri, values, null, null);
I also try like this:
Uri conUri = Uri.parse("content://mms-sms/conversations" + "/" + threadid);
getContentResolver().update(conUri, values, null, null);
But what I got is another error message:no such column: message_count: , while compiling: UPDATE pdu SET message_count=?,snippet=? WHERE thread_id=334
Thanks for any help
There is no way to update the count, snippet etc of a thread directly. But I can help you with your actual problem. There is actually a bug in the android code, which is the root of your issue. When you try to delete in the following way -
getContentResolver().delete("content://sms/", "_id=?", new String[] {id});
Android does not update the thread associated with that sms.
The solution is is to do this
getContentResolver().delete("content://sms/" + id, null, null);
For those who are curious what the bug is -
Android code does something stupid like this, when you delete using method 1 -
Delete all messages given by the query.
Get all the conversation threads associated with the messages given by the query and update them. Whoa wat ?! Step 1 just deleted messages given by query so, step 2 is always going to return a null set for threads associated with those message, as all the messages are already deleted.
Hope this helps.

Categories

Resources