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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:51:51+00:00 2026-06-02T07:51:51+00:00

I want to update a variable in my activity once every one second and

  • 0

I want to update a variable in my activity once every one second and for that i am using an Async task and a runnable inside it. I keep getting this error saying “Cannot refer to a non-final variable myRunnable inside an inner class defined in a different method” . When i change the declaration to final , it shows “The local variable myRunnable may not have been initialized” . How do i rectify the error? Is there a better way of doing what i want to do?

    import android.os.AsyncTask;
    import android.os.Handler;

    public class Timer extends AsyncTask<String, Process, Void>{
static int seconds=0;
TimerTask timer;
static int minutes=0;
static int hours=0;
private String timeTaken;
public Handler myhandler = new Handler();

@Override
protected Void doInBackground(String... params) {

   final Runnable myRunnable = new Runnable() {

@Override
public void run() {
    ......
......
    ...... 
myhandler.postDelayed(myRunnable, 1000);
}
};

    return null;
}   
  • 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-02T07:51:52+00:00Added an answer on June 2, 2026 at 7:51 am

    How about something like this instead of using an AsyncTask (although something similar can be done within an AsyncTask, and because it runs in another thread I could simply block this worker thread using Thread.sleep(…) instead of doing the whole Runnable thing…

    private myhandler = new Handler();
    private boolean periodicCallsEnabled=false;
    
    public boolean isPeriodicUpdatingEnabled(){
        return periodicCallsEnabled;
    }
    
    public void startPeriodicUpdates(){ //CALL THIS IN ORDER TO START THE PERIODIC UPDATES
        periodicCallsEnabled=true;
        periodicCall();
    }
    
    public void stopPeriodicUpdates(){ //CALL THIS TO STOP THEM
        periodicCallsEnabled=false;
        myhandler.removeCallbacksAndMessages(null);
    }
    
    
    void periodicCall(){
        if(periodicCallsEnabled){
    
            //INSERT CUSTOM CODE HERE TO BE EXECUTED EVERY SECOND
            // ......
    
            myhandler.postDelayed(new Runnable(){
                public void run(){
                    periodicCall();
                }
            },1000);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to update an attribute defined by a variable in an activerecord model.
I want to update a .class file in a jar with a new one.
I want to update a table to indicate that some rows are parents of
I have a variable say, column_name. I want to update a column whose name
I want to update and select one row within one query in SqLite. In
I want to update a single record in a table to reflect that a
I have declared a variable in my .xsl file. Now I want to update
I want to update a Django session variable following a Javascript event (well, actually
i want to set one variable for my entire application and i want to
I read file from user using FileReference. I have a variable which I want

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.