Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to loop a service?

My service needs to check for something every minute and

while(true)
{
   Thread.sleep(60000) 
   //REST OF CODE HERE//
}

is not working. Making the application freeze and asking me to forcefully stop it.

I am sure the problem is with the while loop but I thought it was the only way to infinitely repeat the service whenever the onStart() method executes.

Any suggestions are appreciated.

EDIT

I fixed it and in case you were wondering how the code looks like well there you go:

@Override
public void onStart(Intent intent, int startId) {
    // TODO Auto-generated method stub
    super.onStart(intent, startId);
    Toast.makeText(this, "Service running", Toast.LENGTH_SHORT).show();

    handler = new Handler(){

        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
            Toast.makeText(protectionService.this, "5 secs has passed", Toast.LENGTH_SHORT).show();
        }

    };



    new Thread(new Runnable(){
        public void run() {
        // TODO Auto-generated method stub
        while(true)
        {
           try {
            Thread.sleep(5000);
            handler.sendEmptyMessage(0);

        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 

        }

                        }
    }).start();
}

Basically the service is been called and the tasks of that service will be repeated every 5 secs in that case.

I would like to thank vineetska, Janusz and inazaruk for suggesting to use Handlers. I would like to thank everyone who answered as well, your help was very much appreciated.

like image 367
Batzi Avatar asked Aug 19 '11 06:08

Batzi


People also ask

How do you run a service in a loop?

If you want to repeat some code every minute you can use a handler. A handler has a method called postDelayed this allows you to specify a time after which a runnable will be run. Just call the method again at the end of your runnable to rerun the code after a minute.

What is a service loop?

service loop (plural service loops) An extra length of wire or cable included in an electrical or electromechanical assembly for neatness, accessibility, freedom of movement or future serviceability.

Where should service loops be?

Standard cabling practice calls for a circular service loop of horizontal cable to be placed in the ceiling or overhead above the distribution frame in the telecommunications closet. This loop is meant to provide slack cable if the distribution frame is moved or the cable is reterminated at the patch panel.

How Big Should service loop be?

The recommended lengths are: a minimum of 3 meters in the telecommunications closet for both twisted-pair and fiber cable, and 1 meter for fiber and 30 centimeters for twisted-pair cable at the outlet. Note: Generally, the length of service loop that is required is stated in the construction specifications.


1 Answers

create a thread in your service and put while loop there like this:

 new Thread(new Runnable(){
    public void run() {
    // TODO Auto-generated method stub
    while(true)
    {
       Thread.sleep(60000) 
       //REST OF CODE HERE//
    }

                    }
}).start();
like image 134
Vineet Shukla Avatar answered Oct 23 '22 02:10

Vineet Shukla