I am trying to create a service which does something every 5 seconds.
The problem is the moment I put the while(true) it gives me an error.
I imagine this is just not allowed. If so, what is a better way to implement a repetitive method?
public class repService extends Service {
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
while(true){
Thread timer = new Thread(){
public void run(){
try {
sleep(5000);
// do something
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// do something
}
}
};
timer.start();
}
}
You can use
Timer().scheduleto repeat task every 5 seconds.If you need to modify or play on UI part then make sure to write than code in
runOnUiThread