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 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

I have some code that works like the advised use of TransactionScope, but has
As a newbie, I have been advised to preferably use heredoc compared to too
I have been having a hard time understanding how to use a singleton to
Why this code show me a window without image? I try to use QByteArray
I am using clojure and hiccup (with noir) and I have this code: (defn
I have been advised to use the following options with GCC, as it helps
I have started to learn ASP.NET MVC, and at this time of studying I
I have a recursive function which contains some drawing code inside. I was advised
I have a little bit of XML that looks like this: <LayerDefinition xsi:type =GridLayerDefinition>
Pep8 advises to always use cls as the first argument of a class method

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.