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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:40:44+00:00 2026-05-23T08:40:44+00:00

How can i send a message between an activity and a thread if put

  • 0

How can i send a message between an activity and a thread if put the thread in a class?

Okay, here is a little more detailed code:

public class AnActivity extends Activity implements OnClickListener, Callback
{

    Button          b           = null;
    TextView        t           = null;
    MyThread        mThread     = null;
    public Handler  mainHandler = new Handler( this );

    @Override
    public void onCreate( Bundle savedInstanceState )
    {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.main );

        b = ( Button ) findViewById( R.id.buttonSEND );

        b.setOnClickListener( this );
        mThread = new MyThread( this );
        mThread.start();
    }

    @Override
    public void onClick( View v )
    {
        Message m = Message.obtain();
        m.what = 4;
        mThread.myThreadHandler.sendMessage( m );
    }

    @Override
    public boolean handleMessage( Message msg )
    {
        Toast.makeText( this, "What= " + Integer.toString( msg.what ), Toast.LENGTH_SHORT ).show();
        return false;
    }
}

The following class is not a part of AnActivity

public class MyThread extends Thread
{
    public Handler myThreadHandler;
    Activity mainActivity;

    public MyThread ( AnActivity anActivity )
    {
        this.mainActivity = anActivity;     
    }


    @Override
    public void run()
    {
        Looper.prepare();
        myThreadHandler = new Handler()
        {
            // sending back a message immediately after a received one
            public void handleMessage( Message msg )
            {
                Message m = Message.obtain();
                m.what = 10;
                // MY PROBLEM:
                // mainActivity.mainHandler.sendMessage( m ); // mainHandler is not visible
                // mainActivity.sendMessage( m ); // does not working
            }
        };
        Looper.loop();
    }
}
  • 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-23T08:40:44+00:00Added an answer on May 23, 2026 at 8:40 am

    This is my Activity:

    public class AnActivity extends Activity implements OnClickListener, Callback
    {
    
        Button          b           = null;
        TextView        t           = null;
        MyThread        mThread     = null;
        public Handler  mainHandler = new Handler( this );
    
    
        @Override
        public void onCreate( Bundle savedInstanceState )
        {
            super.onCreate( savedInstanceState );
            setContentView( R.layout.main );
    
            b = ( Button ) findViewById( R.id.buttonSEND );
    
            b.setOnClickListener( this );
            mThread = new MyThread( this );
            mThread.start();
        }
    
    
        @Override
        public void onClick( View v )
        {
            Message m = Message.obtain();
            m.what = 4;
            mThread.myThreadHandler.sendMessage( m );
        }
    
    
        @Override
        public boolean handleMessage( Message msg )
        {
            Toast.makeText( this, "This is AnActivity. What=" + Integer.toString( msg.what ), Toast.LENGTH_SHORT ).show();
            return false;
        }
    }
    

    Modified thread:
    In the constructor i can store UI thread’s messagehandler.

    public class MyThread extends Thread
    {
        public Handler  myThreadHandler;
        Activity        mainActivity;
        Handler         mainHandler;
        int             what    = -1;
    
    
        public MyThread ( AnActivity anActivity )
        {
            this.mainActivity = anActivity;
            this.mainHandler = anActivity.mainHandler;
        }
    
    
        @Override
        public void run()
        {
            Looper.prepare();
            myThreadHandler = new Handler()
            {
                public void handleMessage( Message msg )
                {
                    what = msg.what;
    
    
                    // Writing back to main UI thread
                    mainActivity.runOnUiThread( new Runnable()
                    {
    
                        @Override
                        public void run()
                        {
                            Toast.makeText( mainActivity, "This is MyThread. What=" + Integer.toString( what ), Toast.LENGTH_SHORT ).show();
    
                        }
                    } );
    
                    Message m = Message.obtain();
                    m.what = 10;
                    mainHandler.obtainMessage( 12 ).sendToTarget();
                }
            };
            Looper.loop();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to send an email message using the .NET MailMessage class which can
Can i create an application which can send the picture message without using intent??
I am trying to send a message to my main activity from an Async
How can I send keyboard input messages to either the currently selected window or
I can send any windows application key strokes with PostMessage api. But I can't
If webserver can send gzip response, why can't browser sent gzip request?
In HTML, you can send data from one page to another using a GET
We've built up an application infrastructure based on ActiveMQ. We can send and receive
How can I send an email from C++? Is there a good cross-platform (MS
How can I send email using Perl through my Gmail account? The one CPAN

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.