42

我需要从我的应用程序中通过云获取一些数据。我在 RESTful android 应用程序上观看了 google IO 视频@http ://www.youtube.com/watch?v= xHXn3Kg2IQE&t= 43m58s 它建议在最后的幻灯片中使用 SyncAdapter 与 Android 系统集成。

后来我了解到必须使用一个帐户来实现 SyncAdapter。我的应用程序不使用帐户。用户无需注册即可自由下载数据。我还能使用 SyncAdapter 吗?有没有我可以使用的股票虚拟账户?

编辑:我的应用程序确实有一个内容提供程序,所以这不是问题

Edit2:我刚刚查看了“设置”->“帐户和同步”下的“天气”和“股票”应用程序。您可以看到它们允许同步,但没有删除帐户按钮。另一方面,谷歌、Facebook 和 Skype 应用程序允许同步 PLUS,它们有一个删除帐户按钮。这意味着 Weather 和 Stock 不使用帐户,而 Google、Facebook 和 Skype 使用。

我找到的教程@ http://ericmiles.wordpress.com/2010/09/22/connecting-the-dots-with-android-syncadapter/和@ http://www.c99.org/2010/01/23 /writing-an-android-sync-provider-part-1/说必须有一个帐户才能使用同步适配器。:S ???

4

3 回答 3

18

正如 Android 开发者文档所说

即使您的应用程序不使用帐户,您仍然需要提供身份验证器组件。如果您不使用帐户或服务器登录,则验证器处理的信息将被忽略,因此您可以提供包含存根方法实现的验证器组件。您还需要提供一个绑定服务,允许同步适配器框架调用身份验证器的方法。

有一整篇关于创建存根验证器的文章。我意识到这个问题很老,很久以前就接受了答案,但我觉得官方文档中最近添加的内容应该包含在此处。

于 2013-09-05T11:06:27.900 回答
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.

于 2011-03-01T03:39:05.177 回答
12

我不断收到这个问题的很多通知,所以我想我会分享这个信息。这就是在没有帐户的情况下添加 SyncAdapter 的方式。你可以把这个放在onCreate课堂MyApplication extends Application上。这假设您已经拥有SyncAdapterContentProvider实施了。您可以按照问题中列出的教程进行操作。

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"
/>
于 2012-04-10T02:30:57.693 回答