2013年3月15日----ProgressDialog与线程整合应用
在Android里,我们通常是通过Progress Dialog来显示一个“加载中”对话框,这个类封装在Android.app.ProgressDialog里,但需要留意的是Android的Progerss Dialog必须要在后台程序运行完毕前,以dismiss()方法来关闭取得焦点的对话框,否则程序就会陷入无法终止的无穷循环中;又或者在线程里不可有任何更改Context或parent View的任何状态、文字输出等事件,因为线程里的Context与View并不属于parent,两者之间也没有关联。所以在下面的范例中,我们以线程(Thread)来模拟后台的运行,再通过线程运行完毕时,关闭这个加载中的动画对话框。
Static
ProgressDialog.show (Context context, CharSequence title, CharSequence message)
Static
ProgressDialog.show (Context context, CharSequence title, CharSequence message, boolean indeterminate)
Static
ProgressDialog.show (Context context, CharSequence title, CharSequence message, boolean indeterminate, boolean cancelable)
Static
ProgressDialog.show (Context context, CharSequence title, CharSequence message, boolean indeterminate, boolean cancelable, DialogInterface.OnCancelListener cancelListener)
需要留意的是,第一个参数必须为目前运行Activity的Context,否则会报错,所以在本范例中使用的是MainActivity.this;而为了让跳出的ProgressDialog显示标题以及内容,所以用到了第二第三个参数,最后一个参数可传可不传。