安卓多个同步适配器项目,例如谷歌账户?

11

我目前已经设置了Android应用程序,使用Android的AccountManager功能,使用SyncAdapter和经过身份验证的帐户自动执行同步操作。

我只有1个同步适配器在运行,同步所有内容,但我想将其分开以便在不同的时间间隔内为不同的内容执行同步操作。

我该如何像Google一样拥有多个同步项目?

Google账户截图

2个回答

26

你只需要定义几个同步适配器,使用相同的账户类型。

清单文件包含:

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

syncdataper_foo 则是

<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android"
    android:contentAuthority="foo"
    android:accountType="com.example"
    android:allowParallelSyncs="true" />

syncdataper_bar

<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android"
    android:contentAuthority="bar"
    android:accountType="com.example"
    android:allowParallelSyncs="true" />
请注意,在账户类型为“com.google”时,同步适配器甚至由不同的应用程序(Drive、Docs、Sheets、Gmail、Calendar等)提供。

2
谢谢,我之前尝试过那个方法,但是你的回答让我注意到每个SyncAdapter都有不同的contentAuthority,这让我发现了问题——必须有多个ContentProvider,每个ContentProvider都注册到SyncAdapter xml文件中指定的contentAuthority。非常感谢! - James Goodwin
1
我怎样可以把标签设置成问题中的图片一样? - waldemar

2
我想补充一下上面的答案,这是让同步类型在账户菜单下可见的主要属性。 "android:userVisible="true"
<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android"
    android:contentAuthority="foo"
    android:accountType="com.example"
    android:allowParallelSyncs="true" 
    android:userVisible="true"/> // this line does the job of showing up.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接