button remains pressed while the Asynctask is being executed

前端 未结 1 429
挽巷
挽巷 2020-12-20 10:30

I have a button, which on pressed, executes the following code:

public void onClick(View v) {
            // TODO Auto-generated method stub
            //pr         


        
相关标签:
1条回答
  • 2020-12-20 10:57

    Don't use get().

    data=new WebkioskExtractor().execute(username,password).get();  // Bad! :(
    
    data=new WebkioskExtractor().execute(username,password);  // Good! :) 
    

    It blocks the UI which is why your Button remains pressed. This is also why your ProgressBar isn't showing up (it also runs on the UI). I assume you start your ProgressBar in onPreExecute() and dismiss() it in onPostExecute() of your AsyncTask. If not, this is what you should be doing. If everything else in your AsyncTask is set up correctly then removing .get() should fix your problem.

    Return your result from doInBackground() to onPostExecute() and this should give you what you want. You can also do whatever you need to on the UI from onPostExecute() or any other method of the AsyncTask besides doInBackground().

    ProgressBar

    You don't need to set the Visibility on the ProgressBar. See this example:

    public class GetUsersTask extends AsyncTask<Void, Void, Void> {
        ProgressDialog progress = ProgressDialog.show(LoginScreen.this, "Downloading Users", "Please wait while users are downloaded");
         // you can create it here
    
        @Override
        protected void onPreExecute()
        {
            // show it here like so
            progress.setCancelable(false);
            progress.isIndeterminate();
            progress.show();
        }
    
        @Override
        protected void onPostExecute(Void result) {
    
                // and dismiss it here
                progress.dismiss();
            }
    
        } 
    
        @Override
        protected void onProgressUpdate(Void... values) {
            // can update the ProgressBar here if you need to
        }
    
        @Override
        protected Void doInBackground(Void... params) {
            ...
             }
    

    Using a callback

    If you need to get a result from AsyncTask which is not an inner-class of your Activity then you can use an interface with a callBack. More about doing that in this answer

    AsyncTask Docs

    0 讨论(0)
提交回复
热议问题