In my android application,i would like to place a progress bar such that it shows the user that the data is gettin开发者_如何学Pythong downloaded and gets dismissed once the data is loaded.
Is there any way that i can achieve this.
Thanks in advance:)
You can achieve it by AsyncTask class.
In that three steps you have to follow,
- you have to start the ProgreesDialog in
onPreExecute()
. doInBackground()
takes control of the Downloading Progress.onPostExcecute()
runs after the second step. on that you can dismiss your progressdialog, start the New Activity and finish your splashscreen.
More Info, check the Documentation. It has a explanation with example code.
CODE:
private class Task extends AsyncTask<Void, Void, Void> {
private final ProgressDialog dialog = new ProgressDialog(
your_class.this);
// can use UI thread here
protected void onPreExecute() {
this.dialog.setMessage("Loading...");
this.dialog.setCancelable(false);
this.dialog.show();
}
@Override
protected Void doInBackground(Void... params) {
try {
// do downloading images code here
} catch (Exception e) {
}
return null;
}
protected void onPostExecute(Void result) {
//start the another activity and then close your current activity here.
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
}
}
精彩评论