Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8597669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:57:52+00:00 2026-06-12T00:57:52+00:00

(working code extract added below) My app needs to be notified of all inserts

  • 0

(working code extract added below)

My app needs to be notified of all inserts and deletes (and maybe updates, but less important) of contacts. This means when the app is started it will need a list of changes. While it is running it should be notified immediately (is it even possible to make changes to contacts outside the app while it is running?).

Should I be using a ContentObserver? Do I need a Service? Is there a way at app startup to get a list of changes that occurred since the last time the app ran?

Thanks.


ContentObserver does indeed work. However, for contacts, it does much less than I hoped for. You only get a notification that something has changed (in fact, you may get several notifications). You wont know what changed. Better than no notification though, I guess.

When you receive the notificaton, you’ll have to run queries to find out if any of the contacts you are interested in have changed. If you need to check all of them, I think you’ll be better off using a SyncAdapter.

Here’s the code I ended up using. First a ContentObserver subclass; this receives notifications from whatever provider you register with (see next block of code):

  class MainContentObserver extends ContentObserver
  {
    public MainContentObserver (Handler handler)
    {
      super (handler);
    }

    @Override
    public void onChange (boolean selfChange)
    { 
      Message msg = handler.obtainMessage();
      msg.what = CONTACTS_CHANGED; // const int declared elsewhere
      msg.obj = null;
      handler.sendMessage (msg);
    }
  }

Here’s the sceond block – this is the onCreate from your activity (or it could be in onResume). There are two important parts. One, I implement and instantiate a handler. This will receive “messages” from the observer, which runs in a separate thread, and relay them to my activity. The second piece is the creation of the observer, which happens through the register call.

  @Override
  public void onCreate (Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);

    // receive notices from our background threads.
    handler = new Handler()
    {
      @Override
      public void handleMessage (Message msg)
      {
        if (msg.what == CONTACTS_CHANGED)  // const int declared elsewhere
          System.out.println ("handler: contacts changed");
        else
          throw new IllegalArgumentException ("unrecognized handler message source: " + msg.what);
      }
    };

    // register content observer for contact changes
    contactsObserver = new MainContentObserver (handler);
    getContentResolver().registerContentObserver (ContactsContract.AUTHORITY_URI, true, 
                                                  contactsObserver);

    ... other initialization ...
  }

Finally, one more block of code – you need to unregister the observer or (I’ve read) you’ll have a memory leak. (If you regsiter in onResume, be sure to unregister in onPause.)

  @Override
  public void onDestroy ()
  {
    super.onDestroy();
    getContentResolver().unregisterContentObserver (contactsObserver);
  }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:57:54+00:00Added an answer on June 12, 2026 at 12:57 am

    I know there is no broadcast for what you want to do. ContentObserver is what you have to go with. Also check:
    Native contact change notification

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this working code, but now i need to be able to change
I had this code working yesterday, but it seems like I edited it a
Below is the code i use to extract data from a table to a
I am working with Optimizely guiders.js example code: http://jeffpickhardt.com/guiders/ Though if I extract the
This code is working perfectly: $(document).ready(function() { $(#submit).click(function(){ $(#output).load(extract.php, {query: $(#input).val()} ); }); });
Possible Duplicate: Parse PHP code to extract function names? I'm working on a script
I'm porting some (working) code from Linux to Windows 8. I'm using DDK. typedef
I have working code of js, it shows different div for selected option. <html>
I have some working code with a crutch to add BOM marker to a
This is my Working Code DriftMul:=99; WriteProcessMemory(HandleWindow, ptr($4E709C), @DriftMul, 2, Write); I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.