How to check for sync settings in android

2020-06-23 08:32发布

问题:

I am building an android app for which I need to check the sync setting of each individual account registered in the device. I know that I can do it through ContentResolver class but I am having some problem with it. I've managed to get the list of all accounts on the device but I don't know where to get the relevant authority of a specific account at run time. Below is the code:

AccountManager acm = AccountManager.get(getApplicationContext());
    Account[] acct = acm.getAccounts();
    for(int i=0;i<acct.length;i++){
        int p = ContentResolver.getIsSyncable(acct[i], null);
        Log.i(TAG,"account name is"+acct[i].name);
        Log.i(TAG,"answer to syncable is: "+String.valueOf(p));

The getIsSyncable(Account am,String authority) asks for an account and an authority. As you can see I am passing in NULL instead of the actual authority. Does anyone know of a way I can find the authority to the relevant account?

回答1:

You can retrieve the known SyncAdapters and then query the ContentResolver

AccountManager acm
        = AccountManager.get(getApplicationContext());
    Account[] acct = null;

    SyncAdapterType[] types = ContentResolver.getSyncAdapterTypes();
    for (SyncAdapterType type : types) {
      Log.d(TAG, "--------------------");
      Log.d(TAG, type.authority + "--" + type.accountType);
      acct = acm.getAccountsByType(type.accountType);
      for (int i = 0; i < acct.length; i++) {
        int p = ContentResolver.getIsSyncable(acct[i], type.authority);
        Log.i(TAG, "account name: " + acct[i].name);
        Log.i(TAG, "syncable: " + String.valueOf(p));
      }
    }

Output:

11-15 17:12:51.899: DEBUG/syncsample(4572): com.google.android.music.MusicContent--com.google 11-15 17:12:51.899:

INFO/syncsample(4572): account name: xxxxxxxxx@gmail.com 11-15 17:12:51.899: INFO/syncsample(4572): syncable: 1 11-15 17:12:51.899:

INFO/syncsample(4572): account name: xxxxxx@google.com 11-15 17:12:51.899: INFO/syncsample(4572): syncable: 0



回答2:

As there are so many settings in particular account so you have to give the authority of each individual account's specific component.

Like for Contacts you can give like ContactsContract.AUTHORITY which is nothing but com.android.contacts so for other component you can find it from the package name and give it.



标签: android sync