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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:26:22+00:00 2026-06-14T10:26:22+00:00

I’m trying to port a PC Java program to the Android platform. The PC

  • 0

I’m trying to port a PC Java program to the Android platform. The PC application uses a Swing.Timer to trigger an update every second. The associated listener, upon being called, gets new data from a database, then updates/redraws the screen using Graphics2D. I’ve learned how to use Android’s Canvas to draw the same things that I do with the PC application. Now I’m trying to learn how to use the equivalent Timer in Android. Unfortunately things don’t seem as straightforward on the Android platform. There are Timers, Handlers, AlarmManagers, and AsyncTasks. It would seem that AsyncTasks and AlarmManagers are more appropriate for one time (heavy duty?) tasks (right? wrong?) With regard to Timers and Handlers, I’ve seen many posts that say don’t use Timer, use Handlers instead. I found the approach used in the code below somewhere out there on the web and tried it. It seems like it should do what I want but it hangs the GUI whenever I click the stop button. Does anyone know why it does that?

Thanks times a million
Bill

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    dateFormat = new SimpleDateFormat(dateFormatString);
    mHandler = new Handler();
    mUpdateTimeTask = new MyRunnable();
    Button button = (Button) findViewById(R.id.start_button);
    button.setOnClickListener(new MyStartListener());
    button = (Button) findViewById(R.id.stop_button);
    button.setOnClickListener(new MyStopListener());
}

class MyStartListener implements View.OnClickListener {
    public void onClick(View v) {
        if (startUptimeMillis == 0L) {
            startUptimeMillis = SystemClock.uptimeMillis();
            mHandler.removeCallbacks(mUpdateTimeTask);
            mHandler.postDelayed(mUpdateTimeTask, 100);
        }
    }
};

class MyStopListener implements View.OnClickListener {
    public void onClick(View v) {
        mHandler.removeCallbacks(mUpdateTimeTask);
   }
};

class MyRunnable implements Runnable {
    public void run() {
        final long start = startUptimeMillis;
        long millis = SystemClock.uptimeMillis() - start;
        int seconds = (int) (millis / 1000);
        int minutes = seconds / 60;
        seconds = seconds % 60;
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        TextView tv = (TextView) findViewById(R.id.time_textView);
        tv.setText(dateFormat.format(calendar.getTime()));
        mHandler.postAtTime(this, (((minutes * 60) + seconds + 1) * 1000));
    }
 };

EDIT:
The problem is that postAtTime needs an absolute time at which to start, not a delay which is what my example is using. (See postAtTime here)
So I replaced all of the timing code above with the below and it does what I want!!:

long millis = SystemClock.uptimeMillis();
mHandler.postAtTime(this, millis+1000);
  • 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-14T10:26:23+00:00Added an answer on June 14, 2026 at 10:26 am

    I don’t see how this could hang your app, unless you mean the start button doesn’t work any more… Perhaps you want to add this to your stop listener:

    public void onClick(View v) {
        startUptimeMillis = 0l; // Reset startUptimeMillis
        mHandler.removeCallbacks(mUpdateTimeTask);
    }
    

    As far as Timers, AsyncsTask, etc… You are correct, the best way to program an event in the near future in Android is with a Handler and Runnable. AlarmManagers are not intended for fast callbacks like in animations and AsyncTasks are better for heavy duty computation.

    I would like a to offer a simpler update Runnable:

    class MyRunnable implements Runnable {
        public void run() {
            // You should make this a class variable and initialize it in onCreate(), 
            //   there is no need to search for the same View every second.
            TextView tv = (TextView) findViewById(R.id.time_textView);
    
            final long now = System.currentTimeMillis();
            tv.setText(dateFormat.format(now));
            mHandler.postAtTime(this, 1000 - (now - start) % 1000); // Accounts for millisecond offsets over time
            // mHandler.postDelayed(this, 1000); // Effected by minute offsets
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to select an H1 element which is the second-child in its group
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have been unable to fix a problem with Java Unicode and encoding. The

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.