Insert progress spinner into splashscreen android

后端 未结 1 1707
南方客
南方客 2021-01-14 16:39

In my android application,i would like to place a progress bar such that it shows the user that the data is getting downloaded and gets dismissed once the data is loaded.

相关标签:
1条回答
  • 2021-01-14 17:15

    You can achieve it by AsyncTask class.

    In that three steps you have to follow,

    1. you have to start the ProgreesDialog in onPreExecute().
    2. doInBackground() takes control of the Downloading Progress.
    3. 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();
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题