Android SyncAdapter using a google account
Asked Answered
C

1

20

I have written a SyncAdapter that takes a "com.google" account and performs a sync with an appengine web service. Testing this with the dev tools sync tester (on the emulator), this sync appears to work just fine.

The problem is, it's not syncing by default. And going to the account in "accounts & sync" shows my google account to be blank - as if there's no sync services available.

I suspect that in order to get my sync shown in the "accounts & sync" menu, I'd need to implement my own AccountAuthenticator that would do exactly the same thing as what I presume google's AccountAuthenticator must already do. This is not an exciting job, and it seems very unnecessary. So:

  • is there a way to add an entry to the "accounts & sync" menu that uses my SyncAdapter but relies on a google account? or to add a sub-menu to google accounts that enables sync of my appengine service?

if not,

  • is there a way I can re-use google's AccountAuthenticator from within an AccountAuthenticator I write?
Claribelclarice answered 31/5, 2010 at 11:42 Comment(0)
C
23

So it turns out you can have a syncAdapter that uses a "com.google" account, but this requires you write a matching ContentProvider. For example, in AndroidManifest.xml:

<service android:name=".sync.SyncAdapterService" android:exported="true" android:process=":contacts">
  <intent-filter>
    <action android:name="android.content.SyncAdapter" />
  </intent-filter>
  <meta-data android:name="android.content.SyncAdapter" android:resource="@xml/syncadapter" />
</service>

and in syncadapter.xml:

<?xml version="1.0" encoding="utf-8"?>
<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android"
    android:contentAuthority="net.gfxmonk.android.pagefeed"
    android:accountType="com.google"
    android:supportsUploading="false"
    android:userVisible="true"
/>

You then must have a ContentProvider with authority "net.gfxmonk.android.pagefeed" in order for android to associate that sync action with your application. Once you have this (it doesn't even need to do anything meaningful, just exist), your program can appear inside the "accounts & Sync" setting panel - within your chosen google account.

As an additional piece of work, you may need to call:

ContentResolver.setIsSyncable(account, "net.gfxmonk.android.pagefeed", 1)

with account as the Account object you want to use for your sync credentials.

Claribelclarice answered 4/6, 2010 at 9:35 Comment(8)
where to put that setIsSyncable method under what class?Thekla
@mikedroid: I think that method can be called any time, from anywhere. I call it once on program load if I can't find any google accounts that are syncable (as indicated by ContentResolver.getIsSyncable)Claribelclarice
Can't seem to get this working. What permissions are you using? Also, your in AndroidManifest.xml: block seems to be missing?Sanjuanitasank
@Paddy: thanks for pointing out the missing XML - it was always in the answer, just never displayed because it wasn't indented enough - oops! As for the permissions, my full manifest file can be found here: github.com/gfxmonk/pagefeed-android/blob/master/src/main/… - I'm afriad it's been a while, I can't remember which is actually relevant here.Claribelclarice
@Claribelclarice Thanks for the information! My issue was a typo. Because I'm an idiot like that.Sanjuanitasank
Looks great. @Claribelclarice do you know of a way to have your app listed as a seperate account and still use com.google auth? I am using app engine user api for auth.Heinrick
@gfxmonk android:contentAuthority in syncadapter.xml is just the name of content provider connected to sync adapter, so it should be application specific (applications which don't have content provider mostly use contacts just to be able to use Authenticator framework)Sialkot
I implemented a sync like this. It works at most devices except for the Samsung SM-P600 (Galaxy Note 2014). I don't see the sync task at the Google account at tablet settings and it won't sync ever...Disbursement

© 2022 - 2024 — McMap. All rights reserved.