How to modify android's default (original) contact addition code - android

I am developing an app where I need to execute a piece of code whenever user adds a new contact. The solution for this is to use ContentObserver to get notified and then execute the code but for this I need to run the observer service in background which in turn will affect battery life of user's handset.
Instead can I extend the original contact addition code provided by android so that after storing contact in contact db it will execute my code. In this way task becomes efficient.
Is it possible?

Related

Should I store my contacts list in DB or Sharedpreference to avoid running the same task

I have my app such that when entered in a certain activity, I show a list of contacts in my phone( similar to Chat app where you see a list of contacts when trying to start a new chat).
So I just wanted to know about the implementation details of these task for best/ efficient work.
Is the contact list is fetched everytime from my device whenever I enter the activity(which is actually redundant, because we maybe doing same work over and over again,
But again we cannot rely on saved data in DB/SharedPref as contacts data is dynamic and bound to change,
so need your expert suggestion how to actually make this work in best possible way(I have already written method to fetch contacts- so whould I fetch the contacts afresh everytime or any other way around is there, what the popular chat apps implemet to show the contacts everytime?
Personally, I would implement the fetching of the contacts on a background thread (using coroutines) that is called periodically (perhaps each time they the activity is created, as you suggested, or maybe once when they open the app).
Because it's on a background thread, it would prevent them using the app (or any dropped frames while the activity is created). They might have thousands of contacts or be using a really slow device.
Presumably, you would want to store some other data associated with each contact: for example, if you opened up a chat with the user, you would want to associate all the messages with the user, so I would use a database such a realm rather than shared preferences.
So the entire process would probably look something like this:
Opening the app triggers a service that fetches all of the user's contacts on a background thread.
Service updates the database, perhaps removing contacts which don't exist and adding the new ones.
If you were handling this in a view model, your fragment or activity could observe the list of contacts and update the UI once it had changed.

Android - Listen to changes in android contacts and get the deltas

I'm trying to understand the whole concept of sync adapters from last couple of weeks and have few questions regarding the functionality.
I want to listen to all the changes in android contacts
Approach 1:
I have created a custom account and also created a sync adapter.
Written a broadcast listener listening to ContactsContract.Contacts.CONTENT_URI and in
public void onChange(boolean selfChange) of ContentObserver class i'm calling ContentResolver.requestSync(account,ContactsContract.AUTHORITY,bundle) which is acting as a trigger point to start the sync adapter when ever there is a change to contacts data.
Everything looks fine till here but in the SyncAdapter class i'm not getting any data in SyncResult variable in onPerofmSync
So how Can I get the delta changes from contacts?
Correct me if this is not the way to trigger sync adapter.
Approach 2:
I have followed some example where they were talking about querying the contacts database where dirtyFlag is set to 1.
I was able to get the changes but my question is
who will set that dirtyFlag value back to '0' ?
If I have to do that then lets say there are multiple applications on the phone which does the same thing. If some application sets the flag to '0' before my application tries to access the information. I'll be missing out on all the changes.
Approach 3:
Query the database and keep track of version for each contact in my own database and if 'version' has changed consider that as a change in contact. This looks like a heavy process.
Finally my question is what is the best way to listen to changes in
contacts on user's device?
What you can do is persist (possibly using a local MySql database) a mapping between Contacts.LOOKUP_KEY and its RawContacts.VERSION values.
Once you get a broadcast that a change happened in the Contacts DB (via onChange) you query for all RawContacts.VERSIONs and their Contacts.LOOKUP_KEY and compare it to the state your have persisted before, if one of them changed, you'd know that contact changed.

Android Wear DataLayer API usage for synchronizing when phone app is closed

So, I want to learn this synchronization strategy instead of just using the simpler MessageAPI, but am really struggling with how to successfully implement this.
My project is like this: I make queries to download a small amount of text from an API, via my phone. I will make these queries every so often, haven't really decided on how often just yet. The data will update the watch, which should hold onto the last data received. After that first download occurs, I send data using a DataMap, to the Android Watch. I only send that once, because I believe that sets up a channel to continually send updates when ready. If that is wrong, please correct me.
My main question is this: what if the Android phone's app closes? Then the data object goes to null, and gets sent to the Watch as null? Or, should I send an object from a long-running service or shared preferences on the Android phone, so that the object is never null?
Think of the Data Layer as more of an event system, i.e., you update your data and you're notified on the other side when the data is updated (created, changed, or deleted). You don't have to worry about if the Activity is killed after that. Even if the data was 'deleted', you would be notified it was deleted.
On the Wear device, you would listen for the changes via a Service or Activity and update UI, DB, etc. accordingly.
It probably make sense to read through this Android training guide. (It isn't too long.) The Handling Data Layer Events section is probably the most useful.

Android SQLite adding contacts 'dynamically'

In my Android app, an Activity performs a query to get the contacts on the phone and stores them into a sqlite database. For now, this is performed only once during the first launch of the Activity because that operation takes about two seconds to execute (which is actually a lot of time)
However that workaround comes with an issue: if the phone user adds some contact my app will never be able to get them. Then, I was wondering if it was possible for my app to ´listen ´ for that event and add the contact in the database when it occurs.
Is such a thing possible?
You can do it by using ContentObserver.
You have to simply
Implement a subclass of ContentObserver
you have to register your ContentObserver to listen the change.
For more detail how to do it you can follow this article Use Android’s ContentObserver in Your Code to Listen to Data Changes

How can I observe for any changes to a contact

In Android, how can I observe for any changes made to a contact?
I know I need to register for a content observer? But what should be the URI for passing to the registerContentObserver function?
Here is what I tried:
registerContentObserver( ContentUris.withAppendedId(People.CONTENT_URI, personId), true, myContactsObserver);
But I don't get notified when I change the phone number of the contact.
Thank you for any pointers.
More information would make it easier to figure out.
I wonder if it's because your Activity is no longer running when the contact changes. If you're changing the contact through the built-in Contacts application, that would imply that your activity is no longer at the top of the stack, so there's no guarantee that it's still resident.
You might try registering the contact observer in a service that you explicitly start and stop.

Categories

Resources