• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps A loop in a Service/AsyncTask?

Shelly22

Newbie
Hello nice Guys and Forum.

I need a endless loop/while/for.
How to do this in a AsyncTask or Service?

[HIGH]public class Back extends AsyncTask<Void, Void, Void>{

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

handler.postDelayed(new B1(), 2000);
handler.postDelayed(new B2(), 3000);

return null;
}
}[/HIGH]

I want to make my handler endless loop/while/for.

And sorry for my English.

I hope you can help me =)
 
same way you would do an infinite loop anywhere else (highly discouraged, btw!).

If you really insist on doing an infinte loop, however, you just do:

[high]
while (true) {
//Do stuff here
}
[/high]

EDIT:
Instead of using an infinite loop, you should consider having a boolean value that keeps tract of the state, and check the value of that variable in the loop instead. Then, whenever the app should exit, you simply invert the value of the boolean variable and the thread will shut down gracefully.
 
What do you mean it "doesn't work?" Can you be more specific? Is it crashing? Is the animation not updating? What?
 
It's crashed. Its not work.

[HIGH]
while(true){
handler.postDelayed(new B1(), 0000);
handler.postDelayed(new B2(), 2000);
}
[/HIGH]

Understand? :)
 
All you are doing in your background thread is posting to the UI thread? I fail to see the benefit of actually using a thread at all in this case.

Anyway, can you post your logcat output?
 
Back
Top Bottom