-1

I want download details from web and update the UI within the doInBackground(), For that I think I must get reference to activity within that method .How can I do it or is there another way to do that? What must be the something parameter? Or can’t update UI real-time?

public class DownloadActivity extends ListActivity {

public class DownloadItems extends AsyncTask<Something,Integer,Long> {

    @Override
    protected Long doInBackground(DownloadActivity... params) {

        Toast.makeText(params[0], getIntent().getExtras().get("location").toString(), Toast.LENGTH_SHORT).show();
        return null;
    }

}

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    new DownloadItems().execute(Something);
}

}
CodingGorilla
  • 19,612
  • 4
  • 45
  • 65
user2722364
  • 65
  • 1
  • 8

5 Answers5

2

You can either use a Handler or update your UI in onPostExecute(), which I recommend. Let your Async take care of its background logic and update the UI when that work is finished.

Jade Byfield
  • 4,668
  • 5
  • 30
  • 41
  • 1
    onPostExecute is called after doInBackground finishes, for multiple items and to feedback on each one override onProgressUpdate to feedback on each item as it happens. – Daniel S. Fowler Aug 27 '13 at 16:52
  • Kindly,can you post some example code?It sounds like what i looking for----> – user2722364 Aug 28 '13 at 05:24
0

You can create a constructor for passing or adding Context as a parameter.

public class DownloadItems extends AsyncTask<Something,Integer,Long> {

    Context context;

    public DownloadItems(Context cntx){
        context = cntx;
    }
    @Override
    protected Long doInBackground(DownloadActivity... params) {

        //Toast.makeText(params[0], getIntent().getExtras().get("location").toString(), Toast.LENGTH_SHORT).show();
        Toast.makeText(context, "String test", Toast.LENGTH_SHORT).show();
        return null;
    }

}

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    new DownloadItems(this).execute(Something);
}

By passing the context of the activity you can make any operation that are context related.

Ariel Magbanua
  • 3,083
  • 7
  • 37
  • 48
  • Wouldn't this throw an exception? – Jade Byfield Aug 27 '13 at 16:43
  • Its work fine but cann't add View to the activity. this.addContentView(view, params) work inside onCreate but not with Context ctx within onPostExecute(),Why is that and how can i solve that? ---->please help me<----- – user2722364 Aug 28 '13 at 07:49
  • Thanks for showing usage of constructor in this problem ,I had able to add view by simple change Context to type of DownloadActivity – user2722364 Aug 28 '13 at 08:19
  • You mean you want to a android View on the layout of your activity? What are you trying to achieve If I solved this already base on your current question, it would be great to accept this as an answer :) – Ariel Magbanua Aug 28 '13 at 09:38
0

You can't execute UI operations in doInBackground(), you must do them in onPostExecute(). In DownloadActivity, you will create an instance of DownloadItems, and pass it the url where you want to download your stuff : For example :

public class DownloadActivity extends ListActivity {

    private void someMethod() {
        DownloadItems yourTask = new DownloadItems(getApplicationContext());
        yourTask.execute(yourUrl);
    }

In the AsyncTask, you will do your download operations in doInBackground() and return the result so it can be handled by onPostExecute() :

public class DownloadItems extends AsyncTask<Something,Integer,Long> {

    Context mContext;

    public DownloadItems(Context context){
        mContext = context;
    }

    @Override
    protected String doInBackground(String... params) {

        String theResult;
        // download operations using url stored in params[0], and where you set theResult variable (for example...)
        return theResult;
    }

In onPostExecute(), you deal with the result, for example in your code above, you can call the Toast :

      @Override
      protected void onPostExecute(String result) {
          Toast.makeText("YOUR TAG", result, Toast.LENGTH_SHORT).show();
      }
jbihan
  • 3,053
  • 2
  • 24
  • 34
  • Its work fine but cann't add View to the activity. this.addContentView(view, params) work inside onCreate but not with Context ctx within onPostExecute(),Why is that and how can i solve that? ---->please help me<----- – user2722364 Aug 28 '13 at 07:50
  • Thanks for showing usage of constructor in this problem ,I had able to add view by simple change Context to type of DownloadActivity – user2722364 Aug 28 '13 at 08:19
  • You can cast your Context to an Activity : `private Activity = (Activity) mContext` then you can call `addView()` on mActivity. Then you can accept my answer :) – jbihan Aug 28 '13 at 10:55
0

The best way is to simply move anything which affects UI into onPostExecute() because it's there to allow you to update the UI, it's the point of it.

There are other ways but when using AsyncTask there's really no reason not to use this.

public class DownloadActivity extends ListActivity {

    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        new DownloadItems(this).execute();
    }

    public class DownloadItems extends AsyncTask<Something,Integer,Long> {

        private Context context;

        public DownloadItems(Context c){
            context = c;
        }

        @Override
        protected Long doInBackground(DownloadActivity... params) {
            // Do something
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            Toast.makeText(context, context.getIntent().getExtras().get("location").toString(), Toast.LENGTH_SHORT).show();
        }
    }
}
whitfin
  • 4,539
  • 6
  • 39
  • 67
  • Its work fine but cann't add View to the activity. this.addContentView(view, params) work inside onCreate but not with Context ctx within onPostExecute(),Why is that and how can i solve that? ---->please help me<----- – user2722364 Aug 28 '13 at 07:52
  • Thanks for showing usage of constructor in this problem ,I had able to add view by simple change Context to type of DownloadActivity – user2722364 Aug 28 '13 at 08:16
0

You can call this in doInBackground:

runOnUiThread(new Runnable() {
   public void run() {
       //Your code
   }
});

But isn't right... Please read the AsyncTask for more details, or use the onPostExecute to update UI...

William Da Silva
  • 703
  • 8
  • 19