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

  • SEARCH
  • Home
  • 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 7568877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:02:14+00:00 2026-05-30T15:02:14+00:00

I have read this helpful post : SharedPreferences.onSharedPreferenceChangeListener not being called consistently However I

  • 0

I have read this helpful post :

SharedPreferences.onSharedPreferenceChangeListener not being called consistently

However I am having no luck. I am trying to create an OnSharedPreferenceChangeListener that runs in a service. Everything is implemented correctly but the listener is not always triggered.

public MyServiceOne extends Service {

    public SharedPreferences mSharedPreferences;

    // Listener defined by anonymous inner class.
    public OnSharedPreferenceChangeListener mListener = new OnSharedPreferenceChangeListener() {        

        @Override
        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
            Log.d("debug", "A preference has been changed");            
        }
    };

    @Override
    public void onCreate() {            
        mSharedPreferences = getSharedPreferences(MySharedPreferences.NAME, Context.MODE_PRIVATE);
        mSharedPreferences.registerOnSharedPreferenceChangeListener(mListener);
    }

    @Override   
    public void onDestroy() {
        super.onDestroy();
        mSharedPreferences.unregisterOnSharedPreferenceChangeListener(mListener);
    }
}

UPDATE

The problem stems from a fact I did not mention. I am running two services and when shared preference changes are made in MyService2 nothing is triggered. In the manifest I define the services to run in different processes. Is there a way to make this work?

  • 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-30T15:02:16+00:00Added an answer on May 30, 2026 at 3:02 pm

    After gaining some experience with Android I have come back to answer this question and help anyone else who is having trouble with this type of situation.

    The implementation of the OnSharedPreferenceChangeListener is correct. The problem lies in the manifest and application architecture. Because the services are running in different processes Android has created difference Dalvik Virtual Machines for each service therefore they do not “hear” each others listeners.

    This was just bad design – The better way to approach the idea of running two concurrent services is to create one service that threads each “service” off to run concurrently. This way they can share the same Heap thus sharing the same Objects & Listeners

    If someone were determined to use two services they could create a BroadcastReceiver to catch intents for that preferences need to be changed – or communicate through sockets. There are also ways to do this with a ContentProvider (Please NEVER DO THIS). But again, there is no reason for any of this if the design is good.

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

Sidebar

Related Questions

I have already read this post , but I am not sure know to
I have read this post about how to test private methods. I usually do
I have read this question but it's not quite what I was looking for.
this is my first time posting on here and have read alot of helpful
I have read this article from High Scalability about Stack Overflow and other large
I have read this article about 400% boost of your website . This is
I have read this. Jquery checkbox <input type=checkbox name=checkGroup id=all> <input type=checkbox name=checkGroup id=one
First let me say I have read this useful article thoroughly and am using
I have read over this which sort of gives an explanation of when you'd
Possible Duplicate: What’s with the love of dynamic Languages I have already read this

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.