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/的自定义身份验证器。