Android : Detect new Contact addition


Android : Detect new Contact addition

All of us have added new contact in our mobile’s address book. Sometimes developers like us have also added contacts programmatically. This article is all about detecting that new addition of a contact in contact book. Means, whenever a new contact is added in phone book, our android app will get notified and work accordingly(namely storing, syncing etc.). Lets discuss on this interesting topic.

RECEPIE

  • First of all we will create our activity.
  • Next checking of permission related to contacts.
  • Then we will create a Observer class.
  • Create a new service where we will add content observer.
  • Start the service to detect new contact addition.

Discussion and Code

As mentioned above, we have created a new activity ContactWatchActivity which is our launcher activity. Here we will check for contact reading permission and initiate the service.

In our AndroidManifest.xml we will add a permission i.e. READ_CONTACTS to get access of user contacts in phone book. It will look like

Next, we will add two more packages i.e. Services and Utils to keep the code well structured. We will add our Service class and Observer class here.

CONTENT OBSERVER for New Contact

Now the question is what is Content Observer and it purposes? It’s an observer that receives call back for changes to content. The content may be contacts and images, call logs, user-defined or locally saved data It listens to changes of your contents if implemented properly. To use it we need two main things to do

  1. Implement subclass of ContentObserver
  2. Register content observer to listen to the changes made.

So, to detect our contact changes we will create our class MyContentObserver inside Utils package extending ContentObserver as super class. Here we will override onChange method and write our logic inside it. It simply provides uri and a selfChange parameter to detect changes.

Our trick is to query ContactsContract.Contacts.CONTENT_URI and move to last position to get the newly added contact. Yes, it’s that simple. After that we will check if it is a valid contact and then Toast it out. So, the code for the MyContentObserver is

SERVICE

Next we will add our service class in Services package where we will register our ContentObserver. We are using this service class to run it in background. It is mainly helpful if anyone wants the service to run in background even after the app is closed. The service class will look like

For reference related to service class like this please read this article.

After adding the service we need add it in our AndroidManifest.xml. Below is the full manifest code.

Now, in our launcher activity(ContactWatchActivity) we will start the service after checking permission for READ_CONTACTS. See the following code snippet.

Download and Run

That’s all. Now run your app and minimise it. Then add a new contact to your phone book. If everything is fine then you will see resulting Toast message.

You can find the full source code here.

If you find this article helpful, spread it then! Share it to inspire us. Subscribe us to get more stuff like this.


Author
Abhishek is a developer cum blogger working more than 4 years. He loves programming especially open stack technologies. He has decent knowledge in Android development, Wordpress, MongoDB, Node.js and so on. Beside this Abhishek finds himself busy in painting, front-end designing.

Follow Abhishek


Previous


Kotlin in Android Studio
Setting up Kotlin in Android Studio

Next


Campaign measurement install referrer
Install Referrer for Android using Google Play Campaign Measurement