Wednesday, 30 January 2013

Create the Work Method | Android Tutorial pdf

Create the Work Method

The theory of this demo is that we have something that takes a long time, and we want to have that work done in a background thread and update the progress along the way. So, the first step is to build something that will run a long time.
To do that, first, implement a doSomeLongWork() method on LunchList as follows:
private void doSomeLongWork(final int incr) {
SystemClock.sleep(250); // should be something more useful!
}

Here, we sleep for 250 milliseconds, simulating doing some meaningful work.
Then, create a private Runnable in LunchList that will fire off doSomeLongWork() a number of times, as follows:
private Runnable longTask=new Runnable() {
public void run() {
for (int i=0;i<20;i++) {
doSomeLongWork(500);
}
}
};

No comments: