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/的自定義身份驗證器。