如何在android中的Asynk任务中使用自定义进度条

时间:2016-03-07 07:03:57

标签: android

public class LoginActivity extends Activity {

    public static final int DIALOG_LOADING = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        showDialog(DIALOG_LOADING);
        Thread thread =  new Thread(null, doSomeTask);
        thread.start();

    }


    private Runnable doSomeTask = new Runnable() {
        public void run() {
            try {
                //Code of your task
                Thread.sleep(10000);
            } catch (InterruptedException e) {}
            //Done! now continue on the UI thread
            runOnUiThread(taskDone);
        }
    };

    //Since we can't update our UI from a thread this Runnable takes care of that!
    private Runnable taskDone = new Runnable() {
        public void run() {
            dismissDialog(DIALOG_LOADING);
        }
    };



@Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DIALOG_LOADING:
                final Dialog dialog = new Dialog(this, android.R.style.Theme_Translucent);
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                //here we set layout of progress dialog
                dialog.setContentView(R.layout.custom_progress_dialog);
                dialog.setCancelable(true);
                dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                    public void onCancel(DialogInterface dialog) {
                        // TODO Auto-generated method stub

                    }
                });
                return dialog;

            default:
                return null;
        }
    };

使用Upper代码我能够显示自定义进度条,但它在具有指定时间的线程上设置但我在下面有asynk任务。

   class download extends AsyncTask<Void,Void,Void>
{
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... voids) {
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
    }
}

我想在后台完成后设置这里进度条然后进度条应该消失。请指教我

5 个答案:

答案 0 :(得分:2)

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog = new ProgressDialog(TabsActivity.this);
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setIndeterminate(false);
        dialog.setMessage("Loading ...");
        dialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {        
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
            dialog.dismiss();              
    }

答案 1 :(得分:1)

使用AsyncTask,您可以在onPreExecute()中显示对话框,并在onPostExecute()中下载数据后,您可以将其关闭。将下载任务开始到doInBackground()。

public class Download extends AsyncTask<Void,Void,Void>{
    private Activity activity;
    private static final int DIALOG_LOADING = 1;
    public Download(Activity activity){
         this.activity = activity;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // before start download task
        activity.showDialog(DIALOG_LOADING);
    }

    @Override
    protected Void doInBackground(Void... voids) {
         // start dowanload task here
         return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        // after downloaded data
        activity.dismissDialog(DIALOG_LOADING);
    }
}

答案 2 :(得分:0)

Runnable r = new Runnable()
{
@Override
public void run()
{
    // your code here
}
};

 Thread t = new Thread(r);
 t.start();

AsyncTask任务

class download extends AsyncTask<Void,Void,Void>
{
@Override
protected void onPreExecute() {
    super.onPreExecute();
    showDialog(DIALOG_LOADING); //custom alert start when AsyncTask start

}

@Override
protected Void doInBackground(Void... voids) {
    return null;
}

@Override
protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);
  dismissDialog(DIALOG_LOADING);  //custom dialog  Dissmiss when AsyncTask completed

}
}

答案 3 :(得分:0)

您好,我只是修改

public class LoginActivity extends Activity {

    public static final int DIALOG_LOADING = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);


        new download().execute();

    }


   class download extends AsyncTask<Void,Void,Void>
{
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
       showDialog(DIALOG_LOADING);
    }

    @Override
    protected Void doInBackground(Void... voids) {
        //do your stuff here 
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        dismissDialog(DIALOG_LOADING);
    }
}


@Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DIALOG_LOADING:
                final Dialog dialog = new Dialog(this, android.R.style.Theme_Translucent);
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                //here we set layout of progress dialog
                dialog.setContentView(R.layout.custom_progress_dialog);
                dialog.setCancelable(true);
                dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                    public void onCancel(DialogInterface dialog) {
                        // TODO Auto-generated method stub

                    }
                });
                return dialog;

            default:
                return null;
        }
    };

答案 4 :(得分:0)

public class MyAsyncTask extends AsyncTask<String, String, String> {
private Context context;
private ProgressDialog pDialog;

public SavePatientData(Context context) {
    this.context = context;
}

@Override
protected void onPreExecute() {

    super.onPreExecute();
    pDialog = null;
    pDialog = new ProgressDialog(context, ProgressDialog.THEME_HOLO_LIGHT);
    pDialog.setMax(100);
    pDialog.setCancelable(false);
    pDialog.setMessage("Saving\n .....");
    pDialog.setContentView(// your coustom layout);
    pDialog.show();
}

@Override
protected String doInBackground(String... params) {
    // do someting
    return null;
}

@Override
protected void onProgressUpdate(String... values) {
    super.onProgressUpdate(values);
    pDialog.setProgress(Integer.parseInt(values[0]));
}

@Override
protected void onPostExecute(String result) {
    if ((pDialog != null) && (pDialog.isShowing())) {
    pDialog.dismiss();
}

}