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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:35:29+00:00 2026-05-28T13:35:29+00:00

I have an IntentService which updates a preference like this: SharedPreferences.Editor editor = userPrefs.edit();

  • 0

I have an IntentService which updates a preference like this:

SharedPreferences.Editor editor = userPrefs.edit();
editor.putInt("COUNT", intCount);
editor.commit();

In my main activity I am listening for preference changes and update a TextView

userPrefsListener = new SharedPreferences.OnSharedPreferenceChangeListener() {

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {

  if(key.equals("COUNT")) {

    final TextView txvCounter = (TextView) findViewById(R.id.TXV_COUNTER);

    if(txvCounter != null) {

    SharedPreferences userPrefs = getSharedPreferences("USER_SCORE", 0);
    int intCount = userPrefs.getInt("COUNT", 0);
    txvFishcounter.setText(String.format("%03d",intCount));
    }
  }
}
};

userPrefs.registerOnSharedPreferenceChangeListener(userPrefsListener);

For Android 2.3 everything works fine, but for 2.2 I’m getting CalledFromWrongThreadException everytime the OnSharedListener is triggered.

Thanks for your Help!

  • 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-28T13:35:30+00:00Added an answer on May 28, 2026 at 1:35 pm

    The reason why the CalledFromWrongThreadException is raised, is because the OnChangeListener is not supposed to be called from the IntentService.

    What you could do though, is sending a Broadcast (where you can actually include the value as well).

    In case you are only using the SharedPreference for communication, you can replace it entirely (and I would recommend this, as SharedPreferences are wasting Write Cycles).

    You could use a code like this for sending a Broadcast:

    /**
     * Send an Intent with the Broadcast, a permission and a Bundle
     * 
     * @param context
     *            A context to use
     * @param broadcast
     *            String to use, eg. "de.bulling.smstalk.ENABLE"
     * @param permission
     *            Permission needed for receiving
     * @param bundle
     *            Extras to attach
     */
    public static void send_broadcast(Context context, String broadcast, String permission, Bundle bundle) {
        //SettingsClass.log_me(tag, "Sending broadcast " + broadcast);
        Intent i = new Intent();
        i.setAction(broadcast);
        if (bundle != null) {
            i.putExtras(bundle);
        }
        if (permission != null) {
            context.sendBroadcast(i, permission);
        } else {
            context.sendBroadcast(i);
        }
    }
    

    You should include a custom permission as well, so other apps don’t get the Broadcast, but it is not necessarily needed.

    To receive the Broadcast, register a Receiver in your Activity, eg

    private final BroadcastReceiver receiver = new BroadcastReceiver() {
    
        @Override
        public void onReceive(Context context, Intent intent) {
            Bundle b = intent.getExtras();
            doSomething();
        }
    };
    
    
    
    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        IntentFilter filter = new IntentFilter();
        filter.addAction(YOURBROADCAST);
        registerReceiver(receiver, filter);
        [...]
    }
    
    @Override
    public void onDestroy() {
        unregisterReceiver(receiver);
        super.onDestroy();
    }
    

    Also you should consider using Handlers, so your methods in the OnChangeListener call are run by the MainUI thread.

    Example:

    Handler mHandler = new Handler();
    Runnable myCode = new Runnable(){
         @Override
         protected void onRun() {
              yourCode();
              }
         };
    mHandler.run(myCode);
    

    This has also the advantage of running the code delayed with runDelayed(), so you don’t have to use sleep, and you UI will be still responding.

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

Sidebar

Related Questions

In my application I have an IntentService that reads rss feeds. I would like
I have class that extends 'IntentService' - this class occasionally receives data from a
I have an IntentService which queues up web service calls to be made. I
I have an IntentService which makes some web service calls. Before making these calls
Have just started using Visual Studio Professional's built-in unit testing features, which as I
Have you managed to get Aptana Studio debugging to work? I tried following this,
I am trying to implement a RESTful API in which I have to upload
I have an IntentService that downloads some files. The problem is that I create
Good day, I have an activity which i navigate to from an icon on
If I have the following code: Intent intent = new Intent(this,DownloadService.class); for(int i=0;i<filesArray.length;i++){ startService(intent);

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.