Android如何以编程方式启用/禁用自动同步

Android如何以编程方式启用/禁用自动同步,android,sync,Android,Sync,我需要知道如何以编程方式打开和关闭自动同步。我想您需要的是: ContentResolver.setSyncAutomatically(account, authority, true/false); 我想你在找我 ContentResolver.setMasterSyncAutomatically(<boolean>); ContentResolver.setMasterSyncAutomatically(); 医生说: 设置应用于所有提供程序的主自动同步设置 和账户。如果为

我需要知道如何以编程方式打开和关闭自动同步。

我想您需要的是:

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

我想你在找我

ContentResolver.setMasterSyncAutomatically(<boolean>);
ContentResolver.setMasterSyncAutomatically();
医生说:

设置应用于所有提供程序的主自动同步设置 和账户。如果为false,则每个提供程序的自动同步设置 被忽略了

此方法要求调用方保留权限 写入同步设置

因此,不要忘记将权限添加到manifest.xml中:

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

这将禁用/启用所有同步


@萨杰蒙:我更新了这个我认为非常有用的答案(我在我的个人项目中使用了这个)。

本是正确的

你需要使用

ContentResolver.setSyncAutomatically(account, authority, true/false);
您还需要添加权限“写入同步设置”


以编程方式为同步帐户编码:

同步一次:

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());
}
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);
}
自动同步时间间隔:

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());
}
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);
}
如果你想同步帐户一次,调用第一个方法,如果你想某个时间间隔同步,你必须调用第二个方法,并将秒作为参数传递给它(比如10秒)


完成

该答案的可能副本已[关闭],并且来自同一个人+1我编辑并更新了您的答案。现在我认为这一点更清楚了。是否可以从ADB执行此操作?为了能够实现切换,您还需要读取同步设置。是否有任何方法防止用户通过设置屏幕禁用同步,以便始终执行同步