Android multiple sync adapter items like Google Account?

一笑奈何 提交于 2019-12-30 01:08:29

问题


I currently have my Android app set up to use the AccountManager feature of Android, using a SyncAdapter and an authenticated account to performs syncs automatically.

I only have 1 sync adapter running which syncs all content, but I would like to separate this out to performs syncs for different content at different intervals.

How can I have multiple sync items like Google does?


回答1:


You just have to define several sync adapters, using the same account type.

The manifest contains:

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

And syncdataper_foo is

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

And syncdataper_bar is

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

Note that in the case of the account type "com.google", the sync adapters are even provided by different applications (Drive, Docs, Sheets, Gmail, Calendar, etc.).




回答2:


I would like to just add up the main attribute which makes the sync type visible under accounts menu to the above answer.

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


来源:https://stackoverflow.com/questions/30998063/android-multiple-sync-adapter-items-like-google-account

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!