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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:49:47+00:00 2026-06-04T09:49:47+00:00

I would like the Timer to start when I turn ToggleButton ON and to

  • 0

I would like the Timer to start when I turn ToggleButton ON and to cancel when I turn it OFF. It works, but when I try to start it over again after being cancelled I get an error. Where is the problem? Here is the code:

final ToggleButton btnLive = (ToggleButton)findViewById(R.id.live);
    btnLive.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (btnLive.isChecked()) {

                timer = new Timer();
                timer.schedule(timerTask, 5*1000, 5*1000);

            } else {

                timer.cancel();
                timer.purge();
                timer = null;

            }
        }
    });

EDIT: It works with calling timer that way:

timer.schedule(new TimerTask(){
                    private Runnable runnable = new Runnable() {
                        public void run() {
                            new Something().execute();
                        }
                    };
                    public void run() {
                        handler.post(runnable);
                    }
                }, 5*1000, 5*1000);

Can you explain me why first method is not working? Is it range problem?

  • 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-04T09:49:49+00:00Added an answer on June 4, 2026 at 9:49 am

    Use this type of code and check.

    Timer mTimer = new Timer();
    mTimer.schedule(new TimerTask()
                    {
                        @Override
                        public void run() {
                            runOnUiThread(new Runnable() {
                                public void run() {
    
                                }
                            });
                        }
                    }, 5000, 500);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a timer tick that I would like to kick off my backgroundworker
I have a timer that start a notification when it ends. But I would
I would like to play a sound in Objective-C with a millisecond start time.
I've been developing my first iPhone app part-time and would like to start using
I would like to run a timer for every 5 hours and delete the
I would like to bulid a Timer (with minutes from 1 - 90 with
Hi In my android application i am using videoview. I would like to start
I would like to use a timer instead of sleep within a windows service
I have this dilemma many times - and I would like one time and
I would like to parse free-text time intervals like the following, using Python: 1

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.