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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:29:22+00:00 2026-05-17T18:29:22+00:00

I was advised to use this code to make my method execute after a

  • 0

I was advised to use this code to make my method execute after a definite period of time, i modified it a bit and now I have:

private Handler mHandler = new Handler();
private Runnable mUpdateTimeTask = new Runnable() {
    public void run() {
        final long start = mStartTime;

        // Get the difference in ms
        long millis = SystemClock.uptimeMillis() - start;

        // Format to hours/minutes/seconds
        int mTimeInSec = (int) (millis / 1000);

        // Do your thing

        Location location = tracker.getLastKnownLocation(best);
        RetrieveAvgPower(location);

        // Update at the next second
        mHandler.postAtTime(this, 1000);//start + ((mTimeInSec + 10) * 1000));
    }
};

And I try to start and stop it with:

public void onClick(View v) {
         switch (v.getId()) {
            case R.id.start:
                    mStartTime = System.currentTimeMillis();
                    mHandler.removeCallbacks(mUpdateTimeTask);
                    mHandler.postDelayed(mUpdateTimeTask, 1000);
                    break;
                case R.id.stop:
                    mHandler.removeCallbacks(mUpdateTimeTask);
                    break;}}

However, there is an issue. First of all, my method sets text and writes a line to log file, but if I use the code above text is not being set, however all info is being written to log file automatically. Another thing is i can’t stop runnable – after it starts executing program seems to be not responding and crashes if I try to press stop button. What am I doing wrong and how it can be solved?

  • 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-17T18:29:22+00:00Added an answer on May 17, 2026 at 6:29 pm

    Shouldn’t the last line of run() call Handler.postDelayed() rather than Handler.postAtTime()?

    Depending on how event queues are implemented in Android you might be killing the thread by using the wrong one… you’re basically repeatedly setting a Runnable to run at 1 second after the thread originally started, so no other events get to run.

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

Sidebar

Related Questions

After asking this question I was advised not to check in binaries into subversion.
It is advised to use override instead of new key word in C#. Why
As I've read somewhere it is advised to use !== and === instead.
People always advised me that if I am doing some application that should use
In this answer to a recent question , I was advised to be wary
Can somebody advise me what this code does and how can I convert it
I have the following implementation of a clone_ptr in an attempt to make safe
I have code that gives a list of all possible values for any given
I have two projects; A and B, where B needs to use some classes
To trim the leading spaces we are using strmove. But we were advised to

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.