Android版的AccountManager [英] Android AccountManager

查看:104
本文介绍了Android版的AccountManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我一步一步的方法来使用在Android中的AccountManager与简约的例子一起为了更好的理解?

Can some one help me with step by step approach to use the AccountManager in android along with a minimalistic example for better understanding?

推荐答案

我其实回答这个,所以我可以得到一个清晰的认识自己,所以这里去(我决不是精通与Android还):

I'm actually answering this so I can get a clear understanding myself, so here goes (I'm by no means proficient with Android yet):

应用程序通常会想​​先检查帐户的存在,你可以使用:

An application will usually want to check for the existence of an account first, you can use:

AccountManager mgr = AccountManager.get(getApplicationContext());
Account[] accounts = mgr.getAccountsByType("com.mydomain");
// assert that accounts is not empty

您需要使用一个 AccountManagerFuture<&捆绑GT; 持有认证令牌的结果。这必须是异步,因为Android的设备可能会要求用户在此期间登录:

You'll want to use an AccountManagerFuture<Bundle> to hold results of the authentication token. This has to be async since the Android device may ask the user to login in the meantime:

private AccountManagerFuture<Bundle> myFuture = null;
private AccountManagerCallback<Bundle> myCallback = new AccountManagerCallback<Bundle>() {
    @Override public void run(final AccountManagerFuture<Bundle> arg0) {
        try {
           myFuture.getResult().get(AccountManager.KEY_AUTHTOKEN); // this is your auth token
       } catch (Exception e) {
           // handle error
       }
   }

}

现在,你可以要求身份验证令牌异步:

Now you can ask for the auth token asynchronously:

myFuture = mgr.getAuthToken(accounts[0], AUTH_TOKEN_TYPE, true, myCallback, null);

AUTH_TOKEN_TYPE 取决于您的身份验证机制。对于谷歌账号的那简直是'啊'。

AUTH_TOKEN_TYPE is dependent on your authentication mechanism. For google accounts it is simply 'ah'.

现在,只要你做了一个身份验证的请求只是沿着令牌传递(在标题中,作为一个参数,等等),所以在服务器端知道你是谁。

Now whenever you do an authenticated request just pass along the token (in the header, as a param, etc), so the server side knows who you are.

这篇关于Android版的AccountManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