我还在努力与同步适配器进行定期同步。我可以使用请求同步手动触发我的 SyncAdapter,但添加PeriodicSync 不会触发。
我注意到在all在这些示例中,进入“设置”->“帐户”会显示 SyncAdapter 的帐户,其中有一个小“同步轮”(如果同步正常则通常为绿色;如果最近同步失败则为红色)以及“上次同步”时间戳。我的帐户(从谷歌文档 http://developer.android.com/training/sync-adapters/creating-authenticator.html)没有列出任何类似同步轮或时间戳的内容。
进一步挖掘暴露了问题所在:我的内容提供商没有label在它的 XML 中(我之前使用它没有任何问题,所以我浏览了文档的这一部分)。为我的内容提供商添加一个简单的标签,使其显示在“设置”中我的帐户下,以及同步轮和时间戳。
以下是从我的应用程序中获取灵感的一些代码。希望它能帮助某人、某处!
/res/xml/sync_adapter.xml
<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android"
android:accountType="com.example.database"
android:allowParallelSyncs="false"
android:contentAuthority="com.example.database.data.provider"
android:isAlwaysSyncable="true"
android:supportsUploading="false"
android:userVisible="true" />
/com/example/database/data/MySyncAdapter
public class MySyncAdapter extends AbstractThreadedSyncAdapter {
private static final String TAG = MySyncAdapter.class.getSimpleName();
Context context;
public MySyncAdapter(Context context, boolean autoInitialize) {
super(context, autoInitialize);
this.context = context;
}
public MySyncAdapter(Context context, boolean autoInitialize, boolean allowParallelSyncs) {
super(context, autoInitialize, allowParallelSyncs);
this.context = context;
}
@Override
public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) {
Log.e(TAG, "Performing Sync");
}
}
AndroidManifest.xml(内容提供商需要标签才能显示在帐户中)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.database">
<uses-sdk tools:node="replace" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="18" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />
<uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
<uses-permission android:name="android.permission.READ_SYNC_STATS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:logo="@drawable/chef_collection_logo_white"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<provider
android:name="com.example.database.data.MyContentProvider"
android:authorities="com.example.database.data.provider"
android:label="my provider"
android:exported="false"
android:multiprocess="true"
android:syncable="true" />
<activity
android:name=".app.MainActivity"
android:label="@string/title_activity_main">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name="com.example.database.data.AuthenticatorService"
android:exported="true"
android:process=":auth">
<intent-filter>
<action android:name="android.accounts.AccountAuthenticator" />
</intent-filter>
<meta-data
android:name="android.accounts.AccountAuthenticator"
android:resource="@xml/authenticator" />
</service>
<service
android:name="com.example.database.data.MySyncAdapterService"
android:exported="true"
android:process=":sync">
<intent-filter>
<action android:name="android.content.SyncAdapter" />
</intent-filter>
<meta-data
android:name="android.content.SyncAdapter"
android:resource="@xml/sync_adapter" />
</service>
</application>
</manifest>
主要活动。我在首次运行设置向导后调用此代码,但您可以在任何地方调用它。这将尝试同步每个30秒(用于测试)。请注意,谷歌文档目前是错误的,因为它提到它需要毫秒而不是秒。另一件需要注意的事情是您不能将 null 作为包传递。这样做会抛出 IllegalArgumentException 或类似的异常。
//Create Account
mAccount = createSyncAccount(this);
//Turn on periodic syncing
ContentResolver resolver = getContentResolver();
resolver.setIsSyncable(mAccount, AUTHORITY, 1);
resolver.setSyncAutomatically(mAccount, AUTHORITY, true);
resolver.addPeriodicSync(
mAccount,
AUTHORITY,
Bundle.EMPTY,
30);