2014-04-22 60 views
2

我想在用戶啓動我的應用時實現以下功能:如何在檢查用戶是否登錄時停止啓動活動?

1-如果用戶未登錄,則顯示登錄屏幕。如果用戶已經創建了一個賬戶,並且有一個有效的令牌,則顯示開始屏幕。

爲此,我已經實現了一個基於此處教程http://www.udinic.com/的自定義身份驗證器。

該代碼起作用,我的問題是它會短暫顯示當前活動用戶界面,然後切換到由我的AccountAuthenticator提供的添加帳戶UI。我怎樣才能解決這個問題?

這是代碼:

@Override 
public void onStart(){ 
    super.onStart(); 
    getTokenForAccountCreateIfNeeded(AccountGeneral.ACCOUNT_TYPE, AccountGeneral.AUTHTOKEN_TYPE_FULL_ACCESS); 


} 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    mAccountManager = AccountManager.get(this); 
} 


    /** 
* Get an auth token for the account. 
* If not exist - add it and then return its auth token. 
* If one exist - return its auth token. 
* If more than one exists - show a picker and return the select account's auth token. 
* @param accountType 
* @param authTokenType 
*/ 
private void getTokenForAccountCreateIfNeeded(String accountType, String authTokenType) { 
    final AccountManagerFuture<Bundle> future = mAccountManager.getAuthTokenByFeatures(accountType, authTokenType, null, this, null, null, 
      new AccountManagerCallback<Bundle>() { 
       @Override 
       public void run(AccountManagerFuture<Bundle> future) { 
        Bundle bnd = null; 
        try { 
         bnd = future.getResult(); 
         final String authtoken = bnd.getString(AccountManager.KEY_AUTHTOKEN); 
         showMessage(((authtoken != null) ? "SUCCESS!\ntoken: " + authtoken : "FAIL")); 


        } catch (Exception e) { 
         e.printStackTrace(); 
         showMessage(e.getMessage()); 
        } 
       } 
      } 
    , null); 
} 

力我的用戶在應用的開始登錄。我已經實現了一個基於這裏的教程http://www.udinic.com/的自定義身份驗證器。

回答

1

由於您的身份驗證方法是異步的,因此無法阻止顯示用戶界面。我會執行一個路由器/屏幕快照Activity,檢查身份驗證狀態,然後啓動登錄Activity或實際Activity用戶看到他是否已登錄。請確保您的清單中的路由器Activity設置爲。

如果您想將其保留在單個Activity中,您應該在佈局中放置全屏加載指示器,該指示器位於UI之上,並在您找出要顯示的佈局後淡出。