M
M
Morn2014-12-25 19:06:18
Java
Morn, 2014-12-25 19:06:18

How to create an infinite loop in android?

What is the best way to organize an infinite background loop in Android?
I tried this design with AsyncTask, but it has a serious drawback: no other background processes can run while the loop is running.

class loop extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... params) {
        while(true) {
            //Некоторые действия
        }
    }
}

PS This will be a program that controls the device (timelapse slider) based on Arduino.
I used to do this in Processing with its standard draw() loop. In the loop, the program:
1) Responded to a connection break.
2) Responded to messages received from Arduino.
3) Updated the countdown timer and the progress bar (ProgressBar).

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Armenian Radio, 2014-12-25
@gbg

Tell the whole problem. Perhaps there is a serious architectural flaw in your application, fixing which, you will get rid of the need to use this kind of loop.

A
anyd3v, 2014-12-25
@anyd3v

You can’t do it as you demonstrated, because an error will come out (I don’t remember the exact text, but something like: the background task has been working for too long). I came across this when I got a legacy project and the next developer worked with the comet server like that.
to the account "no other background processes can be executed while the cycle is running." you most likely do not run the task correctly: depending on the version, you need to run it either simply through excecute or through AsynkTask.runOnExcecutor
As a variant of the solution I described above and your approach, you can do this: data or the next request to the server

O
Oleg, 2014-12-25
@Master255

I use the following construction (I don't know how correct it is):
new Transfer(t + "/", false).start();
synchronized (th)
{
try
{
th.wait();//th is a private static Thread th; th = Thread.currentThread();
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
This is to start a new branch and wait in the main one.
Then return already in a new branch like this:
synchronized (th)
{
th.notify();
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question