Check whether there exists an authentication token for given auth. - Android android.accounts

Android examples for android.accounts:AccountManager

Description

Check whether there exists an authentication token for given auth.

Demo Code


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

public class Main {
    /**//from  w  w w . j a  v  a  2s . co  m
     * Check whether there exists an authentication token for given <code>auth</code>.
     *
     * @param context       A {@link Context} object
     * @param account       An {@link Account}
     * @param authTokenType The type of the token
     * @return <code>true</code> if an authentication for specified <code>auth</code> exists,
     * <code>false</code> otherwise
     */
    public static boolean isTokenValid(Context context, Account account,
            String authTokenType) {
        return getAuthToken(context, account, authTokenType) != null;
    }

    /**
     * Retrieve the authentication token for given <code>auth</code>.
     *
     * @param context       A {@link Context} object
     * @param account       An {@link Account}
     * @param authTokenType The type of the token
     * @return Authentication token for given <code>auth</code>, <code>null</code>
     * if there is no token for some reason
     */
    public static String getAuthToken(Context context, Account account,
            String authTokenType) {
        AccountManager accountManager = AccountManager.get(context);
        return accountManager.peekAuthToken(account, authTokenType);
    }
}

Related Tutorials