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.