Android how to enable/disable auto sync programmatically
Asked Answered
C

4

12

I need to know how to toggle auto sync on and off programmatically.

Chopstick answered 28/2, 2011 at 4:40 Comment(2)
possible duplicate of enable/disable auto sync programmatically!Rosalvarosalyn
that one is [closed] and from the same personWandawander
R
23

I think you are looking for

ContentResolver.setMasterSyncAutomatically(<boolean>);

What docs says:

Sets the master auto-sync setting that applies to all the providers and accounts. If this is false then the per-provider auto-sync setting is ignored.

This method requires the caller to hold the permission WRITE_SYNC_SETTINGS.

So don't forget to add permission into manifest.xml:

<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />

This should disable / enable all the syncs.


@Sajmon: I updated this i think very useful answer (i'm using this in my personal project).

Revisionist answered 12/1, 2012 at 16:54 Comment(3)
+1 I edited and updated your answer. Now i think that thing is clearer.Expediency
Is it possible to do it from ADB??Night
To be able to implement a toggle you also need READ_SYNC_SETTINGS.Entoil
P
6

I think what you want is the following:

ContentResolver.setSyncAutomatically(account, authority, true/false);
Perineuritis answered 11/3, 2011 at 22:47 Comment(0)
W
4

Code for Sync Accounts Programmatically:

Sync once:

public static void syncAllAccounts(Context contextAct) throws Exception {
    AccountManager manager = AccountManager.get(contextAct);
    Account[] accounts = manager.getAccountsByType("com.google");
    String accountName = "";
    String accountType = "";
    for (Account account : accounts) {
        accountName = account.name;
        accountType = account.type;
        break;
    }

    Account a = new Account(accountName, accountType);
    ContentResolver.requestSync(a, "com.android.calendar", new Bundle());
}

Sync on time interval automatically:

public static void syncAllAccountsPeriodically(Context contextAct, long seconds) throws Exception {
    AccountManager manager = AccountManager.get(contextAct);
    Account[] accounts = manager.getAccountsByType("com.google");
    String accountName = "";
    String accountType = "";
    for (Account account : accounts) {
        accountName = account.name;
        accountType = account.type;
        break;
    }

    Account a = new Account(accountName, accountType);
    ContentResolver.addPeriodicSync(a, "com.android.calendar", new Bundle(), seconds*1000);
}

If you want to sync accounts once, call first method and if you want to sync on some time of interval you have to call second method and pass seconds (Like 10 Seconds) as arguments in it.

Done

Wakefield answered 18/6, 2015 at 10:16 Comment(0)
L
0

Ben is correct.

You need to use

ContentResolver.setSyncAutomatically(account, authority, true/false);

you will also need to add permission "WRITE_SYNC_SETTINGS"

<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS"/>
Lozoya answered 18/4, 2013 at 6:39 Comment(1)
Is there any way to prevent the user to disable sync through settings screen,So that sync will always performNewsprint

© 2022 - 2024 — McMap. All rights reserved.