Do you know how to sync my account from my app from accountManager with syncadapter?
I have performed the syncadapter service:
public class SyncAdapterService extends Service {
private static SyncAdapter sSyncAdapter = null;
private static final Object sSyncAdapterLock = new Object();
@Override
public void onCreate() {
Log.d("SYNC","ONCREATE");
synchronized (sSyncAdapterLock) {
if (sSyncAdapter == null) {
sSyncAdapter = new SyncAdapter(getApplicationContext(), true);
}
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return sSyncAdapter.getSyncAdapterBinder();
}
public void onDestroy() {
super.onDestroy();
Log.d("SYNC", "ONDESTROY");
}
}
The xml:
<?xml version="1.0" encoding="utf-8"?>
<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android"
android:contentAuthority="@string/content_authority"
android:accountType="@string/auth_type"
android:userVisible="true"
android:allowParallelSyncs="true"
android:isAlwaysSyncable="true"
android:supportsUploading="false"/>
I added the service in the manifest.xml:
<service
android:name=".SyncAdapterService"
android:exported="true">
<intent-filter>
<action android:name="android.content.SyncAdapter" />
</intent-filter>
<meta-data
android:name="android.content.SyncAdapter"
android:resource="@xml/sync_adapter" />
</service>
And the class that will execute the syncadapter methods:
public class SyncAdapter extends AbstractThreadedSyncAdapter{
private Context mContext;
private static final String PREF_ACCOUNT = "accounts";
public SyncAdapter(Context context, boolean autoInitialize) {
super(context, autoInitialize);
}
@Override
public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) {
String accounts = PreferenceManager.getDefaultSharedPreferences(mContext).getString(PREF_ACCOUNT,"");
if(!accounts.equals(account.name)){
ContentResolver.setIsSyncable(account, authority, 0);
return;
}
}
}
The problem I have is that I'm lost with this method, to tell him to synchronize me with the account that I add from the accountManager.