I am writing an android application that will start sync for all accounts added under "Account & sync" settings.I am fetching all the added accounts using the following code
AccountManager am = (AccountManager) getSystemService(Context.ACCOUNT_SERVICE);
Account[]acs = am.getAccounts();
After fetching the account I want to start sync for each account
for(Account ac:acs){
ContentResolver.requestSync(ac,authority,extras);
}
My question is how do i fetch the authority for the retrieved account ?
A far simpler answer is simply to turn off the global sync enabled flag and then turn it back on. Android starts a full sync by defualt in this case.
See ContentResolver.setMasterSyncAutomatically().
Your app will need appropriate permissions in the manifest to be allowed to change that flag.
Related
How can I programmatically remove/clear a linked account from an Android device within Android Settings > Accounts? Is this possible with ADB or Appium, or by some other programmatic method?
Android devices typically keep linked accounts for Google or Facebook on a device settings level, not within a single app's cache. I would like to remove these accounts (especially Facebook, which appears to only have one account per device).
The context for the question is in automated testing.
Removing accounts manually/by hand is not an option.
I'd prefer not to do it via Appium UI automation; even if Appium could solve this problem on one device, different android devices/OS versions have different settings UI, thus UI automation is not a scalable solution.
The best, easiest, and most scalable solution would allow me to do an ADB command which could remove the linked account.
Edit:
Here is an unanswered question on Appium forums asking a similar question: https://discuss.appium.io/t/android-how-to-remove-google-accounts-linked-a-device-on-setting-activity/6920
Linked accounts are stored in the database /data/system_ce/0/accounts_ce.db, to access it you need root access.
In the case if you have root access you can simply remove the entry of the specified account from the database.
I had the same question, and found something to remove ALL accounts.
Take a look at what AuthenticatorDescription types you are getting if you strictly only want to delete e.g. Google Accounts
private void clearAccounts() {
AccountManager manager = AccountManager.get(getApplicationContext());
AuthenticatorDescription[] authTypes = manager.getAuthenticatorTypes();
for (AuthenticatorDescription authDesc : authTypes) {
Account[] accounts = manager.getAccountsByType(authDesc.type);
if (accounts.length == 0) {
continue; // No accounts of this type, continue loop.
}
for (final Account account : accounts) {
manager.removeAccount(account, null, null, null);
}
}
}
I've been looking for a solution to this problem for a while (days, not minutes), but it eludes me quite effectively.
Please note that this is NOT a question about starting up the registration procedure. This must happen automatically without any user interaction.
I would like to add a Google account to my custom device (1000's of them). The account will mostly be used to activate Google Play store on the device so that the app can update when newer versions are available.
My existing code (the shortest snippet of those I tried):
AccountManager mgr = AccountManager.get(this);
Account acc = new Account("email#gmail.com", "com.google");
mgr.addAccountExplicitly(acc, "password", new Bundle()));
naturally yields a
java.lang.SecurityException: caller uid 10047 is different than the authenticator's uid
So how would I go about actually achieving this? My device is rooted so that's not an obstacle if it's the only way.
It is not possible to add/create a Google account using addAccountExplicitly(). You can only add accounts for your own services. even your device is rooted because it will rejected by Google web server. For more detail check this link
Warning: this solution doesn't work well. See comments for explanation.
Well, as it turns out, this is not something easily solved. I ended up registering one device, then pulled the users file from it. Location of users file : /data/system/users/0/accounts.db (if there are multiple user profiles on the device, the last directory may differ according to profile in question).
I stored this file into my app's assets (gzipped, make sure the extension is not something.gz because that gets lost during packaging - didn't bother checking out why).
First I check if my user already exists:
AccountManager mgr = AccountManager.get(this);
for (Account acc: mgr.getAccountsByType("com.google")) {
if (acc.name.equalsIgnoreCase("email#gmail.com"))
return;
}
If it does, I just skip the step. Otherwise I unpack the users file and overwrite existing one (using su). I then also do a reboot to make sure changes are registered.
I need to know 2 things :
Does google sync currently active on a device.
If yes , when was the last backup Datetime.
so far I didn't find a way to get a hand on this data , any help will be appreciated.
I don't mind if it's a solution that will work only from a particular API version.
Thanks
You should use AccountManager, filtering its results by account type (com.google), but above all by sync state, using a ContentResolver.
Check out the code attached:
AccountManager am = AccountManager.get(this);
Account[] accounts = am.getAccountsByType("com.google");
boolean syncEnabled = ContentResolver.getSyncAutomatically(accounts[0], ContactsContract.AUTHORITY);
Three methods are useful to know if sync is activated and enabled:
ContentResolver.getMasterSyncAutomatically() returns true if sync
is enabled for the whole device.
ContentResolver.getIsSyncable(yourAccount, TheProviderYouWant.AUTHORITY)) returns a value > 0 if the provider is syncable
ContentResolver.getSyncAutomatically(yourAccount, TheProviderYouWant.AUTHORITY) returns true if sync is currently enabled for this provider. But be careful to also check getMasterSyncAutomatically(), because getSyncAutomatically() can return true even if getMasterSyncAutomatically() returns false
I'm trying to find out if the user has Google-Photos (picasa) set to sync on their device. Is there any way to programmatically determine whether sync is turned on for any of the google accounts set up on the user's phone?
Also, is there any way to programmatically turn synching off for Google-Photos? If not, what is the correct Intent to launch an activity directly to the Google account's "Data & Synchronization" screen, so that the user can manual disable sync?
Thanks in advance!
EDIT:
I found some code that is useful, but what is the Authority string for "Google-Photos" (aka Picasa)???
import android.provider.ContactsContract;
AccountManager am = AccountManager.get(this);
Account[] accounts = am.getAccountsByType("com.google");
boolean syncEnabled = ContentResolver.getSyncAutomatically(accounts[0], ContactsContract.AUTHORITY);
There are two case
1) If your device is already synchronize with Google account
Then Account selector will select current login account.Refer this link
2) If your account selector does not return any account that means you are not login with any Google account. So you need to synchronize.Now in this case open your account screen and one account This will help you
There is an existing account on the phone that is used for a sync service. The account has some settings that the user entered when he created the account. Theses settings are stored as user data (--> mAccountManager.addAccountExplicitly(account, mPassword, userData)).
The user should be able to change these settings. How can this be achieved? Do I need a standalone app to change existing account data?
I guess the user would go to 'Settings'/'Accounts and sync'/'myAccount' and should find a menu entry like 'modify account data'. This menu entry should open the same activity that the user has already used to enter the data initially.
Any hints to push me in the right direction?
This fooled me for a while too - I expected to find getUserData()/setUserData() methods on the Account class, but they are on the AccountManager instead:
AccountManager am = AccountManager.get(context);
String myData = am.getUserData(account, SomeClass.MY_DATA_KEY);
myData = "Some New Value";
am.setUserData(account, SomeClass.MY_DATA_KEY, myData);
Check out the AccountManager setUserData method docs for more information.
Cheers, Andrew.