External AsyncTask class with ProgressDialog [Update: and returning back?]

后端 未结 3 1484
悲&欢浪女
悲&欢浪女 2020-12-09 23:37

**Updated: (See below)**I have been looking around for couple of days and can\'t find a straight answer to this. Some say it possible to some say to accomplish some say it\

3条回答
  •  青春惊慌失措
    2020-12-10 00:22

    You were creating the ProgressDialog with a null context. The following code worked for me.

    public class AsyncClass extends AsyncTask {
        private Context context;
        ProgressDialog dialog;
    
            public AsyncClass(Context cxt) {
                context = cxt;
                dialog = new ProgressDialog(context);
            }
    
            @Override
            protected void onPreExecute() {
                dialog.setTitle("Please wait");
                dialog.show();
            }
    
            @Override
            protected Void doInBackground(Void... unused) {
                SystemClock.sleep(2000);
                return (null);
            }
    
            @Override
            protected void onPostExecute(Void unused) {
                dialog.dismiss();
            }
        }
    

提交回复
热议问题