android - Starting and stopping progressbar on UI thread from class -
is there slicker/simpler way of doing following? have method in class shows progressbar while thread runs. code works seems little overly clunky having 3 steps.
private void pause() { mactivity.runonuithread(new runnable() { @override public void run() { mprogressbar.setvisibility(view.visible); } }); new thread(new runnable() { @override public void run() { try { //do stuff } catch (interruptedexception e) { e.printstacktrace(); } } }).start(); mactivity.runonuithread(new runnable() { @override public void run() { mprogressbar.setvisibility(view.gone); } }); }
this not show progress bar while thread runs.
your thread can run 10 seconds visibility of progressbar blink if can see @ all.
instead, should hide once thread has completed, correct:
mactivity.runonuithread(new runnable() { @override public void run() { mprogressbar.setvisibility(view.visible); } }); new thread(new runnable() { @override public void run() { try { //do stuff mactivity.runonuithread(new runnable() { @override public void run() { mprogressbar.setvisibility(view.gone); } }); } catch (interruptedexception e) { e.printstacktrace(); } } }).start();
for "slicker" way, use asynctask created task. example:
new asynctask<void, void, void>() { @override protected void onpreexecute() { super.onpreexecute(); // guaranteed run on ui thread mprogressbar.setvisibility(view.visible); } @override protected void doinbackground(void... params) { // stuff return null; } @override protected void onpostexecute(void avoid) { super.onpostexecute(avoid); // guaranteed run on ui thread mprogressbar.setvisibility(view.gone); } }.execute();
Comments
Post a Comment