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

发布于 2024-10-19 06:36:53 字数 29 浏览 1 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

几度春秋 2024-10-26 06:36:53

我认为您正在寻找

ContentResolver.setMasterSyncAutomatically(<boolean>);

文档所说的内容:

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

该方法需要调用者持有权限
WRITE_SYNC_SETTINGS。

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

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

这应该禁用/启用所有同步。


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

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).

不必了 2024-10-26 06:36:53

我认为你想要的是以下内容:

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

I think what you want is the following:

ContentResolver.setSyncAutomatically(account, authority, true/false);
辞取 2024-10-26 06:36:53

以编程方式同步帐户的代码:

同步一次:

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);
}

如果您想要同步帐户< strong>once,调用first方法,如果你想同步某个时间间隔,你必须调用 Second 方法并传递(如 10 秒)作为其中的参数

完成

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

转瞬即逝 2024-10-26 06:36:53

本是对的。

您需要使用

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

您还需要添加权限“WRITE_SYNC_SETTINGS”

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

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"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文