42

I need to fetch some data over the cloud from my app. I've watched the google IO video on RESTful android apps @ http://www.youtube.com/watch?v=xHXn3Kg2IQE&t=43m58s It recommends in the final slides to use a SyncAdapter to integrate with the Android System.

Later I learned that one has to use an Account to implement SyncAdapter. My app does not use an account. Users can freely download data without registration. Can I still use SyncAdapter? Is there a stock dummy account that I could use?

Edit: I do have a content provider for my app so that's not a problem

Edit2: I've just looked at the Weather and Stock apps under Settings -> Accounts & Sync. You can see that they allow syncing, but don't have a remove account button. On the other hand, Google, Facebook and Skype apps allow syncing PLUS they have a remove account button. This means Weather and Stock don't use accounts, whereas Google, Facebook and Skype do.

The tutorials I found @ http://ericmiles.wordpress.com/2010/09/22/connecting-the-dots-with-android-syncadapter/ and @ http://www.c99.org/2010/01/23/writing-an-android-sync-provider-part-1/ say that one MUST have an account to use Sync Adapter. :S ???

siamii
  • 23,374
  • 28
  • 93
  • 143
  • See also [Should I use android AccountManager](http://stackoverflow.com/a/8614699/94363) – rds Dec 23 '11 at 10:40
  • can any one tell me the solution for the following link: [enter link description here][1] [1]: http://stackoverflow.com/questions/29326366/android-updating-contact-details-on-a-webserver-via-user-login – CoderX Mar 31 '15 at 04:13

3 Answers3

18

As the Android Developer docs say

Even if your app doesn't use accounts, you still need to provide an authenticator component. If you don't use accounts or server login, the information handled by the authenticator is ignored, so you can provide an authenticator component that contains stub method implementations. You also need to provide a bound Service that allows the sync adapter framework to call the authenticator's methods.

There is an entire article on Creating a Stub Authenticator. I realise that this question is old and an answer was accepted long ago, but I felt that a recent addition to the official docs should be included here.

nindalf
  • 1,058
  • 2
  • 12
  • 15
12

I keep getting lots of notifications from this question, so I thought I'll share this info. This is how you add SyncAdapter without Account. You can put this in onCreate of MyApplication extends Application class. This assumes you already have a SyncAdapter and ContentProvider implemented. You can do that by following the tutorials listed in the question.

final String ACCOUNT_NAME = "MyApp";
final String ACCOUNT_TYPE = "com.myapp.account";
final String PROVIDER = "com.myapp.provider";

Account appAccount = new Account(ACCOUNT_NAME,ACCOUNT_TYPE);
AccountManager accountManager = AccountManager.get(getApplicationContext());
if (accountManager.addAccountExplicitly(appAccount, null, null)) {
   ContentResolver.setIsSyncable(appAccount, PROVIDER, 1);
   ContentResolver.setMasterSyncAutomatically(true);
   ContentResolver.setSyncAutomatically(appAccount, PROVIDER, true);
}

res/xml/syncadapter.xml

<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android"
    android:contentAuthority="@string/provider"
    android:accountType="@string/account_type"  
    android:userVisible="true"  
    android:supportsUploading="true"
/>

res/xml/authenticator.xml

<account-authenticator xmlns:android="http://schemas.android.com/apk/res/android"
    android:accountType="@string/account_type"
    android:icon="@drawable/app_icon"
    android:smallIcon="@drawable/app_icon"
    android:label="@string/app_label"
/>
niqueco
  • 2,261
  • 19
  • 39
siamii
  • 23,374
  • 28
  • 93
  • 143
  • 3
    calling `setMasterSyncAutomatically(true)` here is bad practice, in my opinion... – Alex Lockwood Aug 12 '12 at 03:41
  • 3
    Not only is it bad practice, but it requires android.permission.WRITE_SYNC_SETTINGS. You shouldn't be overriding your users' choice to disable sync. And especially not the master setting. – Mark Renouf Oct 29 '12 at 09:59
  • Where does `authenticator.xml` get used? Don't you need to subclass `AbstractAccountAuthenticator` as well? – gnuf Jul 07 '13 at 12:15
  • 1
    This code doesn't solve any problem and additionally it will show account in account list `android:userVisible=true` – mente Oct 17 '13 at 12:36
  • this also requires `android.permission.AUTHENTICATE_ACCOUNTS`, as usual. i'm looking for a way to run a SyncAdapter without that permission, but i haven't yet found one. – ryan Jan 23 '14 at 18:05
  • 1
    The above approaches yet list the account in ````Settings -> Accounts```` . Is there anyway to avoid that ? – Kamalakannan J Jul 01 '15 at 06:49
12

I have created a contact sync adapter where I don't have a account authorization and or configuration screens. It wasn't that hard. I don't think having to deal with the Android Account stuff was that much of a deal.

Quote from your tutorial link:

The bad news is that there is no “stock” functionality to give you an easy way to provide an Account to the system. However, in the same Sync Adapter Example that comes with the SDK there is a lot of code you can borrow to give you Account functionality. Unless you desire a custom credentials screen, you can heist all the code in the com.example.android.samplesync.authenticator package with only a few minor changes.

So it's basically just a copy and paste from the example, that's pretty much what I did and it worked fine.

I don't know for sure but all the adapters that don't have "Remove Account" seems to be built-in ROM adapters on all the devices I've looked at. I'm not sure you have to worried about it.

Shane Powell
  • 13,698
  • 2
  • 49
  • 61
  • thanks for your answer, Shane. I've managed to add the account explicitly by commenting out the Login Activity intent. It seems to work now. I've also done some debugging and the Weather and Stock apps are indeed built-in and cannot be integrated like that to AccountManager through the SDK. – siamii Mar 01 '11 at 19:12
  • 7
    Can you please post your code to sync the contact without account ? – Megha May 08 '14 at 10:43
  • can you please provide example – Bhoomi Zalavadiya Jan 13 '18 at 11:19