-
-
Save udacityandroid/e6a90828de84c8b439d9 to your computer and use it in GitHub Desktop.
getSyncAccount method in configuring periodic syncs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Helper method to get the fake account to be used with SyncAdapter, or make a new one | |
* if the fake account doesn't exist yet. If we make a new account, we call the | |
* onAccountCreated method so we can initialize things. | |
* | |
* @param context The context used to access the account service | |
* @return a fake account. | |
*/ | |
public static Account getSyncAccount(Context context) { | |
// Get an instance of the Android account manager | |
AccountManager accountManager = | |
(AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE); | |
// Create the account type and default account | |
Account newAccount = new Account( | |
context.getString(R.string.app_name), context.getString(R.string.sync_account_type)); | |
// If the password doesn't exist, the account doesn't exist | |
if ( null == accountManager.getPassword(newAccount) ) { | |
/* | |
* 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)) { | |
return null; | |
} | |
/* | |
* If you don't set android:syncable="true" in | |
* in your <provider> element in the manifest, | |
* then call ContentResolver.setIsSyncable(account, AUTHORITY, 1) | |
* here. | |
*/ | |
onAccountCreated(newAccount, context); | |
} | |
return newAccount; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment