How to cancel contentresolver applyBatch? - android

I've built an application that requires to export 2000 contacts to native Android Contacts. I use contentresolver.applyBatch to make this happen, but if the user logs out I want to delete all of the apps exported contacts. It can occur that the contentresolver is not fully finished with saving the contacts.
My question:
How can I cancel the following opperation?:
resolver.applyBatch(ContactsContract.AUTHORITY, ops);
What I've tried so far:
I've tried to cancel the sync of the contentresolver like this:
ContentResolver.cancelSync(null, ContactsContract.AUTHORITY);
This doesn't work and I'm also not sure about the null value. It says it needs an account to stop syncing it for. But I'm not starting the sync on a specific account so why would it be needed?
What I need?
Something like resolver.stopBatch (a way to stop applying the batch).
Thank you for all your effort reading this.

If I understand the use case, you're syncing in new RawContacts into the device, when a user logs into your app.
If that's the case, you should create a SyncProvider, and have it sync in RawContacts on an account that is tied to your app and package name.
That way, when a user uninstalls your app, the account is automatically removed along with the data you've synced into the device, and when a user logs out of her account, you simply remove the contacts account programatically, which will also remove all data synced to the device.
Read these tutorials on how to create a contacts account, a SyncProvider and use it to sync contacts on your account:
http://blog.udinic.com/2013/04/24/write-your-own-android-authenticator/
http://blog.udinic.com/2013/07/24/write-your-own-android-sync-adapter/
https://developer.android.com/training/sync-adapters/creating-sync-adapter.html

Related

Android contacts sync in background

I want to show the contacts whoever using my app. I know this is an old one but nothing satisfies me. I'm already using the method that gets all the contacts from phone and send to server and checking the available contacts who all are
using my app then return to my app.
Finally, it will save into the local database. But the problem is when adding new contact we have to sync again. For that time it will take more time and other functionality may become slow.
What I need is when I open the app at first time it should sync all the contacts and then it should monitor the changes like add, delete and update of the contacts then only it should check the changed contact.
I refers this blog, but there is no proper code. So many files are missed. How can i sync contacts?
finally,I have done with this library.

Contact sync adapter in android

I want to use sync adapter in my application to sync the native and third party contacts (except FB) with server. ( Only client to server one way sync)
I have two doubts here -
1) If there are multiple sync adapters in the device and If dirty bit is already cleared by another sync adapter , would my sync adapter be able to detect the contacts change/update immediately.
2) I have used
ContentResolver.setIsSyncable(account, ContactsContract.AUTHORITY, 1);
ContentResolver.setSyncAutomatically(account, ContactsContract.AUTHORITY, true);
to sync the contacts with server .
It does start the sync in every 30 seconds .
But I want it to get notified immediately when ever contacts are added Or Deleted or Updated to start perfromSync operation.
Do I need to use observer too ?
In order to see changes to the contacts you will have to register an observer with the ContentResolver. The sync API is not intended to watch for every change, it is intended to be a background API that Android runs periodically automatically for you, so that it can batch operations and use the network more efficiently, not run when network is disabled, etc...
The dirty bit is used on a per account basis. In general the sync is designed to have an "owning" account per contact, and is not designed for exporting all the contacts from the device to a particular service. There is an aggregation process that handles joining contacts from multiple accounts into one "Contact" as seen by the user. See:
http://developer.android.com/reference/android/provider/ContactsContract.RawContacts.html
For more information on how this works.
If you intend to export all contacts on the device to your service you will need to walk the contacts and insert a raw contact for your service. You will then also want to watch the ContentResolver with an observer to see when the user adds a new contact on some service or periodically walk the table looking for contacts you have not yet exported.
use the version bit. If any contact edited
It automatically increased by 1.
so you create local database for compare previous version to current version. If any changes happen you trigger the update query.
for more information visit here.
This version helps for my sync updates. So I share this answer for all.

android check if a contact installed an app android (like whatsapp)

