Check if Sync is Enabled within Android App
Asked Answered
O

3

10

Is there a way to check, programmatically within my Android app, whether a particular setting under Settings > Accounts and Sync > Data & Synchronization is enabled or not?

Is there a way to check if the general sync settings are enabled?

Thanks!


If it helps to know "why," I'm currently rolling my own sync functionality (not using SyncAdapter). However, if possible I'd like to have my sync service listed under Data & Synchronization. Right now I'm planning to hack a dummy sync service that does nothing and have my app's sync service query whether or not the dummy sync service is enabled. That will tell me whether to sync or not.

Opinionative answered 28/6, 2012 at 21:5 Comment(0)
A
11

You can check whether sync is enable or not with the help of below code and this Document

AccountManager am = AccountManager.get(YourActivity.this);
Account account = am.getAccountsByType(Const.ACCOUNT_TYPE)[0];
  if(ContentResolver.isSyncActive(account, DataProvider.AUTHORITY){
     // sync is enable
  }

You can also set enable/disable programmatically with the help of this ContentResolver.setSyncAutomatically and ContentResolver.setMasterSyncAutomatically

Update :

isSyncActive returns true if there is currently a sync operation for the given account or authority in the pending list, or actively being processed.

Artie answered 28/6, 2012 at 21:26 Comment(4)
Nice. Is there a way that I can "listen" to when the setting is changed in the Data & Synchronization menu? (Maybe there's some kind of broadcast I can receive?)Opinionative
The documentation of "isSyncActive" says: "Returns true if there is currently a sync operation for the given account or authority in the pending list, or actively being processed." --- So as soon as the sync operation for a given account is over, it will return false even though syncing is enabled for that account.Vainglorious
as @NobuGames pointed out, this is not the answer to the question.Dampproof
Where does Const.ACCOUNT_TYPE come from? and where does DataProvider.AUTHORITY come from?Attendant
H
26

To know if a sync is enabled (and not active as rajpara's answer do), use this:

AccountManager am = AccountManager.get(YourActivity.this);
Account account = am.getAccountsByType(YOUR_ACCOUNT_TYPE)[0];
boolean isYourAccountSyncEnabled = ContentResolver.getSyncAutomatically(account, DataProvider.AUTHORITY);
boolean isMasterSyncEnabled = ContentResolver.getMasterSyncAutomatically();

The "master" sync status is the global sync switch the user can use to disable all sync on his phone. If the master sync is off, your account won't sync, even if your account sync status tells that it's enabled.

As @HiB mentionned, the android.permission.READ_SYNC_SETTINGS permission is needed to access the sync status. android.permission.WRITE_SYNC_SETTINGS is needed to enable/disable it.

You also need android.permission.GET_ACCOUNTS to get the accounts, as MeetM mentionned.

Heartburning answered 20/11, 2013 at 14:31 Comment(6)
android.permission.READ_SYNC_SETTINGS and android.permission.WRITE_SYNC_SETTINGS neededMikelmikell
Are you sure about the WRITE_SYNC_SETTINGS one?Heartburning
for writing the value... so you are right here is not necessary to mentionMikelmikell
Also android.permission.GET_ACCOUNTS is required. Although its obvious, but for anyone like me :)Threonine
The android documentation seems to be completely misleading here as it only talks about a network tickle developer.android.com/reference/android/content/…, java.lang.String)Dampproof
Where does YOUR_ACCOUNT_TYPE come from? and where does DataProvider.AUTHORITY come from?Attendant
A
11

You can check whether sync is enable or not with the help of below code and this Document

AccountManager am = AccountManager.get(YourActivity.this);
Account account = am.getAccountsByType(Const.ACCOUNT_TYPE)[0];
  if(ContentResolver.isSyncActive(account, DataProvider.AUTHORITY){
     // sync is enable
  }

You can also set enable/disable programmatically with the help of this ContentResolver.setSyncAutomatically and ContentResolver.setMasterSyncAutomatically

Update :

isSyncActive returns true if there is currently a sync operation for the given account or authority in the pending list, or actively being processed.

Artie answered 28/6, 2012 at 21:26 Comment(4)
Nice. Is there a way that I can "listen" to when the setting is changed in the Data & Synchronization menu? (Maybe there's some kind of broadcast I can receive?)Opinionative
The documentation of "isSyncActive" says: "Returns true if there is currently a sync operation for the given account or authority in the pending list, or actively being processed." --- So as soon as the sync operation for a given account is over, it will return false even though syncing is enabled for that account.Vainglorious
as @NobuGames pointed out, this is not the answer to the question.Dampproof
Where does Const.ACCOUNT_TYPE come from? and where does DataProvider.AUTHORITY come from?Attendant
L
3
boolean isEnabled = ContentResolver.getSyncAutomatically(account, MyProvider);
if(isEnabled)
{
...do something
}

Works for me

Linnell answered 6/7, 2015 at 10:38 Comment(2)
Where does account and MyProvider come from?Attendant
AccountManager am = AccountManager.get(YourActivity.this); Account account = am.getAccountsByType(Const.ACCOUNT_TYPE)[0]; Provider you must be created by yourselfLinnell

© 2022 - 2024 — McMap. All rights reserved.