Example usage for android.accounts AccountManager addAccountExplicitly

List of usage examples for android.accounts AccountManager addAccountExplicitly

Introduction

In this page you can find the example usage for android.accounts AccountManager addAccountExplicitly.

Prototype

public boolean addAccountExplicitly(Account account, String password, Bundle userdata) 

Source Link

Document

Adds an account directly to the AccountManager.

Usage

From source file:saschpe.birthdays.helper.AccountHelper.java

public static Bundle addAccount(Context context) {
    Log.d(TAG, "AccountHelper.addAccount: Adding account...");

    final Account account = new Account(context.getString(R.string.app_name),
            context.getString(R.string.account_type));
    AccountManager manager = AccountManager.get(context);

    if (manager.addAccountExplicitly(account, null, null)) {
        // Enable automatic sync once per day
        ContentResolver.setSyncAutomatically(account, context.getString(R.string.content_authority), true);
        ContentResolver.setIsSyncable(account, context.getString(R.string.content_authority), 1);

        // Add periodic sync interval based on user preference
        final long freq = PreferencesHelper.getPeriodicSyncFrequency(context);
        ContentResolver.addPeriodicSync(account, context.getString(R.string.content_authority), new Bundle(),
                freq);//from   ww  w .j  av  a2s  . co  m

        Bundle result = new Bundle();
        result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
        result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
        Log.i(TAG, "Account added: " + account.name);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            manager.notifyAccountAuthenticated(account);
        }
        return result;
    } else {
        Log.e(TAG, "Adding account explicitly failed!");
        return null;
    }
}

From source file:Main.java

public static void ensureUserIsLoggedIn(Context context, String accountType) {
    AccountManager manager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
    Account account = new Account("username", accountType);
    String password = "testpassword";
    manager.addAccountExplicitly(account, password, null);
}

From source file:Main.java

/**
 * Create a new dummy account for the sync adapter
 *
 * @param context The application context
 *//*from w  w  w  . java 2 s .co  m*/
public static android.accounts.Account CreateSyncAccount(Context context, Account newAccount) {
    // Get an instance of the Android account manager
    AccountManager accountManager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
    /*
     * Add the account and account type, no password or user data
     * If successful, return the Account object, otherwise report an error.
     */
    if (accountManager.addAccountExplicitly(newAccount, null, null)) {
        /*
         * If you don't set android:syncable="true" in
         * in your <provider> element in the manifest,
         * then call context.setIsSyncable(account, AUTHORITY, 1)
         * here.
         */
    } else {
        /*
         * The account exists or some other error occurred. Log this, report it,
         * or handle it internally.
         */
    }
    return newAccount;
}

From source file:Main.java

/**
 * Create a new dummy account for the sync adapter
 *
 * @param context The application context
 *//*from w  w  w.  ja  v a 2  s .  c o m*/
public static Account CreateSyncAccount(Context context) {

    // Create the account type and default account
    Account newAccount = new Account(ACCOUNT_NAME, ACCOUNT_TYPE);
    // Get an instance of the Android account manager
    AccountManager accountManager = (AccountManager) context.getSystemService(context.ACCOUNT_SERVICE);

    /*
     * Add the account and account type, no password or user data
     * If successful, return the Account object, otherwise report an error.
     */
    if (accountManager.addAccountExplicitly(newAccount, null, null)) {
        Log.d(TAG, "Account created: " + newAccount.name);

        return newAccount;
    } else {
        /*
         * The account exists or some other error occurred.
         * Try and get account first. Then log or report the error and return null.
         */
        Account[] accounts = accountManager.getAccounts();
        for (Account account : accounts) {
            if (account.name.equals(ACCOUNT_NAME)) {
                Log.d(TAG, "Account exists: " + account.name);
                return account;
            }
        }
        Log.d(TAG, "Error occured. The account is null");
        return null;
    }

}

From source file:org.enbyted.android.zseinfo.view.activity.MainActivity.java

public static Account createSyncAccount(Context context) {
    Account newAccount = new Account(ACCOUNT, ACCOUNT_TYPE);
    AccountManager accountManager = (AccountManager) context.getSystemService(ACCOUNT_SERVICE);

    if (accountManager.addAccountExplicitly(newAccount, null, null)) {
        ContentResolver.setSyncAutomatically(newAccount, AUTHORITY, true);
        ContentResolver.setIsSyncable(newAccount, AUTHORITY, 1);
    } else {//from   w w  w  .j  a  v  a 2s  . c o m

    }
    return newAccount;
}

