Android开发者syncadapter教程是否已过时。
因为我试图在我的应用程序中实现同步适配器,将数据同步到服务器。但是在实现以下部分时,我会遇到日食错误。
实现在网络可用时同步的syncadapter:
Android开发者教程:
public class MainActivity extends FragmentActivity {
...
// Constants
// Content provider authority
public static final String AUTHORITY = "com.example.android.datasync.provider";
// Account
public static final String ACCOUNT = "default_account";
// Global variables
// A content resolver for accessing the provider
ContentResolver mResolver;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
// Get the content resolver for your app
mResolver = getContentResolver();
// Turn on automatic syncing for the default account and authority
mResolver.setSyncAutomatically(ACCOUNT, AUTHORITY, true);
...
}
...
}
我的代码中发生了什么: 有以下变量
// Constants
// The authority for the sync adapter's content provider
public static final String AUTHORITY = "com.example.android.datasync.provider";
// An account type, in the form of a domain name
public static final String ACCOUNT_TYPE = "example.com";
// The account name
public static final String ACCOUNT = "dummyaccount";
// Instance fields
Account mAccount;
// Global variables
// A content resolver for accessing the provider
ContentResolver mResolver;
并在oncreate中执行此操作:
mAccount = CreateSyncAccount(this);
// Get the content resolver for your app
mResolver = getContentResolver();
// Turn on automatic syncing for the default account and authority
mResolver.setSyncAutomatically(ACCOUNT, AUTHORITY, true);
我在 mResolver.setSyncAutomatically(ACCOUNT,AUTHORITY,true)中的eclipse中出错; 它表示如下:
ContentResolver类型中的setSyncAutomatically(Account,String,boolean)方法不适用于参数(String,String,boolean)
所以我被困在这里,教程告诉我这样做,但ACCOUNT需要是类型帐户而不是字符串。教程是否过时了?
答案 0 :(得分:0)
setSyncAutomatically
的第一个参数是Account
对象,而不是String。您正在使用String。您需要使用AccountManager
来获取正确的帐户并使用它。
答案 1 :(得分:0)
使用:
帐户newAccount =新帐户(" AccountName"," com.example.accountType");这可用于访问您已创建的帐户。
答案 2 :(得分:0)
帐户参数来自帐户类型。我在每个设备上都有完整的Android文档,谷歌的BasicSyncAdapter(http://developer.android.com/downloads/samples/BasicSyncAdapter.zip)可以通过AIDE编译,但在自定义身份验证器和SyncAdapter的点上,并不是所有内容都尽可能清晰。我希望这个例子可以帮到你一点:http://udinic.wordpress.com/2013/07/24/write-your-own-android-sync-adapter/