Retrieve the Account object for given accountName and accountType. - Android Account

Android examples for Account:Account Name

Description

Retrieve the Account object for given accountName and accountType.

Demo Code


//package com.java2s;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;

public class Main {

    public static Account getAccount(Context context, String accountName,
            String accountType) {
        AccountManager accountManager = AccountManager.get(context);
        Account[] accounts = accountManager.getAccountsByType(accountType);
        for (Account account : accounts) {
            if (account.name.equals(accountName)) {
                return account;
            }//  www .  ja v a  2 s .c  o m
        }
        return null;
    }
    public static Account getAccount(Context context, String accountType) {
        AccountManager accountManager = AccountManager.get(context);
        Account[] accounts = accountManager.getAccountsByType(accountType);
        if (accounts.length > 0) {
            return accounts[0];
        }
        return null;
    }
}

Related Tutorials