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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:39:41+00:00 2026-06-06T23:39:41+00:00

Having read lots of snippets and tutorials, I’m still (or even more) confused about

  • 0

Having read lots of snippets and tutorials, I’m still (or even more) confused about the road to take. I need a thread/backgroundtask, that listens for incoming events on a socket and report any incomings to the UIThread. What would be the preferred way to choose? Own thread or multitasking? Best way to transfer data to the main thread?

Thanx for any thoughts on the matter.

Regards,
Marcus

Considering your answers below, I’ve tried the following:

MainActivity:

public class MainActivity extends Activity {


Handler handler = new Handler() {

    @Override
       public void handleMessage(Message msg) {
            toastSomething();
      };
};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    threadstarter();
}


protected void threadstarter() {

    super.onStart();

    Thread backgroundthread = new Thread(new WorkerThread(handler));
    backgroundthread.start();
}

public void toastSomething() {
    Toast.makeText(this, "hello", Toast.LENGTH_SHORT).show();
} }

An my runnable:

public class WorkerThread implements Runnable {

Handler messageHandler;

WorkerThread(Handler incomingHandler) {
    messageHandler = incomingHandler;
}

public void run() {

    while (true) {

        for (int i = 0; i <= 100000; i++) {
            // wait a moment
        }
        messageHandler.sendEmptyMessage(1);
    }
} }

My layout only holds an additional checkbox:

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="CheckBox" />

Good thing is, the toast appears. Bad thing, the cehckbox is unresponsive and the app crashes pretty quick. Isn’t that, how it should be done?

Edit: the msg in sendMessage in the WorkerThread seems to be the troublemaker as the exception says, the message is all read in use?

  • 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-06T23:39:42+00:00Added an answer on June 6, 2026 at 11:39 pm

    Here’s the code, that’s finally working:

    UIThread:

    public class MainActivity extends Activity {
    
        public static final String LOG_TAG = "UIThread";
    
        Handler handler = new Handler() {
    
            @Override
               public void handleMessage(Message msg) {
                    toastSomething();
                    //Log.v(LOG_TAG, "main thread");
              };
        };
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            threadstarter();
        }
    
        protected void threadstarter() {
    
            super.onStart();
    
            Thread backgroundthread = new Thread(new WorkerThread(handler));
            backgroundthread.start();
        }
    
        public void toastSomething() {
            Toast.makeText(this, "hello", Toast.LENGTH_SHORT).show();
        }
    }
    

    WorkerThread:

    public class MainActivity extends Activity {
    
        public static final String LOG_TAG = "UIThread";
    
        Handler handler = new Handler() {
    
            @Override
               public void handleMessage(Message msg) {
                    toastSomething();
                    //Log.v(LOG_TAG, "main thread");
              };
        };
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            threadstarter();
        }
    
        protected void threadstarter() {
    
            super.onStart();
    
            Thread backgroundthread = new Thread(new WorkerThread(handler));
            backgroundthread.start();
        }
    
        public void toastSomething() {
            Toast.makeText(this, "hello", Toast.LENGTH_SHORT).show();
        }
    }
    

    HTH someone. Thanx to all for the input.

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

Sidebar

Related Questions

I've read lots about self referential classes in Rails, but am still having problems
I've read lots about the Microsoft's threaded apartment model, but I'm still having a
I'm still entangled in error handling problems. After having read and tried lots of
Preface: I've read lots of articles about images inside a div having a strange
I've read tutorials etc on the web about localization in Asp.Net and MVC, but
Having read the documentation for VPython and GTK threading , it seems to me
Having read other people's questions I thought window.onload=... would answer my question. I have
Having read Joe Armstrong's book and watched Kevin Smith screencasts I've built a simple
Having read this link on RBAR and this , my understanding of RBAR amounts
Having read the documentation of Java's String class, it doesn't appear to support popping

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.