title: SyncService for synchronizing data between android and server not starting
body: <p>I am currently trying to synchronize some data with a server using a background service. Using the following sites as guides:</p>
  
  <ul>
  <li><a href="https://developer.android.com/training/sync-adapters/index.html" rel="nofollow noreferrer">https://developer.android.com/training/sync-adapters/index.html</a></li>
  <li><a href="http://blog.udinic.com/2013/07/24/write-your-own-android-sync-adapter/" rel="nofollow noreferrer">http://blog.udinic.com/2013/07/24/write-your-own-android-sync-adapter/</a></li>
  </ul>
  
  <p>I also scoured StackOverflow and have tried the following solutions:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/32694750/sync-adapter-not-running">Sync Adapter not running</a>
  
  <ul>
  <li>Not possible since I am currently using the account the user logged in with and is added to the account manager</li>
  </ul></li>
  <li><a href="https://stackoverflow.com/questions/30931419/sync-adapter-not-calling-oncreate">Sync Adapter not calling onCreate</a>
  
  <ul>
  <li>By starting the service using the intent, I get the onCreate() to run for the service but it does not run onBind and the adapter is still not called when onChange runs ContentResolver.requestSync(...)</li>
  <li>Also, I already currently use requestSync(ACCOUNT, AUTHORITY, new Bundle()) and I triple checked that the account and authority are the same as what is the account type and authority in the syncadapter.xml</li>
  </ul></li>
  <li><a href="https://stackoverflow.com/questions/33628060/contentresolver-requestsync-in-sync-adapter-is-not-working-in-android">ContentResolver.requestSync in Sync Adapter is not working in Android</a>
  
  <ul>
  <li>I am currently logging and viewing the LogCat with "No Filters" and can see all my other logs. However, the service logs don't show up which means that my service is not created</li>
  </ul></li>
  </ul>
  
  <p>I have tried other solutions on StackOverflow as well but the solutions are essentially encapsulated by those listed above. I am wondering if there is something missing where I need to start this service in my code.</p>
  
  <p>SyncService.java:</p>
  
  <pre><code>public class SyncService extends Service {
  private static SyncAdapter syncAdapter = null;
  private static final Object syncAdapterLock = new Object();
  
  @Override
  public void onCreate() {
      super.onCreate();
      Log.d("SyncService", "[DEBUG]       SyncService created...");
      synchronized (syncAdapterLock) {
          if (syncAdapter == null) {
              syncAdapter = new SyncAdapter(getApplicationContext(), true);
          }
      }
  }
  
  @Override
  public IBinder onBind(Intent intent) {
      Log.d("SyncService", "[DEBUG]      onBind(...) run for SyncAdapter...");
      return syncAdapter.getSyncAdapterBinder();
  }
  }
  </code></pre>
  
  <p>SyncAdapter.java:</p>
  
  <pre><code>public class SyncAdapter extends AbstractThreadedSyncAdapter {
  private ContentResolver resolver;
  private AccountManager am;
  
  public SyncAdapter(Context context, boolean autoInitialize) {
      super(context, autoInitialize);
      resolver = context.getContentResolver();
      am = AccountManager.get(context);
  }
  
  // Maintains compatibility with Android &gt;= 3.0 platform versions
  public SyncAdapter(Context context , boolean autoInitialize, boolean allowParallelSyncs) {
      super(context, autoInitialize, allowParallelSyncs);
      resolver = context.getContentResolver();
      am = AccountManager.get(context);
  }
  
  @Override
  public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) {
      Log.d("SyncAdapter", "[DEBUG]       onPerformSync for account[" + account.name + "] called. Syncing....");
  
      // Connect to server
      // Download and upload data
      // Handle data conflicts and determining if data is current
      // Clean up
  }
  }
  </code></pre>
  
  <p>Observer:</p>
  
  <pre><code>public class AlarmObserver extends ContentObserver {
  Account account;
  
  public AlarmObserver(Handler handler, Account account) {
      super(handler);
      this.account = account;
  }
  
  @Override
  public void onChange(boolean selfChange) {
      onChange(selfChange, null);
  }
  
  @Override
  public void onChange(boolean selfChange, Uri changeUri) {
      Log.d("AlarmObserver", "[DEBUG]     Change to content detected. Account: " + account.name + " Uri: " + changeUri.toString() + " Authority: " + AlarmContract.AUTHORITY);
      ContentResolver.requestSync(account, AlarmContract.AUTHORITY, new Bundle());
  }
  }
  </code></pre>
  
  <p>syncadapter.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;sync-adapter xmlns:android="http://schemas.android.com/apk/res/android"
      android:contentAuthority="livotto.smarthomeapi.AlarmProvider"
      android:accountType="@string/default_account_type"
      android:userVisible="true"
      android:supportsUploading="true"
      android:allowParallelSyncs="false"
      android:isAlwaysSyncable="true" /&gt;
  </code></pre>
  
  <p>Activity where I am using the resolver:</p>
  
  <pre><code>        AccountManager am = AccountManager.get(this);
      Account[] accounts = am.getAccounts();
  
      if (accounts.length &gt; 1) {
          // Dialog should appear asking which account to use
      }
      account = accounts[0];      // for now we take the first account
  
      resolver = getContentResolver();
      observer = new AlarmObserver(new Handler(), account);
      resolver.registerContentObserver(AlarmContract.CONTENT_URI, true, observer);
  </code></pre>