Issue
I'm trying to write from a BufferedInputStream, I used while
statement before and it worked well, but now I want to delay the loop of writing each 250 millisecond
. So, I thought about using a Handler
with postDelayed
.
This was my code using while
:
while (count = input.read(data)) > 0){
//File writing...
}
But when I turned it to this:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
try {
//File writing...
} catch (IOException e) {
}
}
}, 250);
I got this RuntimeException:
Can't create handler inside thread that has not called Looper.prepare().
I think, it says that I cant create a handler inside a thread (AsyncTask/Thread), but I hope someone has a workaround for this. Thanks in advance!
Solution
Try calling Thread.sleep() instead of calling postDelayed on a hanler, because inside the doInBackround method you are already in backround thread and there is no issues in calling sleep for a small time interval
try {
Thread.sleep(250)
//File writing...
}catch (IOException e) {
}catch (InterruptedException e) {
}
`
Answered By - Ramees Thattarath
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.