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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:26:22+00:00 2026-06-12T05:26:22+00:00

In my Android application I have an Observable Data class, which contains a method

  • 0

In my Android application I have an Observable Data class, which contains a method download() to download and parse some data. When this is done, it notifies its Observers.

Further, I have an Activity and a Service. The Activity calls the download() method asynchronous using an AsyncTask.

The Service constantly needs to listen for updates to process the data further. This should not happen in the UI thread. I’ve created this little Service class:

public class MyService extends Service {

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public void onCreate() {
        new MyWorkerThread().start();
    }

    private class MyWorkerThread extends Thread implements Observer{

        @Override
        public void run() {
            observeData();
        }

        private void observeData(){
            Data.getInstance().addObserver(this);
        }

            @Override
            public void update(Observable observable, Object data) {
               // In what thread does the code placed here run?
            }
    }
}

Now, my question: When the download() method of the Data class notifies its Observers, in which thread will the update() method run? Will this be in the thread of the AsyncTask created by the Activity, which prevents the code from going through the onPostExecute() method until the service is done? If so, would I be better off making my MyService an Observer and create a new MyWorkerThread each time?
Or will the code run in it’s own thread by the MyWorkerThread?

I hope it’s understandable, thanks in advance!

  • 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-12T05:26:23+00:00Added an answer on June 12, 2026 at 5:26 am

    I guess I’d better create a new Thread everytime the notifyObservers method is called

    You can do that but you don’t have to. For example using a single HandlerThread would accomplish the same. Also you don’t have to deal with multiple threads running in parallel then.

    public class ObserverService extends Service {
    
        private Looper mLooper;
    
        private void onDataChanged() {
            // do whatever you need.
            // This is executed in a background thread.
        }
    
        @Override
        public void onCreate() {
            HandlerThread ht = new HandlerThread("ObserverService");
            ht.start();
            mLooper = ht.getLooper();
            final Handler handler = new Handler(mLooper) {
                @Override
                public void handleMessage(Message msg) {
                    // executed in background HandlerThread
                    onDataChanged();
                }
            };
            Observer observer = new Observer() {
                @Override
                public void update(Observable observable, Object data) {
                    // this is executed in whatever thread notifies
                    // enqueue max 1 messages.
                    handler.removeMessages(1234);
                    handler.sendEmptyMessage(1234);
                }
            };
            // register observer that send message to background thread.
            Data.getInstance().addObserver(observer);
        }
    
        @Override
        public void onDestroy() {
            mLooper.quit();
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            return Service.START_STICKY;
        }
    
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my android application i have created the database which contains some data and
I am building an Android application and I have JSON data which contains Unicode
i'm building an android application which have a chat. in this chat i each
In my Android application I have a main activity MyActivity which overrides onConfigurationChanged() method.
In my Android Application i have to download video from Amazon bucket. When I
I would like to develop an Android application which will have a local database.
In my android application I have a dialog box in which the user inputs
I am making an android application in which i have five items in a
in my android application i have created Background thread to load data from the
In my Android application I have a new intent which is launched depending on

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.