create Account - Android Account

Android examples for Account:Account Information

Description

create Account

Demo Code


import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AccountManagerCallback;
import android.accounts.AccountManagerFuture;
import android.accounts.AuthenticatorException;
import android.accounts.OperationCanceledException;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.TextView;
import java.io.IOException;

public class Main{
    public static final String ACCOUNT = "uTestAccount";
    public static final String PASSWORD = "Very secret word";
    public static void createAccount(Context context) {
        Account newAccount = new Account(ACCOUNT, getType(context));
        AccountManager accountManager = getManager(context);

        if (accountManager.addAccountExplicitly(newAccount, PASSWORD, null)) {
        } else {//w w  w.j  a v  a 2s.c  om
            accountManager.setPassword(newAccount, PASSWORD);
        }
    }
    public static String getType(Context context) {
        return context.getString(R.string.account_type);
    }
    public static AccountManager getManager(Context context) {
        return (AccountManager) context
                .getSystemService(Context.ACCOUNT_SERVICE);
    }
}

Related Tutorials