Skip to content Skip to sidebar Skip to footer

Asynctask - Return A String In Postexecute To The Activity, Which Started The Async Task Without Blocking The Ui Thread

I have an Activity, which starts an AsyncTask with an Implemented process dialog. That works fine! But i want to get a String return when the asyncTask has finished. So i have to r

Solution 1:

Create a class in your project which extends activity as shown below:

publicclassSomeClassextendsActivity
{
    publicvoiddataFromPostExecute(String data) 
    {
        System.out.println("in mainactivity");
    }
}

If you want a single thread for every activity, just create a class which extends Application

publicclassAsyncextendsApplication
{
privateSocket globalSocket;

    @OverridepublicvoidonCreate()
    {
        //socket = null;
    }

    publicSocketgetglobalSocket() 
    {
        return globalSocket;
    }

    publicvoidsetGlobalSocket(Socket globalSocket) 
    {
        this.globalSocket = globalSocket;
    }
}

In your socket class which extends Asynctask do the following:

publicSocketClassextendsAsyncTask<String,String,String>
{
    Async app;
    privateSomeClass t_act;
    publicSocketClass(SomeClass sct) 
    {
        t_act = sct;
        this.con = tst;
        app= ((Async)sct.getApplicationContext());
    }

    protectedvoidonPostExecute(String data)
    {
        t_act.dataFromPostExecute(data);
    }
}

Then, in your activity extend SomeClass and do as shown below:

publicclassActivity1extendsSomeClass
{
    publicvoiddataFromPostExecute(String data)
        {
            //do whatever you want. "data" of this method contains the values from                                     postexecute()
        }
}

Solution 2:

Your return value from doInBackground() is you formal in onPostExecute(). So you should just be able to pass it in.

Solution 3:

What do i have to write into onPostExecute and the Activity grab the string from doInBackground?

When you are using AsyncTask then you can update your UI only on onProgressUpdate and onPostExecute method. Your doInBackground() method returns some data and these data is going to onPostExecute method(it depends also how your generic are declared). Generally, there is no another approaches how to do it.

You meant this:

AsyncTaska=newAsyncTask(Context);
a.execute(Input);

First means that your constructor looks like

publicMyAsync(Context c){
   this.c = c;
}

Second means that you declared your first generic type(assumption Input param is String) as

privateclassMyAsyncextendsAsyncTask<String, Void, String> {
  //...
}

And you want to update UI with String that returns doInBackground() method and merely place is onPostExecute method with IN parameter String that returns doInBackground().

protectedvoidonPostExecute(String stringReturnedFromDoInBackground) 
{
   // some work
}

So if you want to do it in different way, change your application logic and use for example ResultReceiver with IntentService.

Post a Comment for "Asynctask - Return A String In Postexecute To The Activity, Which Started The Async Task Without Blocking The Ui Thread"