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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:17:04+00:00 2026-06-10T02:17:04+00:00

I need a timer which will send a message for its’ handler. I’ve made

  • 0

I need a timer which will send a message for its’ handler. I’ve made a class that implements Runnable and I feed its’ object to the thread runnable constructor. When I start the thread it hangs application and it obviously isn’t working asyncroniously. I could’ve used AsyncTask but I’ve also heard that they’re designed for short-term operations while my background timer must work throughout activity onResumed state. Would you mind pointing out my mistake and maybe giving useful links on the subject of threads in android. Thanks.

Here’s the code I’ve written:

@Override
public void onResume() {
    // TODO Auto-generated method stub
    super.onResume();

    _myTimerInstance = new MyTimer(new Handler() {
        @Override
        public void dispatchMessage(Message msg) {
            super.dispatchMessage(msg);
            // ...
        }
    });
    _myThread = new Thread(_myTimerInstance);
    _myThread.run();


}

private static class MyTimer implements Runnable {

    private Handler _myHandler;
    private boolean _activityHasBeenLeft;

    public MyTimer(Handler myHandler) {
        _myHandler = myHandler;
    }

    public void setActivityHasBeenLeft(boolean b) {
        _activityHasBeenLeft = b;
    }

    @Override
    public void run() {
        while (!_activityHasBeenLeft) {
            try {
                Thread.sleep(2000);
                _myHandler.sendEmptyMessage(0);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
  • 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-10T02:17:06+00:00Added an answer on June 10, 2026 at 2:17 am

    You should always use Thread.start() not Thread.run()
    Thread.run() is like a normal method call and is run on the same thread.

    use

    _myThread.start();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to have a DateTimePicker which will be able to select the Time
i need a timer that works like this : imagine somebody have a limit
I'm looking at generating pseudo-random one-time-passwords that are time sensitive. To send a message,
I have an application which uses SslStream to send and receive data with its
I'm building a system that will send verses of scripture to subscribers over e-mail.
I am trying to use a single function in a class that will start
I have created a messaging system for users which will allow users to send
I need to send(synchronise) data between few(3 and more) apps. And Message Broker must
I need to create an array of images in which, every time I tap,
I need to do a simple thing, which I used to do many times

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.