如何将 SyncAdapter 定义为默认不同步?

发布于 2025-01-08 03:34:57 字数 1354 浏览 3 评论 0原文

所以我在我的应用程序中实现了 SyncAdapter。由于我正在与 Google 任务同步,因此我使用用户现有的 Google 帐户。在应用程序中,用户必须进入首选项、启用同步并选择一个帐户。此时,我尝试获取 authToken,这将创建一个弹出窗口,要求用户授权我的应用程序使用他们选择的 Google 帐户。

如果用户以前从未安装过我的应用程序,那么这一切都非常有效 但是,现在假设用户卸载了该应用程序并稍后重新安装。当他们打开应用程序时,设置当然不会被选中。但他们会发现该应用程序无论如何都会与他们之前选择的 Google 帐户同步!

原因是 SyncAdapter 默认切换为同步。例如,这是“帐户和帐户”中的视图。安装后在进行任何更改之前同步屏幕:

默认情况下会选中同步。第一次安装时这并不重要,因为我的应用程序尚未获准访问他们的 Google 帐户。但在重新安装时,它确实很重要。 所以我的问题是:我需要添加/更改/删除什么才能使其默认取消选中?

我的syncadapter.xml如下:

<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android"
    android:contentAuthority="com.nononsenseapps.NotePad"
    android:accountType="com.google"
    android:supportsUploading="true"
    android:userVisible="true" />

在清单中:

<service
    android:name=".sync.SyncService"
    android:exported="true" >
    <intent-filter>
        <action android:name="android.content.SyncAdapter" />
    </intent-filter>

    <meta-data
        android:name="android.content.SyncAdapter"
        android:resource="@xml/syncadapter" />
</service>

在用户在设置中选择帐户之前,我不会在 Java 代码中进行任何有关同步的调用。

So I have a SyncAdapter implemented in my app. I use the user's existing Google account since I am syncing with Google Tasks. In the app, users have to go to preferences, enable sync and select an account. At this point I try to fetch an authToken which will create a pop-up asking the user to authorize my app to use their selected Google account.

enter image description here

This all works great if the user has never installed my app before. But, assume now that the user un-installs the app and re-installs it at a later time. When they open up the app, the settings will be unchecked of course. But what they'll find is that the app syncs with their previously selected Google account anyway!

The reason is that the SyncAdapter is toggled to sync by default. E.g. this is the view in the Accounts & Sync screen after install before any changes have been made:

It is checked to sync by default. On first install this doesn't matter because my app is not approved to access their Google account yet. But on re-install, it does matter.
So my question is: What do I need add/change/remove to make it unchecked by default?

My syncadapter.xml is as follows:

<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android"
    android:contentAuthority="com.nononsenseapps.NotePad"
    android:accountType="com.google"
    android:supportsUploading="true"
    android:userVisible="true" />

And in the manifest:

<service
    android:name=".sync.SyncService"
    android:exported="true" >
    <intent-filter>
        <action android:name="android.content.SyncAdapter" />
    </intent-filter>

    <meta-data
        android:name="android.content.SyncAdapter"
        android:resource="@xml/syncadapter" />
</service>

I do not make any calls in the Java code concerning sync until the user selects an account in settings.

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

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

发布评论

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

评论(2

甜嗑 2025-01-15 03:34:57
ContentResolver.setSyncAutomatically(account, ContactsContract.AUTHORITY, false);
ContentResolver.setSyncAutomatically(account, ContactsContract.AUTHORITY, false);
じее 2025-01-15 03:34:57

您可以存储 SharedPreference 并在 SyncService 启动时检查它。如果为 false,则停止服务并且不执行任何操作。

当您从用户配置中设置新的共享首选项值以启用同步时,请记住使用 .apply() 或 .commit() 来设置新的共享首选项值。

You can store a SharedPreference and check it on SyncService start. If false, stop the service and don't do anything.

Remember to .apply() or .commit() your new shared preference value when you set it from the user config to enable sync.

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