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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:27:09+00:00 2026-06-15T07:27:09+00:00

I’m working on an Android app that uses uses a background worker thread. I

  • 0

I’m working on an Android app that uses uses a background worker thread. I need to be able to send messages to the thread from the activity, but I can’t quite figure it out.

I have one activity, and one thread to do work in the background. I want to start the thread and be able to send messages (arguments, objects, etc.) to it when needed. I’ve mastered sending messages from the thread to the activity (by passing the activity’s handler to the thread, and using that to send messages), but whenever I attempt to send messages from activity to thread, the app crashes.

I’ve tried following a good 10-12 tutorials that I’ve found online, all of which seemed to have a different way of doing things, but I still haven’t gotten this to work correctly. Could someone please point me in the right direction?

An example simple activity:

import android.os.Bundle;
import android.app.Activity;

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savesInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    private void doWork() {
        Worker worker = new Worker();
        worker.start();

        worker.handler.sendEmptyMessage(0);
    }
}

An example simple thread:

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

public class Worker extends Thread {
    public Handler handler;

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

        handler = new Handler() {
            public void handleMessage(Message msg) {
                Log.d("Worker.run()", "Got message saying " + msg.what);
            }
        };

        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-06-15T07:27:10+00:00Added an answer on June 15, 2026 at 7:27 am

    You have to read how to use and that for is Looper first and don’t forget to stop looper at the end Goodluck

    public class Worker extends Thread {
        public Handler handler;
    @Override
    public run() {
        Looper.prepare();
    //initialization will take a little time you if you want to send message check if handler  != null
        handler = new Handler() {
            public void handleMessage(Message msg) {
                Log.d("Worker.run()", "Got message saying " + msg.what);
            }
        };
    
        Looper.loop();
    }
    
    public void sendMessage(Message m)
    { 
        while(handler == null);
        handler.sendMessages(m); 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I am currently running into a problem where an element is coming back from

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.