2014-10-08 36 views
0

我正在使用方法來獲取值。我正在成功地獲得價值,但問題是價值retun空在mehod看到我的代碼: -帶方法的java方法返回類型

String authtoken; 

public String getAuthTokenForCurrentUser(Activity act, 
     final Account account, final String authTokenType) { 

    final AccountManager mAccountManager = AccountManager.get(act); 
    final AccountManagerFuture<Bundle> future = mAccountManager 
      .getAuthToken(account, authTokenType, null, act, null, null); 

    new Thread(new Runnable() { 
     @Override 
     public void run() { 
      try { 
       Bundle bnd = future.getResult(); 

       authtoken = bnd.getString(AccountManager.KEY_AUTHTOKEN); 

       Log.e("token in method", authtoken); // getting successfully 
      } catch (Exception e) { 
       e.printStackTrace(); 
       showMessage(e.getMessage()); 
      } 
     } 
    }).start(); 

    return authtoken; 
}  

//call here 


MyClass iHealthApp = new MyClass(); 
    String accessToken = iHealthApp.getAuthTokenForCurrentUser(ClientActivity.this, currentAppUserAccount,AccountGeneral.AUTHTOKEN_TYPE_FULL_ACCESS); 
    Log.e("access token","token "+ accessToken); // this is null here 

回答

2

您應該使用Callable檢索執行(與Future一起)的結果,還是應該等待線程完成檢索分配的authtoken。在你的情況下,發生的情況是:當你的線程啓動時,你的方法已經返回,返回未分配的值authtoken

+0

謝謝。我解決了這個問題.. – aditay 2014-10-08 08:28:31