How to get a string back from AsyncTask?

前端 未结 8 2029
Happy的楠姐
Happy的楠姐 2020-12-03 04:01

I have the following class:

public class getURLData extends AsyncTask{

@Override
protected String doInBackground(String... pa         


        
8条回答
  •  情话喂你
    2020-12-03 04:41

    If the user clicks the button, then has to wait for the content, what do they do meanwhile?

    Why not do this:

    1. User clicks button.
    2. You check for connectivity. If the user isn't connected to the Internet, tell them.
    3. You start an IntentService by sending an Intent to send the HTTP request.
    4. When the request finishes, you post a notification.
    5. The user clicks the notification, which returns to an Activity that can do the next step.

    This allows the user to go off and do whatever while the request is being processed.

    An IntentService runs in the background on its own thread. When it receives an Intent, it runs onHandleIntent(). When that method finishes, the Service is cached: it's not active, but it can re-start quickly when the next Intent arrives.

提交回复
热议问题