Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

import android.content.Context;

import android.util.Log;

public class Main {
    public static final String TAG = "SYNC_UTIL_DEBUG";
    public static final String ACCOUNT_TYPE = "example.com";
    public static final String ACCOUNT_NAME = "dummyaccount";

    /**
     * Create a new dummy account for the sync adapter
     *
     * @param context The application context
     */
    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;
        }

    }
}