From source file:edu.mit.mobile.android.locast.accounts.Authenticator.java

/**
 * Adds an account that acts as a placeholder to allow the sync to work properly. The sync
 * framework always requires an account.
 *
 * @param context//from  w w w. ja  v a  2s. c o m
 */
public static void addDemoAccount(Context context) {
    final Account[] accounts = Authenticator.getAccounts(context);

    if (accounts.length > 0) {
        // if there's already a demo account, we don't want to add another.
        // if there isn't, we're not going to delete the actual account.
        return;
    }
    final AccountManager am = AccountManager.get(context);
    final Account account = new Account(DEMO_ACCOUNT, AuthenticationService.ACCOUNT_TYPE);
    am.addAccountExplicitly(account, null, null);

    ContentResolver.setSyncAutomatically(account, MediaProvider.AUTHORITY, true);
}

From source file:com.example.igorklimov.popularmoviesdemo.sync.SyncAdapter.java

/**
 * Create a new dummy account for the sync adapter
 *
 * @param context The application context
 *//*from   ww  w. ja va 2 s  .  c  om*/
public static Account getSyncAccount(Context context) {
    Account newAccount = new Account(context.getString(R.string.app_name),
            context.getString(R.string.acc_sync_type));
    AccountManager accountManager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);

    if (accountManager.getPassword(newAccount) == null) {
        if (!accountManager.addAccountExplicitly(newAccount, "", null))
            return null;

        onAccountCreated(newAccount, context);
    }

    return newAccount;
}

From source file:Main.java

/**
 * Sets password of account, creates a new account if necessary.
 *//*w w  w.  j  a  v  a2 s.  c o m*/
private static Account findOrCreateAccount(AccountManager accountManager, String username, String refreshToken,
        String accountType) {

    for (Account account : accountManager.getAccountsByType(accountType)) {
        if (account.name.equals(username)) {
            accountManager.setPassword(account, refreshToken);
            return account;
        }
    }

    Account account = new Account(username, accountType);
    accountManager.addAccountExplicitly(account, refreshToken, null);
    return account;
}

From source file:com.stoneapp.ourvlemoodle2.activities.MainActivity.java

/**
 * Create an entry for this application in the system account list, if it isn't already there.
 *
 * @param context Context/*from  w ww . j  a  v  a  2 s. c  o  m*/
 */
public static void CreateSyncAccount(Context context) {
    final String AUTHORITY = BuildConfig.APPLICATION_ID + ".provider";
    // Value below must match the account type specified in res/xml/syncadapter.xml
    final String ACCOUNT_TYPE = BuildConfig.APPLICATION_ID;
    // hours in seconds
    final long SYNC_INTERVAL = SettingsUtils.getSyncInterval(context) * 60L * 60L;

    List<MoodleSiteInfo> sites = MoodleSiteInfo.listAll(MoodleSiteInfo.class);
    String accountName = sites.get(0).getUsername();

    if (TextUtils.isEmpty(accountName))
        accountName = "OurVLE User";

    // Create account, if it's missing. (Either first run, or user has deleted account.)
    Account account = new Account(accountName, ACCOUNT_TYPE);

    AccountManager accountManager = (AccountManager) context.getSystemService(ACCOUNT_SERVICE);

    /*
     * Add the account and account type, no password or user data
     * If successful, return the Account object, otherwise report an error.
     */
    if (accountManager.addAccountExplicitly(account, null, null)) {
        // Inform the system that this account is eligible for auto sync when the network is up
        ContentResolver.setSyncAutomatically(account, AUTHORITY, true);
        // Recommend a schedule for automatic synchronization. The system may modify this based
        // on other scheduled syncs and network utilization.
        ContentResolver.addPeriodicSync(account, AUTHORITY, Bundle.EMPTY, SYNC_INTERVAL);
    }
}

From source file:inforuh.eventfinder.sync.SyncAdapter.java

public static Account getAccount(Context context) {
    AccountManager accountManager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);

    Account account = new Account(context.getString(R.string.app_name),
            context.getString(R.string.account_type));

    if (accountManager.getPassword(account) == null) {
        accountManager.addAccountExplicitly(account, "", null);
        onAccountCreated(account, context);
    }/*from   ww  w  .  j a  va  2s .co m*/
    return account;
}