获取电子邮件地址后获取姓名和图片

时间:2011-11-17 12:58:07

标签: android email username accountmanager

我通过帐户管理员使用以下代码获取用户的电子邮件地址:

package g.login;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AccountManagerCallback;
import android.accounts.AccountManagerFuture;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class GLogin extends Activity {

    private AccountManager _accountMgr = null;
    private GLogin _this;

    /** Called when the activity is first created. */
    @Override    
    public void onCreate(Bundle savedInstanceState) {

        _this = this;

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        try {
            _accountMgr = AccountManager.get(this);
            Account [] accounts = _accountMgr.getAccountsByType("com.google");
            String accountsList = "Accounts: " + accounts.length + "\n";
            for (Account account : accounts) {
                accountsList += account.toString() + "\n";
            }
            setMessage(accountsList);
        } catch (Exception e) {
            setMessage(e.toString());
        }

        Button loginBtn = (Button) findViewById(R.id.login);
        loginBtn.setOnClickListener(mLoginListener);
    }

    private OnClickListener mLoginListener = new OnClickListener() {
        public void onClick(View v) {
            try {
                Account [] accounts = _accountMgr.getAccounts();
                if (accounts.length == 0) {
                    setResult("No Accounts");
                    return;
                }
                Account account = accounts[0];
                _accountMgr.getAuthToken(account, "mail", false, new GetAuthTokenCallback(), null);
            } catch (Exception e) {
                setResult(e.toString());
            }
        }
    };

    private class GetAuthTokenCallback implements AccountManagerCallback<Bundle> {
        public void run(AccountManagerFuture<Bundle> result) {
            Bundle bundle;
            try {
                bundle = result.getResult();
                Intent intent = (Intent)bundle.get(AccountManager.KEY_INTENT);
                if(intent != null) {
                    // User input required
                    startActivity(intent);
                } else {
                    _this.setResult("Token: " + bundle.getString(AccountManager.KEY_AUTHTOKEN) + "\n" +
                            "KEY_ACCOUNT_NAME: " + bundle.getString(AccountManager.KEY_ACCOUNT_NAME));
                    Log.e("TOKEN", bundle.getString(AccountManager.KEY_AUTHTOKEN));
                }
            } catch (Exception e) {
                _this.setResult(e.toString());
            }
        }
    };

    public void setResult(String msg) {
        TextView tv = (TextView) this.findViewById(R.id.result);
        tv.setText(msg);
    }

    public void setMessage(String msg) {
        TextView tv = (TextView) this.findViewById(R.id.message);
        tv.setText(msg);
    }
}

我能够提取authtoken和ACCOUNT_NAME(类似于example@gmail.com)。 我的问题是,在收到此电子邮件后,我如何获得用户的附加详细信息,如真实姓名(如果共享),个人资料图片等?

0 个答案:

没有答案