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 6698623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:35:59+00:00 2026-05-26T06:35:59+00:00

Some background information: When I run my application, a foreground activity displays cached information

  • 0

Some background information:

When I run my application, a foreground activity displays cached information from my local SQLite database and meanwhile a background service connects to server and gets some fresh information. When data is retrieved, the service fires up my ContentProvider’s insert method, to update my local SQLite database. When the insert is done, I notify my ContentObserver (foreground activity) about the change to refresh the view.

I know the insert works fine, because when I rerun the application manually I can see new data on my screen. Unfortunately, auto refresh does not work. Calling the method ContentObserver.notifyChange() fails. The weirdest thing is that it doesn’t even stop on the catch (Exception e) line. It goes straight outside the try/catch block. I have no idea what’s going on. LogCat, and console are quiet.

Here’s the part of code from insert method within my ContentProvider responsible for insert and change notification.

SQLiteDatabase sqlDB = db.getWritableDatabase();
try {
    long newID = sqlDB.insertOrThrow(StatsCollectorDatabase.TABLE_USERS, null, values);
    if (newID > 0) {
        Uri newUri = ContentUris.withAppendedId(uri, newID);
        ContentResolver contentResolver = getContext().getContentResolver();
        contentResolver.notifyChange(uri, null); // This is the line that fails
        return newUri;
    } else {
        throw new SQLException("Failed to insert row into " + uri);
    }
} catch (SQLiteConstraintException e) {
    Log.i(DEBUG_TAG, "Ignoring constraint failure.");
} catch (Exception e) {
    Log.i(DEBUG_TAG, e.getLocalizedMessage());
}

Here’s the code from onCreate method in foreground Activity:

Cursor cursor = this.managedQuery(StatsCollectorProvider.CONTENT_URI, null, null, null, null);
String[] columns = new String[] { StatsCollectorDatabase._USER_NAME, StatsCollectorDatabase._USER_DEVICE_ID };
int[] views = new int[] { R.id.name_entry, R.id.number_entry };

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this.getApplicationContext(), R.layout.list_item, cursor, columns, views, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
this.setListAdapter(adapter);

Intent intent = new Intent(this.getApplicationContext(), UsersDownloaderService.class);
this.startService(intent);    

I tried googling, but found nothing relevant. How do I fix this problem?

I can provide more code if this’ll be necessary.

  • 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-05-26T06:36:00+00:00Added an answer on May 26, 2026 at 6:36 am

    I solved it. My activity didn’t inherit FragmentActivity and my layout didn’t contain any fragments, so it could not work – there wasn’t any registered ContentObservers.

    Now it looks like this:

    First the main activity takes off, registering the fragment layout and starting the background service in it’s onCreate method:

    super.onCreate(savedInstanceState);
    setContentView(R.layout.list);
    
    Intent intent = new Intent(this.getApplicationContext(), UsersDownloaderService.class);
    this.startService(intent);  
    

    This is my list layout:

    <?xml version="1.0" encoding="utf-8"?>
    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        class="org.moyshe.cursors.ListViewFragment"
        android:name="org.moyshe.cursors.ListViewFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </fragment>
    

    Class ListViewFragment extends ListFragment and implements LoaderCallbacks<Cursor>. This is my ContentObserver i’ve been missing. If anyone’s interested on how it looks, please read this article – it helped me a lot.

    My Service and ContentProvider classes didn’t change.

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

Sidebar

Related Questions

As usual, some background information first: Database A (Access database) - Holds a table
First some background information: We have three environments for our EJB3 application: test, development
I'm trying to run some queries to a database from a pylons (paster-based) webserver
And if you do, can you give some background information on the implementation and
Some background info; LanguageResource is the base class LanguageTranslatorResource and LanguageEditorResource inherit from LanguageResource
Some background: I've created a Swing application which uses the Substance LaF (Thanks again,
Some background: My job involves maintaining a large multi-threaded multi-process C++ / C# application,
Some Background to begin: I've implemented a custom MembershipProvider that validates a user from
Some background: we have a windows application (c#) that locate in the system try.
EDIT: Update - scroll down EDIT 2: Update - problem solved Some background information:

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.