Android多个同步适配器项目,如Google帐户?

Jam*_*win 11 android sync accountmanager android-syncadapter

我目前将Android应用设置为使用Android的AccountManager功能,使用SyncAdapter和经过身份验证的帐户自动执行同步.

我只有一个同步适配器运行,它同步所有内容,但我想将其分开,以不同的间隔执行不同内容的同步.

我如何拥有像Google这样的多个同步项?

Google帐户截图

rds*_*rds 24

您只需使用相同的帐户类型定义多个同步适配器.

清单包含:

<service android:exported="true" android:name="com.example.FooSyncAdapterService">
  <intent-filter>
    <action android:name="android.content.SyncAdapter" />
  </intent-filter>
  <meta-data android:name="android.content.SyncAdapter" android:resource="@xml/syncadapter_foo" />
</service>
<service android:exported="true" android:name="com.example.BarSyncAdapterService">
  <intent-filter>
    <action android:name="android.content.SyncAdapter" />
  </intent-filter>
  <meta-data android:name="android.content.SyncAdapter" android:resource="@xml/syncadapter_bar" />
</service>
Run Code Online (Sandbox Code Playgroud)

而且syncdataper_foo

<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android"
    android:contentAuthority="foo"
    android:accountType="com.example"
    android:allowParallelSyncs="true" />
Run Code Online (Sandbox Code Playgroud)

而且syncdataper_bar

<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android"
    android:contentAuthority="bar"
    android:accountType="com.example"
    android:allowParallelSyncs="true" />
Run Code Online (Sandbox Code Playgroud)

请注意,对于帐户类型"com.google",同步适配器甚至由不同的应用程序(云端硬盘,文档,表格,Gmail,日历等)提供.

  • 谢谢,我之前尝试过,但是你的回答让我注意到每个SyncAdapter都有不同的contentAuthority,这向我展示了问题 - 必须有多个ContentProviders,每个都注册到SyncAdapter xml文件中指定的contentAuthority.非常感谢! (2认同)