Skip to content

Instantly share code, notes, and snippets.

@jbruchanov
Last active February 1, 2016 13:53
Show Gist options
  • Save jbruchanov/7f5704e3f2b690b1200e to your computer and use it in GitHub Desktop.
Save jbruchanov/7f5704e3f2b690b1200e to your computer and use it in GitHub Desktop.
public class MyActivity extends Activity implements PBarChange {
private ProgressBar mPbar;
private MyThread mMyThread;
public void onCreate(...){
super.onCreate(...);
setContentView(R.id.content);
mPbar = (ProgressBar)findViewById(R.id.pbar);
mMyThread = new MyThread(this);
mMyThread.start();
}
public void onDestroy(){
mMyThread.stop();
}
public void onProgressChanged(int progress){
mPbar.setProgress(progress);
}
}
public class MyThread extends Thread {
public interface PBarChange {
void onProgressChanged(int progress);
}
private Handler mHandler;
private int mProgress;
private boolean mIsRunning = true;
public class MyThread(PBarChange listener){
mListener = listener;
mHandler = new Handler(Looper.getMainLooper());
}
public void run() {
while(mIsRunning) {
mProgress = 10...;
mHandler.post(mUpdateAction);
}
}
public void stop(){
mIsRunning = false;
}
private Runnable mUpdateAction = new Runnable(){
public void run(){
mListener.onProgressChanged(mProgress);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment