gilllsss
gilllsss

Reputation: 97

How to pass function as paramater to AsynTask

since im new in android development, and i need to provide an asynctask class for my http request. i have a lot of http request function type in one activity, and i want to make it dynamic. so i wanted to create only one AsyncTask function that can run all my function.

so this is the example

  private class WebServiceCall extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
                WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
        pBar.setVisibility(View.VISIBLE);
        pBar.setIndeterminate(false);
        pBar.setClickable(false);
    }
    @Override
    protected Void doInBackground(Void... params) {
        // a function that i passed
       Function01();
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        try{
         some code
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
        pBar.setClickable(true);
        pBar.setVisibility(View.GONE);
    }

}

and i just call like this

Oncreate(
new WebServiceCall().execute(function01());

)

any help and code sample would be appreciate, thanks

Upvotes: 1

Views: 80

Answers (3)

GreyBeardedGeek
GreyBeardedGeek

Reputation: 30088

This doesn't address your question directly, but I urge you to investigate both the fairly-well advertised problems with using AsyncTask for anything that's likely to take more than a few milliseconds, and the several really good HTTP / REST frameworks for Android, e.g. Retrofit.

Upvotes: 0

Atef Hares
Atef Hares

Reputation: 4891

I don't know what you mean by a function as a parameter to another function!

but you can use Interfaces for this purpose.

for example:

create an Call Back interface that can be called in onPostExecute()

public interface ResponseCallback {

    void onRespond();
}

and before calling asynckTask define it like this:

   ResponseCallback callback = new ResponseCallback() {
            @Override
            public void onRespond() {
              //code to be done after calling it from onPostExecute
            }
        };

and pass callback to the constructor of of the asynckTask and call it in onPostExecute


of course you can modify the signature of the interface to what ever you want.

Upvotes: 2

Aksenov Vladimir
Aksenov Vladimir

Reputation: 707

Send class object with your function and call function from object in AsyncTask.

public class A
{
//your function
int function()
{
return...;
}
}

private class WebServiceCall extends AsyncTask<Void, Void, Void> {
    A myobj;
    WebServiceCall(A mycustomslass)
    {
         myobj = mycustomclass;
    }
    @Override
    protected void onPreExecute() {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
                WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
        pBar.setVisibility(View.VISIBLE);
        pBar.setIndeterminate(false);
        pBar.setClickable(false);
    }
    @Override
    protected Void doInBackground(Void... params) {
        // a function that i passed

        int cur = myobj.function();//this your function
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        try{
         some code
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
        pBar.setClickable(true);
        pBar.setVisibility(View.GONE);
    }

}

and you can call like

Oncreate(
new WebServiceCall(new A()).execute();
)

Upvotes: 0

Related Questions