AsyncTask return value

前端 未结 4 1599
死守一世寂寞
死守一世寂寞 2020-12-05 06:47

My android app connects to my website to retrieve and upload information so I use an AsyncTask thread.

In one instance, I need my thread to return a true or a false

4条回答
  •  盖世英雄少女心
    2020-12-05 07:38

    You can use AsyncTask get() method for this. It waits if necessary for the computation to complete, and then retrieves its result:

    Toast.makeText(Locate.this, "Testing : " + locationUpdate.execute(location).get(), Toast.LENGTH_LONG).show();
    

    But be sure to not block the main thread for a long period of time, as this will lead to unresponsive UI and ANR.

    UPDATE
    I missed the point that question was about async web download/upload. Web/network operation should considered as a long one and thus the approach "pause UI thread and wait till download finishes" is always a wrong one. Use usual result publishing approach intstead (e.g.: AsyncTask.onPostExecute, Service + sendBroadcast, libraries like Volley, RoboSpice, DataDroid etc).

提交回复
热议问题