2015-08-31 15 views
0

我正試圖在我的應用上實現谷歌登錄。我設法登錄並存儲令牌,但由於任務是異步的,我不知道它何時完成,因此我無法在其他方法中安全地使用該令牌。我怎樣才能將回調方法添加到onPostExecuteAndroid:如何在獲取Google令牌的異步任務中實現回調?

下面是代碼:

@Override 
public void onConnected(Bundle bundle) { 
    // onConnected indicates that an account was selected on the device, that the selected 
    // account has granted any requested permissions to our app and that we were able to 
    // establish a service connection to Google Play services. 
    Log.d(TAG, "onConnected:" + bundle); 
    mShouldResolve = false; 

    mAccountName = Plus.AccountApi.getAccountName(mGoogleApiClient); 

    //HERE I RETRIEVE THE TOKEN AND NEED TO IMPLEMENT CALLBACK 
    new RetrieveTokenTask().execute(mAccountName); 


    // Show the signed-in UI 
    Intent intent = new Intent(getActivity(), MainActivity.class); 
    startActivity(intent); 
} 

@Override 
public void onClick(View v) { 

    if (v.getId() == R.id.sign_in_button) { 
     onSignInClicked(); 
    } 
} 

private void onSignInClicked() { 
    // User clicked the sign-in button, so begin the sign-in process and automatically 
    // attempt to resolve any errors that occur. 
    mShouldResolve = true; 
    mGoogleApiClient.connect(); 

    // Show a message to the user that we are signing in. 
    //mStatusTextView.setText(R.string.signing_in); 
    Log.i("GoogleSignIn", "in progress"); 
} 


private class RetrieveTokenTask extends AsyncTask<String, Void, String> { 

    @Override 
    protected String doInBackground(String... params) { 
     String accountName = params[0]; 
     String scopes = "oauth2:profile email"; 
     String token = null; 
     try { 
      token = GoogleAuthUtil.getToken(getActivity().getApplicationContext(), accountName, scopes); 
     } catch (IOException e) { 
      Log.e(TAG, e.getMessage()); 
     } catch (UserRecoverableAuthException e) { 
      //startActivityForResult(e.getIntent(), REQ_SIGN_IN_REQUIRED); 
     } catch (GoogleAuthException e) { 
      Log.e(TAG, e.getMessage()); 
     } 
     return token; 
    } 

    @Override 
    protected void onPostExecute(String token) { 
     super.onPostExecute(token); 
     Log.i("Token Value: ", token); 
     //TODO : access token verifier https://developers.google.com/identity/sign-in/android/backend-auth 
     accessToken = token; 
    } 
} 

回答

1

u能在onPostExecute方法啓動的主要活動.. 這樣U將確保該任務已執行和即將完成。

入住這

private class RetrieveTokenTask extends AsyncTask<String, Void, String> { 

private Callback callback; 

public RetrieveTokenTask(Callback callback){ 
    this.callback = callback; 
} 

@Override 
protected String doInBackground(String... params) { 
    String accountName = params[0]; 
    String scopes = "oauth2:profile email"; 
    String token = null; 
    try { 
     token = GoogleAuthUtil.getToken(getActivity().getApplicationContext(), accountName, scopes); 
    } catch (IOException e) { 
     Log.e(TAG, e.getMessage()); 
    } catch (UserRecoverableAuthException e) { 
     //startActivityForResult(e.getIntent(), REQ_SIGN_IN_REQUIRED); 
    } catch (GoogleAuthException e) { 
     Log.e(TAG, e.getMessage()); 
    } 
    return token; 
} 

@Override 
protected void onPostExecute(String token) { 
    super.onPostExecute(token); 
    Log.i("Token Value: ", token); 
    //TODO : access token verifier https://developers.google.com/identity/sign-in/android/backend-auth 
    accessToken = token; 
    callback.done(); 
} 

}

//create an interface 
public interface Callback{ 
    //create the callback method 
    void done(); 
} 

當啓動任務,通過回調接口的實例在這樣的例如構造

new RetrieveTokenTask(this).execute(mAccountName); 

並使您的活動實現回調接口。 例如MainActivity implements Callback

現在MainActivity將有一個完成的方法,這種方法是你的回調。

我希望這個解釋很有幫助。

+0

當然,我可以這樣做,但我的問題是如果有可能添加一個回調方法異步任務;) –

+0

哇,謝謝! –

+0

歡迎您:) – vishnus

0

請勿在Android上使用AsyncTask。這是糟糕的,非常糟糕。你會開始有內存泄漏,這是討厭的。

這裏的更多信息,爲什麼是的AsyncTask Android上的壞:http://simonvt.net/2014/04/17/asynctask-is-bad-and-you-should-feel-bad/

爲了您的網絡調用,爲什麼試圖推倒重來?有很多已經建立和測試過的庫可以爲你做所有的努力工作,甚至爲你提供回調。

我推薦你來自Square的Retrofit,他們提供同步呼叫以及異步(回調)和可觀察性。只需選擇一個你想要的,很可能是回調異步。

+0

我使用這個,因爲它是我在幾個地方找到的,可以在谷歌登錄中找回令牌(包括官方的Google文檔https://developers.google.com/identity/sign- in/android/backend-auth),一旦一切正常,我會盡力實現你的建議:) –

相關問題