Check whether an Account with given accountName and accountType exists. - Android Account

Android examples for Account:Account Name

Description

Check whether an Account with given accountName and accountType exists.

Demo Code


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

public class Main {

    public static boolean accountExists(Context context,
            String accountName, String accountType) {
        return getAccount(context, accountName, accountType) != null;
    }/*from   www. j  ava  2s .c  om*/
    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;
            }
        }
        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