Open MyActivity.java and replace the onCreate method:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ProgressDialog dialog = new ProgressDialog(MyActivity.this);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setMessage("Downloading 200gb...");
dialog.setCancelable(true);
dialog.setMax(200);
dialog.setProgress(0);
dialog.show();
Thread t = new Thread(new Runnable(){
public void run() {
while(dialog.getProgress() < dialog.getMax())
{
dialog.incrementProgressBy(1);
try{Thread.sleep(50);}catch(Exception e){/* no-op */}
}
dialog.dismiss();
}
});
t.start();
}
Redeploy (mvn clean install) and launch.
Question: if i cancel the dialog the thread still running on the background..... nice sample
ReplyDeleteThat's a good point. You could use setOnCancelListener to set a boolean die flag... add that to the while condition...?
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDelete