I need to delete the duplicate contacts and then inserting the new contact on Android 2.2.
How to do this?
give me any sample code or sites for this.
To delete the content item from android you need a content URI and some deletion criteria.
Every content type has its own content URI. If you're writing your contacts sync adapter, then you might want to use ContactsContract.RawContacts.CONTENT_URI.
Other thing you need is a ContentResolver - an interface to communicate with a content provider (an operations, such as insert, update and delete are defined in this interface). You can get ContentResolver by calling getContentResolver from your application context.
So, here is the snippet of code that should delete ALL the contacts (not tested it though):
ContentCesolver cr = getContentResolver();
URI uri = RawContacts.CONTENT_URI;
cr.delete(uri, null, null);
Note that when you're using RawContacts.CONTENT_URI, the contact item is not deleted. Instead it is only marked for deletion. To delete it completely you should add a ContactsContract.CALLER_IS_SYNCADAPTER parameter to your URI:
uri.buildUpon()
.appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER,
"true").build()
For further explanation read official docs about content providers.
May be this will help you,
How to remove a contact programmatically in android
Related
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);
I am baffled by notifyChange machenism used in contentProvide:
// Tell the cursor what uri to watch, so it knows when its source data changes
c.setNotificationUri(getContext().getContentResolver(), uri);
and
getContext().getContentResolver().notifyChange(noteUri, null);
Here are the questions (suppose the provider and the client are in different packages):
Is the contentResolver returned by Provider the same as Resolver returned by the client?
Is the cursor the same as returned by the provider and the client?
what is the Uri resolution to be notified changes? the entire table uri or a row?
Please clarify
I know this is an old question. I am answering so that it is helpful for someone who comes across this question.
Is the contentResolver returned by Provider the same as Resolver returned by the client?
Yes, it is the same.
Is the cursor the same as returned by the provider and the client?
I suppose, you are asking this in the context of your first code snippet which is seen inside query() method. Here, you are setting the notification uri on the cursor which is to be returned to the client. So, obviously, the client and the provider are using the same cursor.
what is the Uri resolution to be notified changes? the entire table uri or a row?
Uri for notification depends on the operation:
insert() - The notification Uri is the table Uri appended with the
id of the newly inserted row.
delete() and update() - Here, the Uri is the table Uri appended with the id of the deleted or updated row.
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.
I'm developing an app which requires threaded sms. I was able to retrieve contents from inbox, but in the threaded view sms must be filled with both inbox and sent items.
Separately both content://sms/inbox and content://sms/sent are working well.
How do I join contents from two URI's and order by time?
Can I use content://sms/all?
Null value is returned for cursor when ALL CONTENT URI is used.
How to do this?
At last found the answer for this..
content://sms/all
is something which i couldnt find.
But for retrieving both sent and received we can use
Uri selectUri = Uri.parse("content://sms/");
Cursor cur = getContentResolver().query(selectUri,null,"thread_id="+threadid, null,"DATE desc");
This snippet fetches and displays in descending order
Thanks all
I had the same issue. For this,you can use MatrixCursor.What I have done is-
Get all sms from content://sms/inbox for a thread_id
Get all sms from content://sms/sent for a thread_id
Maintain an arraylist and sort them in the order you want(I did this using bubble sort)
Now define and initialize the matrixCursor
(Refer this: http://groups.google.com/group/android-developers/browse_thread/thread/470dd3a1703848eb/d7e70618ce413261?q=MatrixCursor+join+two+tables for MatrixCursor )
Add all the sorted records to your matrixCursor
(Please note that adding this record should be in the sequence of at what time and from which folder(inbox or sent) they come.MatrixCursor simply lets you create a custom cursor so you need to maintain the sequence.)
I got following problem, I need to use a Content Provider to read a
Database of an other App.
first I want all rows, and after analyzing the data only e.g. the rows
from _id = 1, 3 and 5.
how can I call a Content provider and select only these rows?
or is it possible to create a subset Cursor form an given Cursor?
Thanks in advance.
If you're talking to another app, I assume you're querying the other app's ContentProvider to get the data from them in the first place.
In this situation, the cleanest answer seems not to build your own ContentProvider that filters/wraps theirs. Instead query their ContentProvider from your application directly, and use the select clause in your query() to specify the conditions that define the subset of data you want to be given.