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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:22:02+00:00 2026-05-18T01:22:02+00:00

I have a service class in my Android app which uses a timer. The

  • 0

I have a service class in my Android app which uses a timer. The timer is set to run at a delay of 1min (60000ms). I want the user to be able to dynamically change this delay. Normally, in java I could just use timer.setDelay(); but Android does not have this function.

Service class so far:

public class LocalService extends Service
{
    Timer timer = new Timer(); 
    int newDelay; 

    public IBinder onBind(Intent arg0) 
    {
        return null;
    }

    public void onCreate() 
    {
        super.onCreate(); 
        startService();
    }

    private void startService()
    {     
        timer.scheduleAtFixedRate(new TimerTask()
        {
            public void run() 
            {       
                mainTask();
            }
        }, 0, 60000);
    }

    private void mainTask()
    {
        //do something
    }

    private void shutdownService()
    {
        if (timer != null) timer.cancel();
    }

    public void onDestroy() 
    {     
        super.onDestroy();
        shutdownService();
    }

    private void readNewDelay()
    {
        InputStream in = openFileInput("updateDelay");
        if(in != null)
        {
            InputStreamReader input = new InputStreamReader(in);
            BufferedReader buffreader = new BufferedReader(input);
            newDelay = Integer.parseInt(buffreader.readLine()) * 60000; //value in file is saved in minutes
        }       
        in.close(); 
    }
}

The delay I want is saved to a text file by an activity. I then want the service to read this file and update its timer delay accordingly.

  • 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-18T01:22:03+00:00Added an answer on May 18, 2026 at 1:22 am

    You’ll have to destroy the timer object and create a new one.

    Don’t forget that timers don’t run when the phone sleeps. A better way is to use an Alert instead of a timer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

please tell me that as we have a service in android which can run
I have a WCF Web Service which is referenced from a class library. After
I'm starting an Android app which will have multiple Activities. It will be using
I have the following very simple class which looks for the last known user
i want to check how the service feature set in android works. to check
I have a web service class that the rest of the framework depends on
Can a class have a Service Contract attribute and its methods Operation Contract. Or
I have a Windows Service project, A, with a dependency on a class library
I have a class that I wish to expose as a remote service using
I have some ASP.NET web services which all share a common helper class they

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.