我按照文档中关于如何创建SyncAdapter的指南,我认为我已经准备好所有部分,但出于某种原因,它不起作用。
这是我的SyncAdapter:
public class MediaSyncAdapter extends AbstractThreadedSyncAdapter {
public static final long SECONDS_PER_MINUTE = 60L;
public static final long SYNC_INTERVAL_IN_MINUTES = 60L;
public static final long SYNC_INTERVAL = SYNC_INTERVAL_IN_MINUTES * SECONDS_PER_MINUTE;
public MediaSyncAdapter(Context context, boolean autoInitialize) {
super(context, autoInitialize);
}
public MediaSyncAdapter(Context context, boolean autoInitialize, boolean allowParallelSyncs) {
super(context, autoInitialize, allowParallelSyncs);
}
@Override
public void onPerformSync(Account account, Bundle bundle, String s,
ContentProviderClient contentProviderClient, SyncResult syncResult) {
Timber.e("IT HAPPENED");
SyncService.getStartIntent(getContext());
}
public static void syncImmediately(Context context) {
Bundle bundle = new Bundle();
bundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
ContentResolver.requestSync(getSyncAccount(context),
context.getString(R.string.content_authority), bundle);
}
public static Account getSyncAccount(Context context) {
AccountManager accountManager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
Account newAccount = new Account(context.getString(R.string.app_name),
context.getString(R.string.sync_account_type));
if (null == accountManager.getPassword(newAccount)) {
if (!accountManager.addAccountExplicitly(newAccount, "", null)) {
return null;
}
}
return newAccount;
}
}
我的存根提供商:
public class StubProvider extends ContentProvider {
/*
* Always return true, indicating that the
* provider loaded correctly.
*/
@Override
public boolean onCreate() {
return true;
}
/*
* Return no type for MIME type
*/
@Override
public String getType(Uri uri) {
return null;
}
/*
* query() always returns no results
*
*/
@Override
public Cursor query(
Uri uri,
String[] projection,
String selection,
String[] selectionArgs,
String sortOrder) {
return null;
}
/*
* insert() always returns null (no URI)
*/
@Override
public Uri insert(Uri uri, ContentValues values) {
return null;
}
/*
* delete() always returns "no rows affected" (0)
*/
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
return 0;
}
/*
* update() always returns "no rows affected" (0)
*/
public int update(
Uri uri,
ContentValues values,
String selection,
String[] selectionArgs) {
return 0;
}
}
触发适配器的服务:
public class MediaService extends Service {
private static MediaSyncAdapter mSyncAdapter = null;
private static final Object sSyncAdapterLock = new Object();
public MediaService() {
super();
}
@Override
public void onCreate() {
synchronized (sSyncAdapterLock) {
if (mSyncAdapter == null) {
mSyncAdapter = new MediaSyncAdapter(getApplicationContext(), true);
}
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return mSyncAdapter.getSyncAdapterBinder();
}
}
我的身份验证服务:
public class AuthenticatorService extends Service {
// Instance field that stores the authenticator object
private MediaAuthenticator mMediaAuthenticator;
@Override
public void onCreate() {
// Create a new authenticator object
mMediaAuthenticator = new MediaAuthenticator(this);
}
/*
* When the system binds to this Service to make the RPC call
* return the authenticator's IBinder.
*/
@Override
public IBinder onBind(Intent intent) {
return mMediaAuthenticator.getIBinder();
}
}
我的身份验证员:
public class MediaAuthenticator extends AbstractAccountAuthenticator {
// Simple constructor
public MediaAuthenticator(Context context) {
super(context);
}
// Editing properties is not supported
@Override
public Bundle editProperties(
AccountAuthenticatorResponse r, String s) {
throw new UnsupportedOperationException();
}
// Don't add additional accounts
@Override
public Bundle addAccount(
AccountAuthenticatorResponse r,
String s,
String s2,
String[] strings,
Bundle bundle) throws NetworkErrorException {
return null;
}
// Ignore attempts to confirm credentials
@Override
public Bundle confirmCredentials(
AccountAuthenticatorResponse r,
Account account,
Bundle bundle) throws NetworkErrorException {
return null;
}
// Getting an authentication token is not supported
@Override
public Bundle getAuthToken(
AccountAuthenticatorResponse r,
Account account,
String s,
Bundle bundle) throws NetworkErrorException {
throw new UnsupportedOperationException();
}
// Getting a label for the auth token is not supported
@Override
public String getAuthTokenLabel(String s) {
throw new UnsupportedOperationException();
}
// Updating user credentials is not supported
@Override
public Bundle updateCredentials(
AccountAuthenticatorResponse r,
Account account,
String s, Bundle bundle) throws NetworkErrorException {
throw new UnsupportedOperationException();
}
// Checking features for the account is not supported
@Override
public Bundle hasFeatures(
AccountAuthenticatorResponse r,
Account account, String[] strings) throws NetworkErrorException {
throw new UnsupportedOperationException();
}
}
我的相关清单条目:
<service
android:name="com.android.jventajas.app.moviegami.data.sync.AuthenticatorService">
<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.android.jventajas.app.moviegami.data.sync.MediaService"
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/syncadapter" />
</service>
<provider
android:name="com.android.jventajas.app.moviegami.data.sync.StubProvider"
android:authorities="com.android.jventajas.app.moviegami"
android:exported="false"
android:syncable="true"/>
我的xml syncadapter定义:
<sync-adapter
xmlns:android="http://schemas.android.com/apk/res/android"
android:contentAuthority="@string/content_authority"
android:accountType="@string/sync_account_type"
android:userVisible="false"
android:supportsUploading="false"
android:allowParallelSyncs="false"
android:isAlwaysSyncable="true"/>
我的身份验证员:
<account-authenticator
xmlns:android="http://schemas.android.com/apk/res/android"
android:accountType="@string/sync_account_type"
android:icon="@drawable/common_ic_googleplayservices"
android:smallIcon="@drawable/common_ic_googleplayservices"
android:label="@string/app_name"/>
有谁知道为什么没有触发onperformsync?
谢谢。