Android - query if a sync account is checked for syncing
How ca开发者_如何学运维n I query if a sync account is checked for syncing or not?
The user doesn't control whether account are selected for syncing -- Rather, (acount, contentAuthority) pairs are selected. For example, your gmail account can be checked to sync contacts, but not calendar events.
Here is code to check whether the first entered "com.google-type" account syncs against google contacts. (Note that "com.google" is the type of account, not the actual content of the username. You might have a google apps account with your own domain name in there)
import android.provider.ContactsContract;
AccountManager am = AccountManager.get(this);
Account[] accounts = am.getAccountsByType("com.google");
boolean syncEnabled = ContentResolver.getSyncAutomatically(accounts[0], ContactsContract.AUTHORITY);
This code will obviously fail if accounts[] is size 0 (no accounts registered) and is kind of meaningless in the presence of multiple accounts. You'll need to do some sort of reasonable selection for account. There some are other ways to get hold of an account as well.
Just becuase it's checked doesn't mean it's guaranteed to sync. There are some additional conditions involved -- global sync setting (on/off), network availability, and whether and how periodic resync is scheduled. ContentResolver is your gateway to all those queries as well.
精彩评论