Few ways. This is the easiest to understand. First, build a class extending AsyncTask<>
. You want a String[]
to be the return type. Note: AsyncTask<Params, Progress, Result>
so if you want to return a string, you need to extend AsyncTask<Void, Void, String>
public class YourAsyncTaskName extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
String token = null;
try {
token = GoogleAuthUtil.getToken(
MainActivity.this,
mGoogleApiClient.getAccountName(),
"oauth2:" + SCOPES);
} catch (IOException transientEx) {
// Network or server error, try later
Log.e(TAG, transientEx.toString());
} catch (UserRecoverableAuthException e) {
// Recover (with e.getIntent())
Log.e(TAG, e.toString());
} catch (GoogleAuthException authEx) {
// The call is not ever expected to succeed
// assuming you have already verified that
// Google Play services is installed.
Log.e(TAG, authEx.toString());
}
return token;
}
@Override
protected void onPostExecute(String token) {
/* here you have your token */
Log.i(TAG, "Access token retrieved:" + token);
}
}
Then to start this task, just create an instance and call .execute();
YourAsyncTaskName asyncTask = new AsyncTaskName();
asyncTask.execute();