How do I retrieve the data from AsyncTasks doInBackground()?

后端 未结 6 1051
耶瑟儿~
耶瑟儿~ 2020-11-28 08:13

I\'ll keep this one as simple as I can.

I have a method in my control layer that uses a class CallServiceTask that extends AsyncTask. When

6条回答
  •  借酒劲吻你
    2020-11-28 08:49

    You can use get() to retrieve your value/object back from AsyncTask.

    new CallServiceTask().execute(parameters).get();
    

    This will return you computed result that you are returning. But this will block your UI till your background process is completed.

    Another option is to create an Interface or BroadcastReceiver that you return you the value as soon as your doInBackground() is completed. I had created a demo for the same using Interface and BroadcastReceiver you can check if from my github

    Also, by using second approach your UI will not be blocked!

提交回复
热议问题