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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:49:27+00:00 2026-06-14T18:49:27+00:00

Hi I am trying to use a thread with a handler and a looper

  • 0

Hi I am trying to use a thread with a handler and a looper in Android.

Thread class:

public void run() {
    Looper.prepare();

    handler = new AndroidHandler(context);
    Looper.loop();

    while (!stopMe) {
                       someMethod();
               }
           ((Handler) handler).getLooper().quit();
 }

public void someMethod(){
  Log.i("New System", "Handling ");
    order ++;
    Message m=handler.obtainMessage();
    m.arg1=order;
    handler.sendMessage(m);
 }

in a separate class:

public class AndroidHandler extends Handler{
   public AndroidHandle(Context){
        super();
  }
   public void dispatchMessage(Message m) {
    super.dispatchMessage(m);

}
   @Override
public void handleMessage(Message msg) {

    Log.i("New System", "handling Message "+msg.arg1);
   }
 }

It doens’t work!!! messages aren’t being sent and nothing is getting printed in the console and I don’t know how to fix it…. What is the problem here any ideas? thanks

ps: I don’t want to use the ui thread I want to do this in a separate thread.

  • 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-14T18:49:28+00:00Added an answer on June 14, 2026 at 6:49 pm

    That’s because you are doing your infinite while loop in the same thread as the looper! So this thread is kept busy and cannot receive messages…

    You need to let the Looper’s thread on its own.

    Let’s say you setup your looper thread like this:

    class LooperThread extends Thread {
      public Handler handler;
    
      public void run() {
          Looper.prepare();
    
          mHandler = new Handler() {
              public void handleMessage(Message msg) {
                  Log.i("New System", "handling Message "+msg.arg1);
              }
          };
    
          Looper.loop();
      }
    }
    
    LooperThread looper = new LooperThread();
    looper.start();
    

    Then you can send messages from any OTHER thread (the UI thread or any other one) just the same way as you did:

    Handler handler = looper.handler;
    Message m = handler.obtainMessage();
    m.arg1 = order;
    handler.sendMessage(m);
    

    But don’t do this from the same thread as the looper or it doesn’t make any sense.

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

Sidebar

Related Questions

I'm trying to use thread pool in Java. But the number of threads is
I'm trying to use the UI-Thread, so I've written a simple test activity. But
I'm trying to use ThreadLocal to provide thread safety to pre-existing non-thread-safe classes, but
I'm trying to use set! to modify the thread-local bindings of a var --
Well I'm trying to set a variable to use in a thread, it works
I was trying use a set of filter functions to run the appropriate routine,
I'm trying use self-signed certificate (c#): X509Certificate2 cert = new X509Certificate2( Server.MapPath(~/App_Data/myhost.pfx), pass); on
I'm trying to use ASyncTask to handle networking in the background of my android
I'm trying to learn to use the Bing Maps API for Android. I downloaded
I am trying to use an AsyncTask class that I created to update a

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.