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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:00:14+00:00 2026-06-01T03:00:14+00:00

In my activity oncreate method, i have called a service using OnStartCommand(). My requirement

  • 0

In my activity oncreate method, i have called a service using OnStartCommand(). My requirement is when the user is on the same Activity (when the Activity is visible), a set of code should run repeatedly. (Example .. I should make a web service call and get the response and do some action based on it after regular intervals).

I have put this set of code in this method.

@Override
     public int onStartCommand(Intent i, int flags , int startId){

        // Code to be repeated

return Service.START_STICKY;
}

But, this is getting executed only once. How to make it run repeatedly from the time the user came to this page till he leaves this page ??

  • 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-01T03:00:15+00:00Added an answer on June 1, 2026 at 3:00 am

    CountDownTimer.cancel() method seems to be not working.

    I would recommend you to use Timer instead. It’s much more flexible and can be cancelled at any time. It may be something like that:

    public class MainActivity extends Activity {    
        TextView mTextField;
        long elapsed;
        final static long INTERVAL=1000;
        final static long TIMEOUT=5000;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            mTextField=(TextView)findViewById(R.id.textview1);
    
            TimerTask task=new TimerTask(){
                @Override
                public void run() {
                    elapsed+=INTERVAL;
                    if(elapsed>=TIMEOUT){
                        this.cancel();
                        displayText("finished");
                        return;
                    }
                    //if(some other conditions)
                    //   this.cancel();
                    displayText("seconds elapsed: " + elapsed / 1000);
                }
            };
            Timer timer = new Timer();
           timer.scheduleAtFixedRate(task, INTERVAL, INTERVAL);
       }
    
        private void displayText(final String text){
            this.runOnUiThread(new Runnable(){
                @Override
                public void run() {
                    mTextField.setText(text);
                }});
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have noticed that when using actionBar.setSelectedNavigationItem(x) in the onCreate() method of my Activity,
I have Activity with ListView inside it and in the onCreate method of the
This is my activity oncreate() method. I set a positive button ok with a
I have an ExpandableListActivity (using a SimpleCursorTreeAdapter) which starts another activity when the user
I have a LinearLayout and its child - ImageView. My activity's onCreate does nothing
I have a problem with my activity. This is my onCreate: case NORMAL: game
I have a series of 3 activities, and the 3rd activity binds (I'm using
I'm developing an activity that binds to a local service (in onCreate of the
In the onCreate() method of my Activity, I grab the outermost LinearLayout of the
I have service with thread, From activity I am calling start service(from Activity::onDestroy()) and

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.