How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class?

前端 未结 17 2153
鱼传尺愫
鱼传尺愫 2020-11-21 04:50

I have this two classes. My main Activity and the one that extends the AsyncTask, Now in my main Activity I need to get the result from the OnPostExecute(

17条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-21 05:09

    I make it work by using threading and handler/message. Steps as follow: Declare a progress Dialog

    ProgressDialog loadingdialog;
    

    Create a function to close dialog when operation is finished.

       private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            loadingdialog.dismiss();
    
        }
        };
    

    Code your Execution details:

     public void startUpload(String filepath) {
        loadingdialog = ProgressDialog.show(MainActivity.this, "Uploading", "Uploading Please Wait", true);
        final String _path = filepath;
        new Thread() {
            public void run() {
                try {
                    UploadFile(_path, getHostName(), getPortNo());
                    handler.sendEmptyMessage(0);
    
                } catch (Exception e) {
                    Log.e("threadmessage", e.getMessage());
                }
            }
        }.start();
    }
    

提交回复
热议问题