Skip to main content
Provided some code
Source Link
n0rmzzz
  • 3.8k
  • 2
  • 30
  • 38

EDIT: This is an example of wrapping an existing functionality into an AsyncTask:

private class GetAuthorisationUrlTask extends AsyncTask<String, Void, String>
{
    private OAuthBuilder oAuthBuilder;
    private GetAuthorisationUrlTaskCallback callback;

    public GetAuthorisationUrlTask(Context context, OAuthBuilder oAuthBuilder, GetAuthorisationUrlTaskCallback callback)
    {
        this.oAuthBuilder = oAuthBuilder;
        this.callback = callback;
    }

    @Override
    protected String doInBackground(String... params)
    {
        try
        {
            Log.i(TAG, "Getting authorization url...");
            return oAuthBuilder.getAuthorizationUrl();
        }
        catch (Exception e)
        {
            Log.e(TAG, "Unable to get OAuth authorization url: " + e, e);
            return null;
        }
    }

    @Override
    protected void onPostExecute(String result)
    {
        Log.i(TAG, "Authorisation url: " + result);
        if (result == null)
            Toast.makeText(getApplicationContext(), "Error getting authorization url.", Toast.LENGTH_SHORT).show();
        else
            callback.urlCreated(result);
    }

    static interface GetAuthorisationUrlTaskCallback
    {
        void urlCreated(String url);
    }
}

Now, you can call the AsyncTask and get a callback when the result is ready.

GetAuthorisationUrlTask getAuthorisationUrlTask = new GetAuthorisationUrlTask(this,
        new GetAuthorisationUrlTaskCallback()
        {
            @Override
            public void urlCreated(String url)
            {
                webview.loadUrl(url);
            }
        });
getAuthorisationUrlTask.execute();

I know there is so much boiler-plate code, but it separates your UI completely from the logic of your business class. It does only one single task here, but extending it to be more generic and do more without introducing new classes is easy.

EDIT: This is an example of wrapping an existing functionality into an AsyncTask:

private class GetAuthorisationUrlTask extends AsyncTask<String, Void, String>
{
    private OAuthBuilder oAuthBuilder;
    private GetAuthorisationUrlTaskCallback callback;

    public GetAuthorisationUrlTask(Context context, OAuthBuilder oAuthBuilder, GetAuthorisationUrlTaskCallback callback)
    {
        this.oAuthBuilder = oAuthBuilder;
        this.callback = callback;
    }

    @Override
    protected String doInBackground(String... params)
    {
        try
        {
            Log.i(TAG, "Getting authorization url...");
            return oAuthBuilder.getAuthorizationUrl();
        }
        catch (Exception e)
        {
            Log.e(TAG, "Unable to get OAuth authorization url: " + e, e);
            return null;
        }
    }

    @Override
    protected void onPostExecute(String result)
    {
        Log.i(TAG, "Authorisation url: " + result);
        if (result == null)
            Toast.makeText(getApplicationContext(), "Error getting authorization url.", Toast.LENGTH_SHORT).show();
        else
            callback.urlCreated(result);
    }

    static interface GetAuthorisationUrlTaskCallback
    {
        void urlCreated(String url);
    }
}

Now, you can call the AsyncTask and get a callback when the result is ready.

GetAuthorisationUrlTask getAuthorisationUrlTask = new GetAuthorisationUrlTask(this,
        new GetAuthorisationUrlTaskCallback()
        {
            @Override
            public void urlCreated(String url)
            {
                webview.loadUrl(url);
            }
        });
getAuthorisationUrlTask.execute();

I know there is so much boiler-plate code, but it separates your UI completely from the logic of your business class. It does only one single task here, but extending it to be more generic and do more without introducing new classes is easy.

Source Link
n0rmzzz
  • 3.8k
  • 2
  • 30
  • 38

The point here is that "you cannot use this class form a UI thread", then you create an AsyncTask. What if you had a service and wanted to call some API methods from there? You can't call AsyncTask from a service thread.
I would leave this class as it is and wrap it in an AsyncTask when I wanted to call it from UI.