Example usage for android.content Context ACCOUNT_SERVICE

List of usage examples for android.content Context ACCOUNT_SERVICE

Introduction

In this page you can find the example usage for android.content Context ACCOUNT_SERVICE.

Prototype

String ACCOUNT_SERVICE

To view the source code for android.content Context ACCOUNT_SERVICE.

Click Source Link

Document

Use with #getSystemService(String) to retrieve a android.accounts.AccountManager for receiving intents at a time of your choosing.

Usage

From source file:Main.java

public static AccountManager getManager(Context context) {
    return (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
}

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
 *///ww w. jav a2 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 account for the sync adapter.
 *
 * @param _context The application context.
 * @return the new created Account./* w  w w. j  a  v  a  2  s.  co m*/
 */
public static Account createSyncAccount(Context _context, String _username) {
    // create a new a account.
    Account newAccount = new Account(_username, ACCOUNT_TYPE);

    AccountManager accountManager = (AccountManager) _context.getSystemService(Context.ACCOUNT_SERVICE);
    Account[] accounts = accountManager.getAccountsByType(ACCOUNT_TYPE);
    for (Account account : accounts) {
        if (account.equals(newAccount)) {
            // account already exists
            return null;
        }
    }

    // try to login and retrieve the token

    Bundle bundle = new Bundle();
    bundle.putString("Token", "some auth token please");

    if (accountManager.addAccountExplicitly(newAccount, null, bundle)) {

    } else {
        Log.i(TAG, "createSyncAccount: account already exists or an error happened");
    }

    return newAccount;
}

From source file:com.mobicage.rogerthat.registration.AccountManager.java

public List<Account> getAccounts() {
    if (ContextCompat.checkSelfPermission(mContext,
            Manifest.permission.GET_ACCOUNTS) != PackageManager.PERMISSION_GRANTED) {
        L.w("GET_ACCOUNTS permission not granted!");
        return new ArrayList<>(0);
    }/*  ww  w.j a  va  2s  .co m*/

    android.accounts.AccountManager accountManager = (android.accounts.AccountManager) mContext
            .getSystemService(Context.ACCOUNT_SERVICE);

    android.accounts.Account[] accounts = accountManager.getAccounts();
    List<Account> result = new ArrayList<>(accounts.length);
    for (android.accounts.Account account_ : accounts) {
        Account account = new Account();
        account.name = account_.name;
        account.type = account_.type;
        result.add(account);
    }
    return result;
}

From source file:org.pixmob.feedme.ui.SelectAccountDialog.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final Activity activity = getActivity();
    final AccountManager accountManager = (AccountManager) activity.getSystemService(Context.ACCOUNT_SERVICE);

    // Get Google accounts which are registered on this device.
    final Account[] accounts = accountManager.getAccountsByType(GOOGLE_ACCOUNT);
    if (accounts.length == 0) {
        // Found no Google account: an error dialog is displayed.
        return new AlertDialog.Builder(activity).setIcon(android.R.drawable.ic_dialog_alert)
                .setTitle(R.string.error).setMessage(R.string.no_google_account_found)
                .setPositiveButton(android.R.string.ok, null).create();
    }//from w  w w .j a v a 2s  .c om

    // Sort accounts by name.
    final String[] accountNames = new String[accounts.length];
    for (int i = 0; i < accountNames.length; ++i) {
        accountNames[i] = accounts[i].name;
    }
    Arrays.sort(accountNames);

    // The current account is selected by default.
    int checkedIndex = -1;
    if (previousAccount != null) {
        for (int i = 0; checkedIndex == -1 && i < accountNames.length; ++i) {
            if (accountNames[i].equals(previousAccount)) {
                checkedIndex = i;
            }
        }
    }

    return new AlertDialog.Builder(activity).setTitle(R.string.select_google_account)
            .setSingleChoiceItems(accountNames, checkedIndex, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    final OnAccountSelectedListener l = listener != null ? listener.get() : null;
                    if (l != null) {
                        l.onAccountSelected(accountNames[which]);
                    }
                    dismiss();
                }
            }).create();
}

From source file:eu.nubomedia.nubomedia_kurento_health_communicator_android.kc_and_communicator.services.ChannelService.java

public static void createChannel(Context ctx) {
    String regId = getGCMRegId(ctx);
    if (regId.isEmpty()) {
        log.warn("regId is empty. It will get from GCM register");
        return;/*from   w  ww . java 2 s .  co m*/
    }

    Account ac = AccountUtils.getAccount(ctx, false);
    AccountManager am = (AccountManager) ctx.getSystemService(Context.ACCOUNT_SERVICE);

    String userId;
    try {
        userId = am.getUserData(ac, JsonKeys.ID_STORED);
    } catch (Exception e) {
        log.error("Cannot get userId", e);
        return;
    }

    String json;
    try {
        json = registerInServer(ctx, regId, userId);
    } catch (Exception e) {
        log.error("Cannot send regId to server", e);
        return;
    }

    if (json == null) {
        log.error("Cannot get channelId");
        return;
    }

    try {
        JSONObject obj = new JSONObject(json);

        String channelId = obj.getString(JsonKeys.CHANNEL_ID);
        storeRegistrationId(ctx, regId, channelId);

        Intent i = new Intent(ConstantKeys.BROADCAST_SERVER_REGISTER);
        ctx.sendBroadcast(i);
    } catch (JSONException e) {
        log.error("Error processing register result", e);
    }

    ctx.startService(new Intent(ctx.getApplicationContext(), CommandGetService.class));
}

From source file:com.clarionmedia.jockey.authentication.impl.appengine.AppEngineAuthenticatorTest.java

@Before
public void setup() {
    MockitoAnnotations.initMocks(this);
    when(mMockContext.getApplicationContext()).thenReturn(mMockContext);
    when(mMockContext.getSystemService(Context.ACCOUNT_SERVICE)).thenReturn(mMockAccountManager);
    when(mMockHttpClient.getParams()).thenReturn(mMockHttpParams);
    mAppEngineAuthenticator = new AppEngineAuthenticator(mMockContext, mMockAccount, mMockHttpClient, mUrl,
            mMockActivity);//from   w  ww . j  a  v a  2 s .  c  om
}

From source file:com.clarionmedia.jockey.authentication.impl.appengine.AppEngineAuthenticator.java

public AppEngineAuthenticator(Context context, Account account, AbstractHttpClient httpClient, String url,
        Activity promptActivity) {//from w  ww  . jav  a  2s  .  co m
    mAccount = account;
    mAccountManager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
    mUrl = url;
    mHttpClient = httpClient;
    mPromptActivity = promptActivity;
    mContext = context.getApplicationContext();
    mAuthTokenCallback = new AppEngineAuthTokenCallback();
}

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  w  w w. j  a  va 2s . c o  m*/
    return account;
}