I want to let my app scan the users contactlist , and display the name of the contacts who also installed the app.
I have no idea where to begin with this . so if someone could help me getting started , that would be apreciated.
grtz
You will need to research ContentResolver because you will have to query the database containing the contacts. To do these queries, you will need the URI and column/header names for contacts db which can be found in the Contacts class.
You will also need users to register that they have your app on your own servers and the registration will need a piece of information about the user that other users would have in their contacts (the users phone number, email address, etc).
You will then need to query a few users at a time from the users contacts for this piece of information (using the ContentResolver and Contacts class), pass it to your server (as text) where it will check if they are registered, then send a message back to initial user (and possibly the match) that you found a match.
That's where you should start, and end :)
Note, you will need these permissions in the Manifest file:
android.permission.INTERNET
android.permission.READ_CONTACTS
First of all, welcome to StackOverflow. StackOverflow is for programming questions. As is such, a post asking for help should always provide a description of what you have already tried. If you haven't tried anything yet, I highly encourage you to try something, and post when you encounter a problem.
As your question stands now, we don't know what the problem is. Are you having problems scanning a contact list? Or are you having problems trying to figure out if another user has installed the app?
That being said, you will have to maintain a database for your app containing a list of people who have installed your app. When your app is launched on one device, do whatever you need to do to register the user in your database. Due to the nature of a "contact," one person's phone may only have their phone number, email address, or even just a name. This is of course something you will have to deal with in your implementation.

What if I do not want to sync contacts?

In an Android app I'm working on, I'm using the AccountManager to store the user's account info and also sync a couple of sources (I have two services that respond to the android.content.SyncAdapter intent action).
However, I am not syncing contacts. In spite of this, if I go in the contacts app and choose to add a contact, I get a popup asking me under which account should the contact be created. The account created by my app is listed there as well. I have tried putting android:supportsUploading="false" on my <sync-adapter>s, doesn't work.
The only solution I currently see is to create an empty, invisible contact sync service that has android:supportsUploading="false", but that seems like the biggest and most useless hack ever.
Help?
It appears to me that you have to specify both android:supportsUploading="false" AND your content authority must be android:contentAuthority="com.android.contacts" (ContactsContract.AUTHORITY).
See com.android.contacts.model.Sources.queryAccounts(), which fills in the .readOnly flag, and com.android.contacts.model.Sources.getAccounts(), which is used by com.android.contacts.util.AccountSelectionUtil.getSelectAccountDialog() to populate the contacts dialog.
What's not clear to me is whether this has additional (potentially undesirable) side effects -- particularly given that you're specifying a contentAuthority that is conceptually the opposite of what you want.
-Tim
if you do not want to sync contacts change the authority of your syncadapter's xml file to the authority of your apps contact provider . If you are not maintaining a contacts provider for your app. Just make a dummy provider and gives its authority to your sync adapter's xml file. Giving authority as com.android.contacts in your syncadapter's xml file will cause this behavior as you mentioned :
"However, I am not syncing contacts. In spite of this, if I go in the contacts app and choose to add a contact, I get a popup asking me under which account should the contact be created. The account created by my app is listed there as well. I have tried putting android:supportsUploading="false" on my s, doesn't work."
doing this will solve this issue

Contacts backup and sync application

I am working on a contacts backup and sync.
The target is to send all details of all contacts to a server (custom protocol defined) as a backup.
During initial launch of application, all the contacts will be queued and sent to the server.
And after that, a background service will be running all time which will listen to new contact addition/ contact update, and this new/updated contact will be sent to server by the background service.
I am using RawContactsEntity for fetching the records.
I tried using ContentObserver on RawContacts/Data tables to get notification of contact addition or contact-change. But, AFAIK, the ContentObserver gives notification (onChange()) of changed data in table as a whole and not ID of individual record.
Now my problem is, how to get the exact id's of changed/new records?
I thought of creating a backup-table to compare with native contacts table and get the changed records. But as the number of contacts increase, the performance will decease drastically and this will hamper the battery life too.
Can you suggest me, The best way for achieving this contacts backup operation from performance and memory usage point of view?
Is there any other way for contacts sync operation?
It would be very helpful if anyone can share examples which can help me in this.
If the contacts are stored within your account(AccountManger), Android will mark the dirty flag in raw contacts. If it is not your account then you can not trust the dirty flag as the accounts sync adapter might have updated the contact to the server and reset the dirty flag. Your only option is to either re-upload the full contacts(simple and easy to code) or keep track of the version column in RawContacts and check which one has changed. It is actually not recommended to copy and upload contacts from other accounts as the corresponding sync adapter will anyways maintain a backup of those. Like Google will have a copy of Google contacts on their server.

Categories

Resources