Async task to show an AlertDialog

前端 未结 2 660
北荒
北荒 2020-12-03 19:28

For the past few days, I haven\'t been able to solve an issue with my dialog box. I am running a thread to show the dialog box for 5000ms and removing it. and I am trying to

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-03 19:53

    Why you don't use Android AsyncTask? For example:

    public class MyPreloader extends AsyncTask{
    private Context context;
    private ProgressDialog dialog;
    
    public MyPreloader(Context context){
        this.context = context;
    }
    
    @Override
    protected void onPreExecute() {
        dialog = new ProgressDialog(context);
        dialog.setMessage("Please wait...");
        dialog.setIndeterminate(true);
        dialog.show();
        super.onPreExecute();
    }   
    
    @Override
    protected ResponseBase doInBackground(InputObject... params) {
    InputObject input = params[0]; 
    //some code for background work
        }
    
    @Override
    protected void onPostExecute(OutputObject result) {
        if (dialog.isShowing()) {
            dialog.dismiss();
        }
        super.onPostExecute(result);
    }
    

提交回复
热议问题