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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:33:12+00:00 2026-05-23T07:33:12+00:00

Implementing a Thread by providing a new class that extends Thread and overriding its

  • 0

Implementing a Thread by providing a new class that extends Thread and overriding its run() method is new to me. I’ve tried all day to get it to work. Here’s my code:

/*
 * see http://developer.android.com/reference/java/lang/Thread.html
 */
class threadClass extends Thread {
    private Handler mHandler;
    private Message mMsg;
    // constructor
    public threadClass(Handler handler, Message msg) {
        // do something like save the Handler reference
        mHandler = handler;
        mMsg = msg;
    }
    @Override
    public void run() {
        // do some background processing, call the Handler?
        mHandler.sendMessage(mMsg);
    }
}
public Thread passHandlerToThread(Handler handler) {
    handler.sendEmptyMessage(10);
    Message msg = Message.obtain();
    msg.what = 10;
    Thread thread = new threadClass(handler, msg);
    return thread;
}
private Handler localHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        int what = msg.what;
        if (what == 10) Log.i("localHandler", "what: " + what);
    }
};
public void startThread() {
    Thread thread = passHandlerToThread(localHandler);
    thread.start();
}

I call startThread() in my LocalService onCreate() but nothing happens. What am I doing wrong? I was expecting localHandler() to be called twice: once in passHandlerToThread() and again in run().

  • 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-23T07:33:13+00:00Added an answer on May 23, 2026 at 7:33 am

    Do something like this:

    private final Handler handler = new Handler();
    // toast runnables
    final Runnable updateTwitterNotification = new Runnable() {
        @Override
        public void run() {
            dismissProgressSpinner();
            Toast.makeText(getBaseContext(), "Tweet sent!", Toast.LENGTH_LONG).show();
        }
    };  
    
    final Runnable updateCreateError = new Runnable() {
        @Override
        public void run() {
            Toast.makeText(getBaseContext(), "Tweet error!", Toast.LENGTH_LONG).show();
        }
    };
    
    postMessageInThread();
    
    
    //implementation:
    private void postMessageInThread() {
        Thread t = new Thread() {
            @Override
            public void run() {
    
                try {
                    connectToTwitterService() // or whatever
                    handler.post(updateTwitterNotification);
                } catch (Exception ex) {
                    Log.e(TAG, "Error sending msg", ex);
                    handler.post(updateCreateError);
                }
            }
        };
        t.start();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why is implementing Runnable a better option than extending from Thread class?
I'm implementing a class that talks to a motor controller over a USB device.
How would I go about implementing a thread that runs throughout the application. I
I am implementing this class as a singleton. I am not good at thread
I can't get a spawned thread to stop: I'm implementing the vibration-part of the
C is a mystery all the time! I am implementing a work-crew thread execution
I have class implementing Runnable: public class Abc implements Runnable{ public synchronized void run(){
duplicate of: "pure virtual method called" when implementing a boost::thread wrapper interface I am
Actually, what other ways are available apart from extending the Thread class and implementing
I have a Runnable implementing class which will be run from a Executors.newFixedThreadPool Inside

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.